0

I am new with docker. I am having trouble with multiple containers deploy at a same time, it's occurring race condition. Every time I enter docker-compose up --build command elasticsearch or redis starts first and database starts and exits with error code 0 as well as celery and nginx. I tried using "sleep" command, but no luck(maybe I missed something). Here is my docker-compose.yml file -

version: "3"

services:
  db:
    image: postgres:9.6-alpine
    container_name: myblogdb
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=mydb
    volumes:
      - myblogdb_data:/var/lib/postgresql/data/
    ports:
      - "4949:5432"

  web:
    build: ./app
    command: sh -c "gunicorn djangoApp.wsgi:application --bind 0.0.0.0:8000"
    volumes:
      - ./app:/usr/src/app/
      - my_blog_static_volume:/usr/src/app/djangoApp/settings/staticfiles
      - my_blog_media_volume:/usr/src/app/mediafiles
    ports:
      - "8000:8000"
    depends_on:
      - db
      - redis
      - es

  nginx:
    restart: always
    build: ./nginx
    volumes:
      - my_blog_static_volume:/usr/src/app/djangoApp/settings/staticfiles
      - my_blog_media_volume:/usr/src/app/mediafiles
    ports:
      - "1337:80"
    depends_on:
      - web


  redis:
    image: "redis:alpine"

  es:
    image: elasticsearch:5.6.15-alpine
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms256M -Xmx256M"  
    volumes:
      - my_blog_esdata:/usr/share/elasticsearch/data/
    ports:
      - "9200:9200"

  celery:
    restart: always
    build: ./app
    command: sh -c "celery -A djangoApp worker -l info"
    volumes:
      - ./app:/usr/src/app/
    depends_on:
      - db
      - redis
      - web


  celery-beat:
    restart: always
    build: ./app
    command: sh -c "celery -A djangoApp beat -l info"
    volumes:
      - ./app:/usr/src/app/
    depends_on:
      - db
      - redis
      - web


volumes:
  myblogdb_data:
  my_blog_static_volume:
  my_blog_media_volume:
  my_blog_esdata:

Please let me know if I'm missing something here. Thanks

1 Answers1

0

You need to add a script like wait-for-it or wait-for in order to control startup and shutdown order in compose that basically tells a service to wait for another service before running the start command.

So if you want Django to wait for PostgreSQL the command in docker-compose will be:

["./wait-for", "db:5432", "--", "gunicorn", "djangoApp.wsgi:application", "--bind", "0.0.0.0:8000"]

There is a full explanation in the following answer, the answer describe it for MySQL and Golang but same concept applies to your case

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61