How to specify hostname for the running container?
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
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
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.
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.