1

I want to deploy my little web application with docker on digitalocean. Problem is, that i didn't separate flask + celery, so everything is in one container. Everytime i run docker-compose up the web container crashses ("exited with code 0").

The project structure looks as follows:

.
.
├── docker-compose.yml
├── nginx
│   ├── dockerfile
│   └── nginx.conf
└── web
    ├── app.ini
    ├── bot
    │   ├── __init__.py
    │   ├── main.py
    │   ├── __pycache__
    │   └── timer.py
    ├── celeryd.pid
    ├── config.py
    ├── dockerfile
    ├── dockerignore
    ├── flask_app.py
    ├── __init__.py
    ├── requirements.txt
    ├── tasks.py
    └── webapp
        ├── bot
        ├── __init__.py
        ├── models.py
        ├── __pycache__
        ├── static
        ├── templates
        └── users

This is the docker-compose.yml:

version: "3.7"

services:

  rabbitmq:
    image: rabbitmq:3-management
    hostname: rabbitmq
    container_name: rabbitmq
    environment:
      RABBITMQ_ERLANG_COOKIE: 'SWQOKODSQALRPCLNMEQG'
      RABBITMQ_DEFAULT_USER: 'user123'
      RABBITMQ_DEFAULT_PASS: 'password123'
      RABBITMQ_DEFAULT_VHOST: '/webapp'
    ports:
      - "15672:15672"
      - "5672:5672"
    volumes:
      - rabbitmq_storage:/var/lib/rabbitmq
    networks:
      netzwerk123:
        aliases:
          - rabbitmq

  db:
    image: mariadb
    restart: always
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: 'password123'
      MYSQL_USER: 'user123'
      MYSQL_PASSWORD: 'password123'
      MYSQL_DATABASE: 'webapp'
    ports:
      - "3306:3306"
    volumes:
      - db_storage:/var/lib/mysql
    networks:
      netzwerk123:
        aliases:
          - db


  nginx:
    build: ./nginx  
    container_name: nginx
    restart: always
    ports:  
      - "80:80"
    networks:
      netzwerk123:
        aliases:
          - ngnix


  web:
    build: ./web  
    container_name: web
    hostname: web
    restart: always
    environment:    
      FLASK_DEBUG: 'True'
      FLASK_APP: 'flask_app.py'
    command: celery -A tasks.celery worker --loglevel=info --detach
    expose:
      - 8080
    networks:
      netzwerk123:
        aliases:
          - web
    depends_on:     
      - nginx
      - db
      - rabbitmq
    links:
      - rabbitmq
    tty: true
    stdin_open: true


  adminer:        #only for development purposes
    image: adminer
    container_name: adminer
    restart: always
    ports:
      - "8081:8080"
    depends_on:
      - db
    networks:
      netzwerk123:
        aliases:
          - adminer



volumes:                                    
  db_storage:
  rabbitmq_storage:

networks:
  netzwerk123:

If i comment "command: celery -A tasks.celery worker --loglevel=info --detach" everything runs just fine, since celery is not running. Webpage is fully functioning, database is active. Everything's fine, except for celery.

By uncommenting the command line, docker throws the "web exited with error code 0".

I guess this is due to the fact, that docker exits the container after successfully executing the command. So there are two problems i face:

1) How to run celery in the background of the same container of celery, so that there won't appear the error code 0?

2) How to run celery AND flask parallel in the container?

-> i already tried the following, but same error:

command: bash -c "uwsgi app.ini && celery -A tasks.celery worker --loglevel=info --detach"

Any help is highly appreciated :)

EDIT: The reason i put celery + flask in one container is my task.py. In here i import all sqlalchemy models from the web.webapp.models. So i need to have access to the flask directory and currently i don't see a way how to separate this into two containers

3
  • You need to share a volume between both containers if the need access to the same files Commented Aug 3, 2019 at 10:24
  • I don't know much about docker compose and digitalocean and nothing about celery, so my statement is likely wrong, but you might want to run two separate digitalocean servers and provide an API to communicate between them. If you need to run them in the same server: since a digitalocean server is a docker container (I think), then you'll have to run "docker in docker", so you run two docker containers inside a digitalocean docker container... this might be slow and I don't know if digitalocean provides an easy way to do docker in docker. Commented Aug 3, 2019 at 11:02
  • Also as @David Maze said you can just put the shared code in the two separate container images. Commented Aug 3, 2019 at 11:03

1 Answer 1

5

If the only thing you need to "share" between two different processes is the code, that's already in the Docker image that gets built. You can very easily run two different containers off the same image, but with different commands.

services:
  web:
    build: ./web  
    environment:    
      FLASK_DEBUG: 'True'
      FLASK_APP: 'flask_app.py'
    command: flask run --host=0.0.0.0
    depends_on:     
      - nginx
      - db
      - rabbitmq
  celery:
    build: ./web  
    restart: always
    command: celery -A tasks.celery worker --loglevel=info
    depends_on:     
      - db
      - rabbitmq

Note that both build: the same image. Also, I've made celery worker not --detach so it runs as a foreground process; the container will stay running just so long as the command: is still running.

(For this fragment to work in the context of your original docker-compose.yml file you would need to outright delete all of the networks: blocks in the whole file, but it will work fine: Docker Compose creates a default network and attaches all services to it using the name of their service block. I've also removed a number of other options that just don't matter.)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks David, it works! Didn't expect that it would be so easy, lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.