How to make Docker Compose wait for container X before starting Y?

Better Stack Team
Updated on April 7, 2023

You can make Docker Compose wait for a container X to be fully up and running before starting container Y by using the depends_on option in your docker-compose.yml file.

The depends_on option allows you to specify dependencies between services. By default, Docker Compose starts all services simultaneously, but you can use depends_on to specify that a service should wait for another service to be fully up and running before starting.

Here is an example docker-compose.yml file that shows how to use the depends_on option:

 
version: "3"

services:
  service-x:
    build: .
    ports:
      - "8080:8080"
  service-y:
    build: .
    ports:
      - "8081:8081"
    depends_on:
      - service-x

In this example, we have two services, service-x and service-y. The depends_on option is used to specify that service-y should wait for service-x to be fully up and running before starting. This means that Docker Compose will start service-x first and wait for it to be fully up and running before starting service-y.

Note that the depends_on option does not guarantee that the dependent service is fully up and running, it only waits for the container to be started. It is recommended to use a health check to make sure that the dependent service is fully up and running before starting the dependent service.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →