How to use docker compose alpha scale command to scale services

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose alpha scale command to scale services defined in a Docker Compose project. We will begin by preparing a simple Docker Compose project, which includes installing Docker Compose, creating a basic web application, and defining its Dockerfile and dependencies.

Following the project setup, you will explore how to scale a single service using docker compose alpha scale, scale multiple services simultaneously, and scale a service without starting its dependencies. This hands-on experience will demonstrate the flexibility and power of the scale command for managing the number of running instances of your services.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/DockerfileGroup(["Dockerfile"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/DockerfileGroup -.-> docker/build("Build Image from Dockerfile") subgraph Lab Skills docker/run -.-> lab-555071{{"How to use docker compose alpha scale command to scale services"}} docker/ls -.-> lab-555071{{"How to use docker compose alpha scale command to scale services"}} docker/ps -.-> lab-555071{{"How to use docker compose alpha scale command to scale services"}} docker/rm -.-> lab-555071{{"How to use docker compose alpha scale command to scale services"}} docker/build -.-> lab-555071{{"How to use docker compose alpha scale command to scale services"}} end

Prepare a simple Docker Compose project

In this step, we will prepare a simple Docker Compose project. Since Docker Compose is not pre-installed in the LabEx environment, we will first install it. Then, we will create a simple web application and a Docker Compose file to define and run it.

First, let's install Docker Compose. We will download the Docker Compose binary and make it executable.

sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now, let's verify the installation by checking the Docker Compose version.

docker-compose --version

You should see the installed version of Docker Compose in the output.

Next, we will create a simple web application. We will use a basic Python Flask application. Create a directory for our project and navigate into it.

mkdir my-web-app
cd my-web-app

Inside the my-web-app directory, create a file named app.py with the following content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Docker Compose!'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

This is a simple Flask application that returns "Hello, Docker Compose!" when accessed.

Now, we need a Dockerfile to build a Docker image for our Flask application. Create a file named Dockerfile in the my-web-app directory with the following content:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

This Dockerfile uses a slim Python 3.9 image, sets the working directory, copies and installs dependencies from requirements.txt, copies the application code, exposes port 5000, and specifies the command to run the application.

We also need a requirements.txt file for the Flask dependency. Create a file named requirements.txt in the my-web-app directory with the following content:

Flask==2.2.2

Finally, we will create a docker-compose.yml file to define our service. Create a file named docker-compose.yml in the my-web-app directory with the following content:

version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"

This docker-compose.yml file defines a service named web. It tells Docker Compose to build the image using the Dockerfile in the current directory (.) and map port 5000 on the host to port 5000 in the container.

Now, let's build and run the service using Docker Compose. Make sure you are in the ~/project/my-web-app directory.

docker-compose up -d

This command builds the image (if not already built), creates a container for the web service, and starts it in detached mode (-d).

You can check if the container is running using the docker ps command.

docker ps

You should see a container for the my-web-app-web-1 service running.

To verify that the application is working, you can access it using curl.

curl http://localhost:5000

You should see the output "Hello, Docker Compose!".

Scale a service using docker compose alpha scale

In this step, we will learn how to scale a service using the docker compose alpha scale command. Scaling a service means increasing or decreasing the number of running instances (containers) for that service. This is useful for handling increased load or for high availability.

The docker compose alpha scale command allows you to set the desired number of replicas for a specific service. The alpha subcommand indicates that this is a newer feature in Docker Compose.

Make sure you are in the ~/project/my-web-app directory where your docker-compose.yml file is located.

Currently, we have one instance of the web service running. We can verify this by listing the running containers and filtering by the service name.

docker ps --filter "name=my-web-app-web"

You should see only one container listed.

Now, let's scale the web service to 3 instances. We will use the docker compose alpha scale command followed by the service name and the desired number of replicas.

docker compose alpha scale web=3

This command will start two new containers for the web service, bringing the total number of instances to 3.

After running the command, you can again check the running containers to see the scaled instances.

docker ps --filter "name=my-web-app-web"

You should now see three containers listed, each with a different container ID and name (e.g., my-web-app-web-1, my-web-app-web-2, my-web-app-web-3).

The docker compose alpha scale command is a convenient way to dynamically adjust the number of service replicas without modifying the docker-compose.yml file and running docker compose up again.

To scale the service back down, you can use the same command with a smaller number. For example, to scale back to 1 instance:

docker compose alpha scale web=1

This will stop and remove the extra containers, leaving only one instance running.

Let's scale it back to 3 instances for the next step.

docker compose alpha scale web=3

Scale multiple services simultaneously

In this step, we will learn how to scale multiple services simultaneously using the docker compose alpha scale command. This is useful when you have multiple services in your application that need to be scaled together to handle increased load or maintain a balanced architecture.

To demonstrate this, let's add another simple service to our docker-compose.yml file. We will create a simple Nginx service that serves static content.

First, create a directory for the Nginx static content.

mkdir static

Inside the static directory, create a simple HTML file named index.html with the following content:

<!doctype html>
<html>
  <head>
    <title>Static Content</title>
  </head>
  <body>
    <h1>Hello from Nginx!</h1>
  </body>
</html>

Now, let's update the docker-compose.yml file in the ~/project/my-web-app directory to include the Nginx service. Open the docker-compose.yml file using nano.

nano docker-compose.yml

Add the following service definition below the web service:

nginx:
  image: nginx:latest
  ports:
    - "80:80"
  volumes:
    - ./static:/usr/share/nginx/html

The updated docker-compose.yml file should look like this:

version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./static:/usr/share/nginx/html

This adds an nginx service that uses the nginx:latest image, maps port 80 on the host to port 80 in the container, and mounts the ./static directory to the Nginx default web root (/usr/share/nginx/html).

Save the docker-compose.yml file and exit nano (Ctrl+X, Y, Enter).

Now, let's bring up the new service. Since we modified the docker-compose.yml file, we need to run docker compose up again. The -d flag runs the containers in detached mode.

docker compose up -d

Docker Compose will create and start the nginx service. The web service should already be running from the previous step.

You can verify that both services are running using docker ps.

docker ps

You should see containers for both my-web-app-web-1, my-web-app-web-2, my-web-app-web-3 and my-web-app-nginx-1.

Now, let's scale both the web service to 5 instances and the nginx service to 2 instances simultaneously. We can do this by listing the services and their desired replica counts in the docker compose alpha scale command.

docker compose alpha scale web=5 nginx=2

This command will scale the web service to 5 instances and the nginx service to 2 instances.

After running the command, check the running containers again.

docker ps

You should now see 5 containers for the web service and 2 containers for the nginx service.

This demonstrates how you can scale multiple services with a single docker compose alpha scale command, making it efficient for managing the scaling of your application's components.

Scale a service without starting dependencies

In this step, we will explore how to scale a specific service using docker compose alpha scale without starting its dependencies. By default, when you scale a service, Docker Compose might also start or ensure its dependencies are running. However, there are scenarios where you might only want to scale a single service without affecting others.

To illustrate this, let's add a dependency to our web service. We will add a simple database service, although we won't implement any database logic in our web application for this example. This is just to demonstrate the dependency concept.

Open the docker-compose.yml file in the ~/project/my-web-app directory using nano.

nano docker-compose.yml

Add a new service definition for a database (e.g., a simple postgres image) and add a depends_on section to the web service.

version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./static:/usr/share/nginx/html

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

The updated docker-compose.yml file now includes a db service using the postgres:latest image and the web service has a depends_on: - db line, indicating that the web service depends on the db service.

Save the docker-compose.yml file and exit nano (Ctrl+X, Y, Enter).

Now, let's bring up the services with the updated configuration.

docker compose up -d

Docker Compose will now also create and start the db service because the web service depends on it.

You can verify that all services are running using docker ps.

docker ps

You should see containers for web, nginx, and db.

Now, let's say we want to scale only the web service to 4 instances without starting or affecting the db service (if it wasn't already running). The docker compose alpha scale command, by default, respects dependencies. However, for this specific scenario where we want to scale only the specified service, the standard docker compose alpha scale command is sufficient as it primarily focuses on the target service. The depends_on relationship primarily affects the startup order when using docker compose up. When using docker compose alpha scale, it focuses on adjusting the number of instances for the specified service.

Let's scale the web service to 4 instances.

docker compose alpha scale web=4

This command will scale the web service to 4 instances. It will not explicitly start the db service if it wasn't running, but since it was started by the previous docker compose up, it will remain running. The key takeaway here is that docker compose alpha scale is designed to scale the specified service without necessarily triggering the startup of its dependencies if they are not already running.

Verify the number of running web service containers.

docker ps --filter "name=my-web-app-web"

You should see 4 containers for the web service. The db service container should also still be running.

This demonstrates that docker compose alpha scale focuses on the target service for scaling.

Finally, let's bring down all the services.

docker compose down

This command will stop and remove all containers, networks, and volumes created by Docker Compose for this project.

Summary

In this lab, we learned how to prepare a simple Docker Compose project. This involved installing Docker Compose in the LabEx environment, verifying the installation, and then creating a basic Python Flask web application. We also created a Dockerfile to build a Docker image for the application and a requirements.txt file to list the application's dependencies. This initial setup provides the foundation for exploring Docker Compose functionalities, specifically focusing on scaling services in subsequent steps.