Lab #18: Scale Command

The docker-compose scale <service name> = <no of instances> sets the number of containers to run for a service.

NOTE:The scale command is deprecated, instead Use the up command with the –scale flag.
docker-compose up --scale <service name> = <no of instances>

Pre-requisite:

Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 1 5 min

Pre-requisite

Assignment

Create a docker-compose.yml file

version: '3.1'
services:
   redis-master:
     image: redis:latest 
     container_name: webserver
     restart: unless-stopped
     ports:
       - "6379"
   redis-slave: 
     image: gcr.io/google_samples/gb-redisslave:v1 
     ports: 
       - "6379"
     environment: 
       - GET_HOSTS_FROM=dns
   frontend: 
     image: gcr.io/google-samples/gb-frontend:v3 
     ports: 
       - "80:80" 
     environment: 
       - GET_HOSTS_FROM=dns

Bringing up the containers

$ docker-compose up -d

Checking container status

$ docker-compose ps
       Name                     Command               State            Ports         
-------------------------------------------------------------------------------------
root_frontend_1      apache2-foreground               Up      0.0.0.0:80->80/tcp     
root_redis-slave_1   /entrypoint.sh /bin/sh -c  ...   Up      0.0.0.0:32769->6379/tcp
webserver            docker-entrypoint.sh redis ...   Up      0.0.0.0:32768->6379/tcp  

Listout the services

$ docker-compose ps --services
redis-slave
frontend
redis-master

Scale service

$ocker-compose scale redis-slave=3
Starting root_redis-slave_1 ... done
Creating root_redis-slave_2 ... done
Creating root_redis-slave_3 ... done

Checking container status

$ docker-compose ps
       Name                     Command                State              Ports         
----------------------------------------------------------------------------------------
root_frontend_1      apache2-foreground               Up         0.0.0.0:80->80/tcp                      
root_redis-slave_1   /entrypoint.sh /bin/sh -c  ...   Up         0.0.0.0:32772->6379/tcp
root_redis-slave_2   /entrypoint.sh /bin/sh -c  ...   Up         0.0.0.0:32775->6379/tcp
root_redis-slave_3   /entrypoint.sh /bin/sh -c  ...   Up         0.0.0.0:32776->6379/tcp
webserver            docker-entrypoint.sh redis ...   Up         0.0.0.0:32768->6379/tcp

NOTE: Host machine can only bind an unallocated port to the container, so trying scale service which is mounted to host will fail.

Contributor

Savio Mathew

Next » Lab #19: Exec Command