docker-compose规模独立的卷 [英] docker-compose scale with separate volumes
问题描述
我想使用scale,但是每个节点需要不同的卷和端口映射。
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.
推荐的方法是什么?
推荐答案
我知道这可能不是一个完美的解决方案,但是如果您不介意在节点之间共享数据的话。做得很好。我正在使用它进行本地测试,因此对于我来说是安全的。
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
Docker-compose.yml
...
volumes:
- /var/run/docker.sock:/var/run/docker.sock
...
Dockerfile
Dockerfile
...
RUN pip3 install docker
...
节点,我从docker import部署以下脚本 get_name.py
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)
当节点启动时( start.sh
),它会查询它的名称并创建到相应子目录的符号链接:
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
...
这篇关于docker-compose规模独立的卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!