Juan Leni
Juan Leni

Reputation: 7588

docker-compose scale with separate volumes

I would like to use scale but I need different volumes and port mapping for each node.

How can I do that? Ideally I would need some kind of environment variable or a script that is run to allocate a volume and ports for each new instance.

What is the recommended way to do this?

Upvotes: 2

Views: 2318

Answers (1)

Juan Leni
Juan Leni

Reputation: 7588

I am aware that this might not be a perfect solution but if you don't mind sharing data between nodes.. it does work well. I am using this for local tests so it is safe in my case.

Docker-compose.yml

...
volumes:
   - /var/run/docker.sock:/var/run/docker.sock
...

Dockerfile

...
RUN pip3 install docker
...

In each node, I deploy the following script get_name.py

from docker import Client
import os

hostname = os.environ['HOSTNAME']

cli = Client(base_url='unix://var/run/docker.sock')
data = cli.containers()

for d in data:
    id = d['Id'][:12]
    names = d['Names']
    if id == hostname:
        print(names[0])
        quit()

print(hostname)

And when the node starts (start.sh), it queries its name and creates a symlink to the corresponding subdirectory:

...
NODE_NAME=$(python /root/scripts/get_name.py)
OWN_VOLUME_NAME="/shared_volumes${NODE_NAME}"
ln -s ${OWN_VOLUME_NAME} /data
...

Upvotes: 1

Related Questions