compose icon indicating copy to clipboard operation
compose copied to clipboard

Feature request: set hostname equal to container name

Open awasilyev opened this issue 10 years ago • 12 comments

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?

awasilyev avatar Jun 12 '16 16:06 awasilyev

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.

antonymayi avatar Nov 29 '16 18:11 antonymayi

+1

ayen-tibco avatar Jan 20 '17 13:01 ayen-tibco

Can you not achieve this right now by using the same name for both --hostname and --name options?

haridsv avatar Apr 07 '17 13:04 haridsv

If we use docker-compose, and scale up an app, it creates problem if we set the hostname.

jaydipdave avatar Jul 25 '18 15:07 jaydipdave

@jaydipdave what kinda prob?

hoozecn avatar Aug 17 '18 11:08 hoozecn

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 avatar Aug 17 '18 14:08 gaoweisgp

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

shyamsriram avatar Feb 26 '19 21:02 shyamsriram

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.

stale[bot] avatar Oct 09 '19 19:10 stale[bot]

This issue has been automatically closed because it had not recent activity during the stale period.

stale[bot] avatar Oct 16 '19 20:10 stale[bot]

Is there a reason why this ticket is closed?

Chealer avatar Feb 05 '21 18:02 Chealer

or a way to get the scale suffix index, to achieve a meaningful and non-bugly hostname.

yurenchen000 avatar May 04 '23 20:05 yurenchen000

Would really like to have a way to set container hostname to the container name, or dynamically via the scale index.

jamest-pin avatar Aug 22 '23 03:08 jamest-pin