When I use docker-compose scale I would like to have an instance id used in the yml file.
docker-compose.yml:
myservice:
volumes:
- ./volumes/foo{AGENT_ID}/bar:/foo/bar
I have seen a lot of discussions around this topic but I still have not found a clear answer.
Is there an environment variable or similar that I can use this way? It would be ideal if I could use that in bash scripts, etc.
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With