11

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?

Ahmed
  • 221
  • 1
  • 2
  • 7

3 Answers3

17

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.

  • Yeah, using commend run is creating a new container. I thought my data are saved every time I exit the container and run it again. – Ahmed Aug 28 '15 at 16:55
  • In `docker-compose.yml` there is the [`hostname` option](https://docs.docker.com/compose/compose-file/compose-file-v3/#domainname-hostname-ipc-mac_address-privileged-read_only-shm_size-stdin_open-tty-user-working_dir) as well. – x-yuri Sep 17 '21 at 01:57
11

To change the hostname of a running container, you can use the "nsenter" command. You will have to be root on the host, though.

We can list the namespaces on the host with the "lsns" command:

# lsns
        NS TYPE  NPROCS   PID USER COMMAND
4026531836 pid       73     1 root init      
4026531837 user     101     1 root init      
4026531838 uts       73     1 root init      
4026531839 ipc       73     1 root init      
4026531840 mnt       72     1 root init      
4026531857 mnt        1    14 root kdevtmpfs
4026531957 net       73     1 root init      
4026532300 mnt       28  1785 root /usr/bin/python /usr/local/bin/supervisord -c
4026532301 uts       28  1785 root /usr/bin/python /usr/local/bin/supervisord -c
4026532302 ipc       28  1785 root /usr/bin/python /usr/local/bin/supervisord -c
4026532303 pid       28  1785 root /usr/bin/python /usr/local/bin/supervisord -c
4026532305 net       28  1785 root /usr/bin/python /usr/local/bin/supervisord -c

The ones with pid 1785 are my docker container. The namespace type that handles hostnames is "uts", so let's run hostname in that namespace:

# nsenter --target 1785 --uts hostname foo

Now "hostname" in your container should yield "foo"!

vasi
  • 211
  • 2
  • 4
  • excelent, i believe that this command can do so many other things like kernel parameters... – Marcelo Guedes Jul 22 '20 at 00:36
  • Works inside a container as well. I have a linux container with that runs the init process to support multiple services. Running the command "sudo nsenter --target 1 --uts hostname " from inside the container did the trick. – iansari Apr 23 '21 at 18:47
1

1.Stop container and service

sudo docker stop CONTAINER_NAME
sudo service docker stop

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

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

Replace

    "Hostname":"WHATEVER"

with

    "Hostname":"NEW_HOSTNAME"

3.Start container and service

sudo service docker start
sudo docker start CONTAINER_NAME
Archemar
  • 1,369
  • 11
  • 19
NamNC
  • 11
  • 1