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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github