CodeGym /Courses /Docker SELF /Scaling Services

Scaling Services

Docker SELF
Level 16 , Lesson 2
Available

8.1 Command docker compose scale

Scaling services is the process of increasing or decreasing the number of container instances that run the same service to manage load and ensure application fault-tolerance. In Docker Compose, the docker compose scale command (in older versions) or the --scale option in newer versions is used for scaling services.

Main concepts of scaling:

  1. Scalability: The ability to easily increase or decrease the number of service instances based on current load.
  2. Fault-tolerance: Running multiple service instances provides fault-tolerance, as the failure of one instance doesn't result in the shutdown of the entire service.
  3. Load balancing: Distributing the load among multiple service instances to optimize performance.

Using the docker compose scale command

Command syntax:


docker compose scale SERVICE=NUM

Where:

  • SERVICE: the name of the service you want to scale.
  • NUM: the number of instances that should be running.

Example usage:

Suppose you have a compose.yaml file with a web service defined.

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

To run 3 instances of the web service, use the command:

Terminal

docker compose scale web=3

Note: Before running the docker compose scale command, make sure the services are started using the docker compose up command.

Using the --scale option in newer versions

In newer versions of Docker Compose, the docker compose scale command has been replaced with the --scale option, which is used together with the up command.

Terminal

docker compose up --scale web=3

This approach is preferred because it allows you to simultaneously start and scale services.

8.2 Practical Examples

Example 1: Scaling a Web Server

Create a file compose.yaml:

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80-90:80"

Start and scale the service to 3 instances:

Terminal

docker compose up --scale web=3

Now you'll have 3 instances of the Nginx web server running on ports within the range 80-90.

Important! If the service is expected to scale to multiple instances, you need to specify a range of ports, for example: "80-90:80". This "reserves" a range of 10 ports on the host machine. When a service replica is added, it will be assigned a port from the range. If this isn't done, ports will be chosen randomly, and the replicas will become unavailable.

Example 2: Scaling with a Database

Create a file compose.yaml:

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80-90:80"
    depends_on:
      - db

  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

Start and scale the web service to 4 instances:

Terminal

docker compose up --scale web=4

Now you'll have 4 instances of the Nginx web server working with a single PostgreSQL database.

8.3 Managing Scalable Services

After scaling services, you can use Docker Compose commands to manage the instances.

Viewing Running Containers

Use the docker compose ps command to view all running containers:

Terminal

docker compose ps

Stopping and Removing Scalable Services

To stop and remove all instances of a service, use the docker compose down command:

Terminal

docker compose down

Tips and Tricks:

  1. Load Balancing: use load balancers to distribute traffic between multiple instances of the service. For example, you can use Nginx or HAProxy for this purpose.
  2. Data Preservation: ensure that the data created by scalable services is saved in volumes or external storage to prevent data loss when containers are stopped or removed.
  3. Monitoring: use monitoring tools like Prometheus or Grafana to track the performance and status of scalable services.
  4. Auto-Scaling: consider automatically scaling services based on load using orchestrators like Kubernetes or Docker Swarm.
3
Task
Docker SELF, level 16, lesson 2
Locked
Scaling Nginx
Scaling Nginx
3
Task
Docker SELF, level 16, lesson 2
Locked
Scaling with a Database
Scaling with a Database
3
Task
Docker SELF, level 16, lesson 2
Locked
Scaling an application with a load balancer
Scaling an application with a load balancer
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION