Feature request: set hostname equal to container name
It will be very useful to have a flag enabling setting hostname equal to container name. Here is my case: I am trying to launch rabbitmq cluster with scaling and overlay multihost network. For successful cluster startup each node should be able to ping each other by hostname. When I using compose scale - each node get id-based hostname, but this hostname is not resolvable by other nodes. And I can't find a way to get container name inside container, to set hostname during startup process.
something like if ('hostname' not in container_options): container_options['hostname'] = container_options['name'] in the _get_container_create_options helps me.
Or maybe any other, easier way?
I think this is fundamental for systems that are picky about DNS. HBase for example is painful if hostname and DNS records differ and compose currently can't make these two consistent. This req is a solution.
+1
Can you not achieve this right now by using the same name for both --hostname and --name options?
If we use docker-compose, and scale up an app, it creates problem if we set the hostname.
@jaydipdave what kinda prob?
Thanks for my smart colleague Matt Phillips with the idea of socket. Here is my work around, with docker-compose. Basically, still leave the real docker container name to be a random value created by docker. But we can use the container ID created from docker-compose YAML file to set PS1, or any other things you like. We use that for saltstack minion_id, for example.
It uses the docker properties associated with the container, available from socket pull, from a running container (docker-compose helps me to mount the socket volume /var/run/docker.sock from base host):
container_name=$(curl -s -XGET --unix-socket /var/run/docker.sock -H "Content-Type: application/json" http://v1.24/containers/$(hostname)/json | jq -r .Name[1:])
So you could create a bash script with this in it, like
id_update.sh: container_name=$(curl -s -XGET --unix-socket /var/run/docker.sock -H "Content-Type: application/json" http://v1.24/containers/$(hostname)/json | jq -r .Name[1:]) echo ${container_name} > /etc/salt/minion_id echo "export PS1="root@${container_name}(\$PWD)# "" >> ~root/.bashrc
Then call this script inside Dockerfile. Ha, I combined this together with the solution for using systemd to start docker container:
Dockerfile: ... # some file assets COPY <base_dir>/minion.sh /var/tmp/id_update.sh ... RUN chmod 755 /sbin/service /etc/init.d/salt-minion /etc/salt/minion.sh ... ENTRYPOINT ["/bin/bash", "/var/tmp/id_update.sh"] CMD ["/usr/lib/systemd/systemd"] #====END of Dockerfile=======
docker build -t test_id_systemd .
docker-compose.yml: version: '2.4' ... services: host_a: image: test_id_systemd:latest tmpfs: #<== these 3 lines are for systemd - /tmp #<== these 3 lines are for systemd - /run #<== these 3 lines are for systemd volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro #<== this line is for systemd - /var/run/docker.sock:/var/run/docker.sock #<== this is for the container ID query
Now we are ready to start up containers with docker-compose
docker-compose -f docker-compose.yml up -d
Log in and check:
docker exec -ti <container_name> bash
hostname. #<== still a random one, but your prompt should be the container name now
I have tried this with 3 tired setup with multiple servers and multiple clients. Hope this will be helpful.
You are a real life Saver!!! This is exactly what I was looking for. Thank you so muc
Thanks for my smart colleague Matt Phillips with the idea of socket. Here is my work around, with docker-compose. Basically, still leave the real docker container name to be a random value created by docker. But we can use the container ID created from docker-compose YAML file to set PS1, or any other things you like. We use that for saltstack minion_id, for example.
It uses the docker properties associated with the container, available from socket pull, from a running container (docker-compose helps me to mount the socket volume /var/run/docker.sock from base host):
container_name=$(curl -s -XGET --unix-socket /var/run/docker.sock -H "Content-Type: application/json" http://v1.24/containers/$(hostname)/json | jq -r .Name[1:])
So you could create a bash script with this in it, like
id_update.sh: container_name=$(curl -s -XGET --unix-socket /var/run/docker.sock -H "Content-Type: application/json" http://v1.24/containers/$(hostname)/json | jq -r .Name[1:]) echo ${container_name} > /etc/salt/minion_id echo "export PS1="root@${container_name}($PWD)# "" >> ~root/.bashrc
Then call this script inside Dockerfile. Ha, I combined this together with the solution for using systemd to start docker container:
Dockerfile: ...
some file assets
COPY <base_dir>/minion.sh /var/tmp/id_update.sh ... RUN chmod 755 /sbin/service /etc/init.d/salt-minion /etc/salt/minion.sh ... ENTRYPOINT ["/bin/bash", "/var/tmp/id_update.sh"] CMD ["/usr/lib/systemd/systemd"] #====END of Dockerfile=======
docker build -t test_id_systemd .
docker-compose.yml: version: '2.4' ... services: host_a: image: test_id_systemd:latest tmpfs: #<== these 3 lines are for systemd
- /tmp #<== these 3 lines are for systemd
- /run #<== these 3 lines are for systemd volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro #<== this line is for systemd
- /var/run/docker.sock:/var/run/docker.sock #<== this is for the container ID query
Now we are ready to start up containers with docker-compose
docker-compose -f docker-compose.yml up -d
Log in and check:
docker exec -ti <container_name> bash
hostname. #<== still a random one, but your prompt should be the container name now
I have tried this with 3 tired setup with multiple servers and multiple clients. Hope this will be helpful.
gaoweisgp, You are a Life Saver !!! This is exactly what I was looking for. Thank you sooo much!!!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically closed because it had not recent activity during the stale period.
Is there a reason why this ticket is closed?
or a way to get the scale suffix index, to achieve a meaningful and non-bugly hostname.
Would really like to have a way to set container hostname to the container name, or dynamically via the scale index.