docker-compose scale with separate volumes

六眼飞鱼酱① 提交于 2020-05-26 09:39:10

问题


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?


回答1:


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
...


来源:https://stackoverflow.com/questions/46077248/docker-compose-scale-with-separate-volumes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!