How to make docker-compose scale start dependencies?

8 min read

One way to make docker-compose scale start dependencies is to use the depends_on option and specify the number of instances of each service you want to start.

For example, let's say you have two services in your docker-compose file app and database, and you want to scale app to 3 instances. To ensure that all instances of app start only after the database service has started, you can use the following configuration in your docker-compose file:

version: '3'

services:
  app:
    image: your-app-image
    scale: 3 # Scale app to 3 instances
    depends_on:
      database:
        condition: service_healthy # Wait for database to be healthy before starting app

  database:
    image: your-database-image

Here, the depends_on option specifies that app service depends on the database service, and the condition: service_healthy ensures that the app service starts only after the database service is healthy.

When you run docker-compose up, it will start the database service first, wait for it to be healthy, and then start 3 instances of the app service while ensuring that they start only after the database service has started.

Similarly, you can scale any service in your docker-compose file and ensure that its dependencies are started first using the depends_on option.