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

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 are 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.


depends_on just guaranteed that database service run before web service. To run web service after database is ready, use wait_for_it.sh script https://docs.docker.com/compose/startup-order/ For example, i have a docker-compose.yml file with two services: app and db, and i want to run container app after db service are ready:

docker-compose.yml

version: "2"
services:
  app:
    image: employee-jdbc
    ports:
      - "8080:8080"
    networks:
      - employee-mysql
    depends_on:
      - mysqldb
    container_name: employee-jdbc
  db:
    image: mysql:5.7
    networks:
      - employee-mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=bootdb  
    container_name: mysqldb
networks:
  employee-mysql:
    external: true

In Dockerfile to build employee-jdbc image:

Dockerfile

FROM openjdk:8
COPY ./target/*.jar ROOT.jar
COPY wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh
ENTRYPOINT ["./wait-for-it.sh","db:3306","--","java","-jar","ROOT.jar"]

wait_for_it.sh file you can download at: https://github.com/vishnubob/wait-for-it