大网数据广告活动
服务器运维问答

How do I change docker host name after the image creation?

· 52CE 我爱测

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 follo…

问题原文

原帖链接 · Ahmed · CC BY-SA 4.0

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?

Note: This question was asked due to a lack of understanding of how docker works. I assumed Docker acted like a VM when I asked this question.

社区高分解答

原帖链接 · vasi · CC BY-SA 3.0

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"!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-08-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。