Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

depends_on doesn't wait for another service in docker-compose 1.22.0

My current docker-compose.yml -

# This docker-compose file uses '.env' file present in the current directory, 
# for database credentials. If you want to change the credentials please 
# change the data in '.env'.
# '.env' file might be hidden as dot files are hidden please unhide to see it.
# Know more about '.env' file: https://docs.docker.com/compose/env-file/

version: '3'

services: 
  postgresdb:
    image: postgres:9.5
    environment: 
      POSTGRES_USER: ${ENV_POSTGRES_USER}
      POSTGRES_PASSWORD: ${ENV_POSTGRES_PASSWORD}
      POSTGRES_DB: ${ENV_POSTGRES_DB}
    volumes: 
      - "../app/volumes/postgres/data:/var/lib/postgresql/data"

  # This is python service. It uses python 3.6 as base image.
  # It will build this service using the Dockerfile present in current directory
  # To modify the values of environment variables please open '.env' file.
  # This service will not run until postgresdb service gets started
  python-app:
    image: python:3.6
    build: .    # Builds using Dockerfile from current directory
    depends_on: 
      - postgresdb
    ports: 
      - "5001:5001"
    tty: true
    volumes: 
      - "../app/volumes/trained_knn_model.clf:/usr/src/app/my-app/trained_knn_model.clf"
      - "../app/volumes/XYPickle.pickle:/usr/src/app/my-app/XYPickle.pickle"
    environment: 
      - POSTGRES_USER=${ENV_POSTGRES_USER}
      - POSTGRES_PASSWORD=${ENV_POSTGRES_PASSWORD}
      - POSTGRES_HOST=${ENV_POSTGRES_HOST}
      - POSTGRES_PORT=${ENV_POSTGRES_PORT}
      - POSTGRES_DB=${ENV_POSTGRES_DB}

My docker-compose.yml file contains 2 services. I have specified postgrasdb service to start before python-app service using depends_on but the docker-compose in not running the services in specified order. How can I get postgrasdb service to be run before python-app service? I am running docker-compose up --build --remove-orphans command.

like image 313
Sunil Avatar asked Oct 08 '18 10:10

Sunil


People also ask

How can we control the start up order of services in Docker compose?

You can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on , links , volumes_from , and network_mode: "service:..." .

Is Docker compose deprecated?

Compose V1 is marked as deprecated, and we'll begin patching only high-severity vulnerabilities or fixing critical bugs until the next milestone. Developers can continue to alias docker-compose to use docker compose.

Which is the latest Docker compose version?

The latest Compose file format is defined by the Compose Specification and is implemented by Docker Compose 1.27. 0+. Looking for more detail on Docker and Compose compatibility?

What is the use of docker wait command?

The 'docker wait' is a command that is used to wait or block until one or more containers stop, and then it outputs their exit codes which means you cannot use your terminal if you are running the command on the terminal.

What is wait for dependency in Docker Compose?

Docker Compose: Wait for Dependencies. If you have ever used Docker Compose to run multi-container applications, there is a good chance that you have run into the following situation. Service A depends on service B, but service B takes a a while to start up and be ready. Because of this, you must add some extra "wait for service B" logic ...

Why is my docker compose service taking so long to start?

If you have ever used Docker Compose to run multi-container applications, there is a good chance that you have run into the following situation. Service A depends on service B, but service B takes a a while to start up and be ready. Because of this, you must add some extra "wait for service B" logic into service A's startup procedure.

What is the difference between docker-compose up and Docker-compse?

Unlike docker-compose up, running docker-compse run with a compose file in which some services depend_on others, those other services are started but Docker Compose does not wait for those services' health checks to return 'healthy'. This was noted here in #2833. Create a Compose file with two services, one (A) depending on the other (B).

What does depends_on mean in Docker Compose?

The documentation suggests that, in Docker Compose version 2 files specifically, depends_on: can be a list of strings, or a mapping where the keys are service names and the values are conditions. For the services where you don't have (or need) health checks, there is a service_started condition.


Video Answer


1 Answers

Note that depends_on only waits for the other container to be up, but not for the process it is running to start. The thing that could probably be happening in your case is that you are trying to connect to the postgres process on its specified port while it's still getting started.

There are two ways you can tackle such a scenario -

  1. Specify some sort of restart clause for your python-app container - You're probably seeing your python-app container in failed state and so you have posted this question. restart: on-failure:10 in the docker-compose.yml for your python-app service will restart your container up to 10 times in case it fails connecting to the postgres container. This will ensure that you would have given it enough time before the postgres container is up and running...the process that is.

  2. Use an external tool like dockerize that allows you to wait on other services before starting up the container. This essentially gives you the behavior you desire with depends_on.

like image 137
gravetii Avatar answered Sep 28 '22 10:09

gravetii