Docker compose is the most important component of the Docker environment that is usually used to configure the services and manage multi-container applications. To configure services in Docker, compose uses the “docker-compose.yml” file. Then, the “docker-compose” command will start and configure services. Users can create the replica of services or containers in docker-compose using the “–scale” option.
This blog will demonstrate how to use the “–scale” within the “docker-compose” file.
How to Use “–scale” Within “docker-compose” File?
The “–scale” is usable only in the “docker-compose” command, not in the “docker-compose.yml” file. To specify the “–scale” option in the docker-compose file, utilize the “replicas” variable within the docker-compose file. The “replicas” specify the number of services or containers that will be duplicated and started.
To replicate the docker-compose service or container, utilize the provided procedure.
Step 1: Create “docker-compose.yml” File
First, create a simple “docker-compose.yml” file and paste the following code:
services:
web:
build: .
hostname: golang.example.com
golang:
image: "golang:alpine"
deploy:
replicas: 4
ports:
- "8080-8084:8080"
In the above code, “web” and “golang” are two services, where “web” will use the “Dockerfile”, and “golang” will pull images from the registry. Here, “replicas” is utilized to specify the number of services that should be replicated and started:
Step 2: Create and Start Containers
Next, use the “docker-compose up” command to start docker-compose service:
Step 3: Verify if Services or Containers are Replicated
For the verification, open the “Docker Desktop” application and check the containers. It can be observed that replicas of the “golang” service are generated successfully:
Alternatively, users can use the “docker ps -a” command to view the containers in the terminal:
How to Use “–scale” in “docker-compose” Command?
You can also use the “–scale” option in the “docker-compose up” command to scale the specified service to the number of instances:
The above command will scale or replicate the “web” service four times:
We have illustrated how to use “–scale” within the docker-compose file.
Conclusion
There is no “–scale” variable used in the “docker-compose” file. However, users can replicate the services of docker-compose within the file with the help of the “replicas” variable. The “replicas” specify the number of services or containers that will be duplicated and started. Additionally, you can utilize the “–scale” option in the “docker-compose up” command to replicate the service. This write-up has illustrated how to use “–scale” within the docker-compose file.