How to Scale Services in Docker Compose

Docker compose is a core tool of Docker that is used to process and manage multiple containers applications and microservices. This tool is used to configure the services of software and applications in the “YAML” file. The Docker compose provides various key functionalities to support containers and scaling of Docker services is one of them.

Docker scaling means scaling or creating replicas of one or more services. These replicas can be used for testing, sharing, and running the same service in different containers. In Docker compose, you can use the “–scale” flag along with the “docker-compose up” command to start specified numbers of replicas of a particular service.

This write-up will illustrate how to scale service in Docker compose.

How to Scale Services in Docker Compose?

To scale the services in Docker, compose, first, specify the service in the compose file. Then, scale and start the service by utilizing the “–scale” option along with the “docker-compose up” command.

While scaling of service, most users got the below-highlighted error:

This error occurs because users try to run different scaling replicas on the same port. While specifying the number of ports in compose, assign multiple ports or allow Docker to allocate them exposing ports automatically. For the explanation, check out the given steps.

Step 1: Create the Docker File
First, create a Dockerfile and paste the mentioned code block into the file. These instructions will be used to configure the “Golang” program in Docker compose:

FROM golang:1.8
WORKDIR /go/src/app
COPY main.go .
RUN go build -o webserver .
EXPOSE 8080:8080
ENTRYPOINT ["./webserver"]

Step 2: Make Compose File
Next, create a “docker-compose.yml” file that has the following instructions:

  • services” specify the composing service. For instance, we have defined the “web” service in the “docker-compose.yml” file.
  • build” key will read instructions from the Docker file to configure the “web” service in the compose container.
  • ports” defines exposing ports. In the code below, we have set “8080:8080” as the exposing port. But the problem with this port is it can only run one service on port “8080” and show the above-mentioned error of replicas:
version: "alpine"
    build: .
      - 8080:8080

To resolve this error, you can specify the value of the port in the range, such as “80-85:8080” or simply define “8080” and lets the Docker decide on which port container will execute:

Step 3: Scale the Compose Service
Next, scale the service to run replicas in a different container. For this purpose, utilize the “–scale” option and set its value as “<service name>=< qno of replicas>”:

> docker-compose up --scale web=2

For the verification, list down the compose container with the mentioned command. Here, you can see we have successfully run the two replicas on ports “61844” and “61845” assigned by Docker:

> docker-compose ps -a

For the confirmation, navigate to the container’s assigned port and check if the service is running or not. For this purpose, first, we have navigated to the “61844” port:

Here, you can see our scaling service has been successfully running on different ports:

That’s all about how to scale services in Docker compose.


To scale the service in composing, first, configure the Docker service in the “docker-compose.yml” file. Then, scale the service using “–scale” with the value “<service>=<no of replicas>” in the “docker-compose up” command. This write-up has demonstrated how to scale service in Docker compose.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.