How to scale Docker Containers with Docker-Compose
Learn how to scale Docker containers efficiently using Docker Compose. This guide covers scaling services, managing stateful apps, and monitoring scaled containers.
I've been working on a few projects at the moment, and scaling keeps coming up. So, let's take a look at scaling a Docker web services stack with Docker-Compose
We are building a web service with three components. Before we get started, we need to lay a bit of groundwork.
- HAProxy, which provides us with round-robin load balancing
- Web Application based on Python
Redis Databasethem to the single web service, which prints the hostname of the container that receives the request, where a counter is incremented. This
The HAProxy receives the web requests and routes them to the single web service, which prints the hostname of the container that receives the request, where a counter is incremented. This value is then stored in the Redis Database.
Now, let's scale our stack out to 5 web instances/containers that are load-balanced and still connected to the Redis DB. Docker-compose easily scales our service from 1 to 5 instances. Once the web services scale out to 5, we need to inform the Database and HAProxy of these changes so they can accept and route traffic accordingly.
As before, the web requests are received by the HAProxy but are now routed round-robin to all 5 of the web service instances. All these instances are also linked to the Redis DB, and thus, our application prints the hostname and increments the number of visits flawlessly while printing the hostname from each container that responds. We can also scale the web service back to 1 container when we are done.
DEMO - How to scale Docker Containers with Docker-Compose
You can find the contents of this demo on my Github/vegasbrianc page
Install
The instructions assume that you have already installed Docker and Docker Compose.
In order to get started, create a new directory on your Docker Host and clone this project to the newly created directory.
Please note that the web services will inherit the name from the directory you create. If you create a folder named test. Then the services will all be named test-web, test-redis, test-lb.
Also, when you scale your services it will then tack on a number to the end of the service you scale.
git clone https://github.com/vegasbrianc/docker-compose-demo.git .
How to get up and running
Once you've cloned the project to your host, we can start our demo project. Easy! Navigate to the directory in which you cloned the project. Run the following commands from this directory.
docker-compose up -d
The docker-compose command will pull the images from Docker Hub and link them together based on the information inside the docker-compose.yml file. This will create ports and links between containers and configure applications as required. After the command completes, we can now view the status of our stack.
docker-compose ps
Verify our service is running by either curling the IP from the command line or viewing the IP from a web browser. You will notice that each time you run the command, the number of times seen is stored in the Redis Database, which increments. The hostname is also reported.
Curling from the command line
curl 0.0.0.0
Hello World!
I have been seen 1 times.
My Host name is 29c69c89417c
Docker Compose Scaling
Now comes the fun part of Docker Compose, which is scaling. Using Docker Compose, let's scale our web service from 1 to 5 instances.
docker-compose scale web=5
We have now scaled our web service container. We should now run an update on our stack so the load balancer and Redis are informed about the new web service containers.
Now run our curl command again on our web services and we will now see the number of visits increase and the hostname change. To get a deeper understanding tail the logs of the stack to watch what happens each time you access your web services with curl.
docker-compose logs
Here's the output from my docker-compose logs after I curled my application 5 times so it is clear that the round-robin is sent to all 5 web service containers.
web_5 | 172.17.1.140 - - [04/Sep/2015 14:11:34] "GET / HTTP/1.1" 200 -
web_1 | 172.17.1.140 - - [04/Sep/2015 14:11:43] "GET / HTTP/1.1" 200 -
web_2 | 172.17.1.140 - - [04/Sep/2015 14:11:46] "GET / HTTP/1.1" 200 -
web_3 | 172.17.1.140 - - [04/Sep/2015 14:11:48] "GET / HTTP/1.1" 200 -
web_4 | 172.17.1.140 - - [04/Sep/2015 14:14:19] "GET / HTTP/1.1" 200 -
FAQ Section: How to Scale a Docker Container with Docker Compose
What is Docker Compose and how does it help in scaling Docker containers?
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It simplifies the process of scaling containers by enabling you to specify the number of instances (or replicas) of a service in a single configuration file (docker-compose.yml
). This makes it easy to manage and scale your application across multiple containers with just a few commands.
How do you scale a Docker service using Docker Compose?
To scale a Docker service using Docker Compose, you use the docker-compose up --scale
command followed by the service name and the desired number of instances. For example, docker-compose up --scale web=3
will start three instances of the "web" service as defined in your docker-compose.yml
file.
Can you scale stateful applications with Docker Compose?
While Docker Compose is excellent for scaling stateless applications, scaling stateful applications can be more challenging. Stateful applications often require persistent storage, which isn't automatically managed by Docker Compose. You'll need to configure external storage solutions or use Docker volumes to ensure that state is maintained across scaled instances.
What are the benefits of scaling containers with Docker Compose?
Scaling containers with Docker Compose offers several benefits, including easier management of multi-container applications, efficient use of resources by running multiple instances of a service, and the ability to handle increased traffic or workloads by quickly scaling up services. It also simplifies orchestration in development and production environments.
How do you manage and monitor scaled Docker containers?
After scaling Docker containers with Docker Compose, you can manage and monitor them using various tools like Docker CLI commands (docker ps
, docker logs
), or more advanced monitoring tools like Prometheus, Grafana, or Docker Swarm. These tools help you keep track of the performance and health of your containers, ensuring that your application runs smoothly at scale.
Follow me
If you liked this article, Follow Me on Twitter to stay updated!