I'm having trouble setting the hostname on a running docker container. I'm also having trouble understanding how to specify hostname after the image is started.

I started a container from an image I downloaded:

sudo docker run -p 8080:80 -p 2222:22 oskarhane/docker-wordpress-nginx-ss

But I forgot to specify hostname through -h; how can I specify the hostname now that the container is running?

  • Can you just stop the container and start it again supplying a hostname? – dawud Apr 20 '14 at 9:56
  • AFAIK that only works when you run an image, not when you start a container. – Miguel Ping Apr 20 '14 at 11:30
  • Sometimes all you want is to see the bash command prompt with a proper name. If that is the case, you can edit the PS1 value in .bashrc. Look at askubuntu.com/a/549150/55365 for suggestions. In any PS1 you can add a (colored) string surrounded by * or # to set it apart, with the name of the server or service. – SPRBRN Jun 1 '17 at 17:14
  • @dawud you can not give a new hostname when runing docker restart documentation – blueFast Mar 2 '18 at 11:12

Edit /etc/hostname is one thing for which you need ssh access inside the container. Otherwise, you can spin up the container with -h option.

To set the host and domain names:

$ docker run -h foo.bar.baz -i -t ubuntu bash
root@foo:/# hostname
root@foo:/# hostname -d
root@foo:/# hostname -f
  • 6
    You can docker exec /bin/bash on a running container, no need to install ssh (which is imho a bad practice for administration purposes). – jjmontes Oct 13 '16 at 20:23
  • You may also want to edit /etc/hosts that it is correctly resolved into an address. – dtoubelis Jul 28 '17 at 18:14
  • 5
    You can not change the hostname in a running container with hostname(missing capability). Running docker run -h <hostname> <image> <container-name> creates a new container with the given hostname. – blueFast Mar 2 '18 at 11:10
  • This answer is wrong. You cannot change hostname of an existing container. – Dojo May 25 '20 at 11:49
  • @Dojo, the answer does mention "otherwise" – amitmula May 28 '20 at 9:08

Stop container and service

sudo docker stop CONTAINER_NAME
sudo service docker stop

Edit config file (JSON) [You should make backup first]






Start container and service

sudo service docker start
sudo docker start CONTAINER_NAME

(Optionally you can also attach docker)

sudo docker attach CONTAINER_NAME

Details about dockers (i.e. CONTAINER_NAME, CONTAINER_ID) can be obtained by running

sudo docker ps -a
  • doesn't work. get overwritten everytime I start the container. – Ahmed Aug 25 '15 at 18:08
  • that's strange. i have just reproduced these steps and it still works. docker version 1.7.1. are you sure you did close container and service before you edited the file? – Tomot Aug 27 '15 at 15:56
  • Works on Docker 17.04.0-ce, when editing /etc/hostname didn't. If you haven't created the container yet, the other answer with -h is better, but in my case I had already gone to the trouble of setting up the container and didn't want to recreate it just to set the hostname. – Una Apr 20 '17 at 19:56
  • Doesn't work Docker version 17.05.0-ce, build 89658be – blueFast Mar 2 '18 at 11:01

In case you use --net=host then you can't change the hostname from -h or from inside the docker.

See https://github.com/docker/docker/issues/5708


Restarting the container would be the easiest option - but you may also edit /etc/hostname and go from there.

  • 1
    Editting /etc/hostname has no effect after restart, since it is overwritten each time with the "real" hostname, managed by docker. You can not run hostname on the container, since the container has no capabilities for that operation (by default, and you do not want to change that) – blueFast Mar 2 '18 at 11:16
  • 1
    And editting /etc/hostname and not restarting the container has no purpose. – blueFast Mar 2 '18 at 11:16

https://evolvingweb.ca/blog/changing-docker-hostnames-namespaces describes a way to do this. Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter. Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace.

  • 2
    please add the solution directly here, and not an external link which may dissappear – Orphans Mar 12 '17 at 9:31

As a few others have pointed out, this can be changed for containers by modifying the config.v2.json file.

Just be sure the Docker service is stopped before the file is edited, else it will be OVERWRITTEN. The steps MUST be done in this order

  • Stop the Container
  • Stop the Docker service
  • Modify the - /var/lib/docker/containers/CONTAINER_ID/config.v2.json - file

  • Start up the Docker service

  • Start the container

This has been confirmed working for me on Docker v17.05.0-ce, on a container using --net=host. Modifying the /etc/hostname file does not work and the file is just overwritten.




find and set

"Config":{"Hostname":"utils","Domainname":"mysite.com", ...}
  • Doesn't work Docker version 17.05.0-ce, build 89658be. Config gets overwritten, who knows from where. And the hostname itself does not change after restart. – blueFast Mar 2 '18 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.