docker,

How to scale docker containers

Jan 17, 2022 · 1 min read · Post a comment

When it comes working with a master - replica services Docker offers a scaling compose command which is very useful. Here I’m going to show you how to use it with an example.

Prerequisites

  • Docker
  • Docker Compose

Docker Scale Example

Let say that we have a compose file that holds a Redis service with a master and replica. Instead of coping the same configuration, let’s see how can you use scale command.

Here is an example of the Compose file:

version: '3'

services:
  redis-master:
    image: redis:alpine
    ports:
      - "6379:6379"
    env_file: 
      - .env
    volumes:
      - redis_data:/data
 ...

  redis-replica:
    image: redis:alpine
    ports:
      - 6379
    env_file: 
      - .env
    depends_on:
      - redis-master
 ...

Now if you want to have 1 master container and 3 replicas use the following command:

docker-compose up  --scale redis-master=1 --scale redis-replica=3

It will start one container from the master and three from the replica.

Conclusion

The scale command is very useful as you can replicate the same scenario that you are using on your server and see its behavior. Taking Redis as a service was just an example, feel free to use the scale command to any service you want to deploy.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.