2

Is it possible to control the order of starting containers in Docker-compose beside the following method?

https://docs.docker.com/compose/startup-order/

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres

I have a container which depends on a redis databse container. However, it takes longer for redis to load in memory which causes the first container to exit. For now, I am using always restart method to deal with the problem as a workaround.

I was wondering if there is a better alternative as I would try to avoid the wait for it script?

2 Answers 2

7

You can specify a healthcheck in your redis container and add condition: service_healthy to your depends_on field. This works since compose 2.1

version: "2.1"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      "db":
        condition: service_healthy
    command: ["python", "app.py"]
  db:
    image: postgres

Detailed example of usage is here: https://github.com/peter-evans/docker-compose-healthcheck/blob/master/docker-compose.yml

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

3 Comments

Let me try this if it works and will get with the result.
Is there an equivelent for version 3? github.com/moby/moby/issues/30404#issuecomment-323212621
Version 3 no longer supports the condition form of depends_on
3

By using the "depends_on" command in docker compose file, it is possible to change the starting order of the containers and give the priority to the containers which are needed to start early.

nginx:
    container_name: OTP-Nginx
    build: 
      context: ./nginx
      args:
        - comapanycode=${COMPANY_CODE}
        - dbtype=${DB_TYPE}
    ports:
      - "80:80"
    links:
      - db:db
    volumes:
      - ./nginx/octopus_nginx_params:/etc/nginx/octopus_nginx_params
     enabled/retail.octopusdashboard.com
      - /home/eleos/octopusupdates/DASHBOARDGLOBAL/branches-cdbwip:/var/www/${COMPANY_CODE}/cdb 
    depends_on:
      - db

Comments

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.