How to `--scale` from within docker-compose file

Viewed 9047

Let's say I have the following docker-compose.yml file:

version: '3'

services:
    load-balancer:
        ...
        ...

    web-application:
        ...
        ...

If I want to run this with 5 replicas of web-application, I have to issue this command:

docker-compose up --scale web-application=5

Is there any way to tell Docker to do the --scale web-application=5 bit from within the docker-compose.yml file?

2 Answers

You can specify the number of replicas in docker-compose only when working in swarm mode.

See the deploy directive.

Example:

services:
  redis:
    image: redis:latest
    deploy:
      replicas: 2

From the documentation:

This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run.

Lastly, there is some discussion about this feature (since it used to be possible) in this GitHub issue, and here is the mention in the most recent compose spec.

I tested replicas in docker compose file, the code that worked for me is the following. docker container example with 5 replicas

You should use docker-compose up -d to execute the configuración

You should see the next results. results.

The results in web browser should be the following: nginx in web browser

Related