70

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?

kenorb
  • 5,943
  • 1
  • 44
  • 53
Miguel Ping
  • 825
  • 1
  • 6
  • 9
  • Can you just stop the container and start it again supplying a hostname? – dawud Apr 20 '14 at 09: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 https://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 01 '17 at 17:14
  • @dawud you can not give a new hostname when runing `docker restart` [documentation](https://docs.docker.com/engine/reference/commandline/restart/) – blueFast Mar 02 '18 at 11:12
  • Related: [How to handle specific hostname like -h option in Dockerfile](https://stackoverflow.com/q/28898787/55075). – kenorb Jan 30 '19 at 00:15

7 Answers7

65

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
foo
root@foo:/# hostname -d
bar.baz
root@foo:/# hostname -f
foo.bar.baz
Læti
  • 2,075
  • 21
  • 33
amitmula
  • 790
  • 6
  • 2
  • 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
  • 7
    You can not change the hostname in a running container with `hostname`(missing capability). Running `docker run -h ` creates a *new* container with the given hostname. – blueFast Mar 02 '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 09:08
8

Stop container and service

sudo docker stop CONTAINER_NAME
sudo service docker stop

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

/var/lib/docker/containers/CONTAINER_ID/config.json

Replace

"Hostname":"WHATEVER"

with

"Hostname":"NEW_HOSTNAME"

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
Tomot
  • 409
  • 4
  • 5
  • 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 02 '18 at 11:01
7

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

chicks
  • 3,639
  • 10
  • 26
  • 36
Ivailo Bardarov
  • 179
  • 1
  • 4
5

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

James V
  • 67
  • 1
  • 3
  • 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 02 '18 at 11:16
  • 1
    And editting `/etc/hostname` and not restarting the container has no purpose. – blueFast Mar 02 '18 at 11:16
1

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.

Chaim Geretz
  • 131
  • 3
0

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.

-3

In

 /var/lib/docker/containers/CONTAINER/config.json

find and set

"Config":{"Hostname":"utils","Domainname":"mysite.com", ...}
Oleg Neumyvakin
  • 599
  • 4
  • 15
  • 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 02 '18 at 11:02