Docker – How to change docker host name after the image creation

docker

I'm learning docker 1.8. I wanted to change the hostname of the container after I create them. I tried to edit /etc/host inside the container and got overwritten every time I attach then start the image. I tried to edit the following file, and the hostname gets overwritten.

[root@localhost ~]# docker inspect high_blackwell | grep hostname
"HostnamePath": "/var/lib/docker/containers/15b84f7012383b1af2d4c8c6443506cdff3b19cb1d87d3dfdcb10df126c4eec3/hostname",

[root@localhost ~]#

None of these answers worked for me

How do you name a docker container?

Best Answer

I am not sure if you mean you want to set the hostname of a new container or of a running container. You say you want to do it after creating the container, but then you also say you're "starting the image". If you haven't started the image you haven't yet created the container -- a container is what you get when you start an image.

You're creating a new container

You can set the hostname on the command line:

docker run --rm -h "example.com" -t -i ubuntu bash
# ...
root@example:/# hostname
example.com

Your container is already running

This is more difficult. You'll want to keep an eye on this Docker issue but until it's resolved you can't do much more than to edit /etc/hosts I think. The hostname command won't work.