WittCodešŸ’»

Scaling Services with Docker Compose

By

Learn how to horizontally scale an application using Docker Compose. To demonstrate, we will spin up multiple Node Docker containers using both the command line and a Docker Compose configuration.

Table of Contents šŸ“–

What is Horizontal Scaling?

Docker Compose makes it easy to horizontally scale an application. Horizontal scaling means adding more machines/nodes to handle a workload like internet traffic or data. These nodes are typically created through simple replication.

Demonstration

To demonstrate, consider the following Dockerfile and docker-compose.yaml files.

FROM node
WORKDIR /server
COPY package*.json .
RUN npm i --omit=dev
COPY . .
CMD ["npm", "start"]
services:
  server:
    pull_policy: build
    image: my-node-i
    build:
      context: server
      dockerfile: Dockerfile
    env_file: .env
    volumes:
      - my-node-v:/server/node_modules

volumes:
  my-node-v:
    name: my-node-v

These configurations spin up a Node container that houses an Express application. Running docker compose up will spin up this container.

docker compose up -d
āœ” Network docker-compose-scale-demo_default     Created                                                                                                           0.0s 
āœ” Container docker-compose-scale-demo-server-1  Started  

We now have one instance of this Node server running and ready to go.

Scaling the Node Containers

Now lets say that the workload to our application is growing. Due to this, we want to scale our Node application to 5 nodes. We can do this with the docker compose scale command.

docker compose scale server=5
[+] Running 5/5
 āœ” Container docker-compose-scale-demo-server-1  Running                                                                                                           0.0s 
 āœ” Container docker-compose-scale-demo-server-5  Started                                                                                                           0.3s 
 āœ” Container docker-compose-scale-demo-server-2  Started                                                                                                           0.8s 
 āœ” Container docker-compose-scale-demo-server-3  Started                                                                                                           0.7s 
 āœ” Container docker-compose-scale-demo-server-4  Started  
docker compose scale server=3
[+] Running 5/5
 āœ” Container docker-compose-scale-demo-server-1  Running                                                                                                           0.0s 
 āœ” Container docker-compose-scale-demo-server-2  Running                                                                                                           0.0s 
 āœ” Container docker-compose-scale-demo-server-3  Running                                                                                                           0.0s 
 āœ” Container docker-compose-scale-demo-server-5  Removed                                                                                                           0.8s 
 āœ” Container docker-compose-scale-demo-server-4  Removed    

The docker compose scale command allows us to scale one or more services up or down. We can specify the number of replicas to scale.

INFO: Note that this command returns immediately but the scaling of the service might take some time.

We can also scale more than one service at a time with this command.

docker compose scale server=5 proxy=10

Docker Compose Deploy Specification

We can also scale a service using the docker-compose.yaml configuration file. This is done with the deploy specification and the replicas key.

services:
  server:
    pull_policy: build
    image: my-node-i
    build:
      context: server
      dockerfile: Dockerfile
    env_file: .env
    deploy:
      replicas: 6
    volumes:
      - my-node-v:/server/node_modules

volumes:
  my-node-v:
    name: my-node-v

This tells Docker Compose to create 6 docker containers of the server service.

ERROR: It is important to note that setting the container port and name can create collisions when working with replicas. This is why we typically have Docker Compose handle the container names and ports for us.