Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simply scale a docker-compose service and pass the index and count to each?

I'm looking for a way to scale up a docker-compose service & saw the --scale option but could not find any method to get the index & count within each container.

Here's a simplified compose file:

version: '2.1'
services:
  my_thing:
    restart: always
    build: .
    entrypoint: /entry.sh
    depends_on:
      - database
  database:
    restart: always
    image: postgres:12.1-alpine
    ...

If I did docker-compose up my_thing --scale 5 within the entry I'd like to be able to see which instance I am (1 to 5) & how many instances there are in total (5 in this example).

I need this as the API 'my_thing' connects to needs this information to delegate events to each instance.

For example my entry could be

echo "Hello I'm container $index of $count";

& then when I run docker-compose up my_thing --scale 5 (note that I'd prefer not to hard-code the the count)

Then each container would respectively run the entry & output:

Hello I'm container 1 of 5
Hello I'm container 2 of 5
Hello I'm container 3 of 5
... and so on

If any container crashed I'd like it restarted knowing it's index.

Is this possible or do I need to figure out some alternative or build my own tooling?

Edit: If there's any example of how to do something like this with docker swarm that may help too.

like image 499
user3818491 Avatar asked Mar 01 '20 22:03

user3818491


People also ask

How do I scale a docker compose service?

This can be controlled by assigning port range on the ports section of the compose YAML file. Scaling can also be done by using up command as well with the --scale flag. Alternatively, in Compose file version 3. x, you can also specify replicas under the deploy section as part of a service configuration for Swarm mode.

Can docker compose auto scale?

Docker – more specifically Docker-Compose – provides a solution with auto-scaling groups.

Which command flag used to run docker compose in background?

Run in the Background Run Docker Compose in detached mode by passing the -d flag to docker-compose up .


1 Answers

#!/bin/bash
# get the container IP
IP=`ifconfig eth0 | grep 'inet ' | awk '{print $2}'`

# get the service name you specified in the docker-compose.yml 
# by a reverse DNS lookup on the IP
SERVICE=`dig -x $IP +short | cut -d'_' -f2`

# the number of replicas is equal to the A records 
# associated with the service name
COUNT=`dig $SERVICE +short | wc -l`

# extract the replica number from the same PTR entry
INDEX=`dig -x $IP +short | sed 's/.*_\([0-9]*\)\..*/\1/'`

# Hello I'm container 1 of 5 
echo "Hello I'm container $INDEX of $COUNT"
like image 71
Amir Razmjou Avatar answered Oct 07 '22 18:10

Amir Razmjou