为了支持一些老的软件方案,我需要绑定(bind)我的容器的hostname
至127.0.0.1
,给我留下这样的东西:
$ hostname
4e84a7ae5f92
$ cat /etc/hosts | grep 127.0.0.1
127.0.0.1 localhost 4e84a7ae5f92
最好的情况是在 Dockerfile 中执行,但由于
docker build
构建一个图像(而不是一个容器),这似乎并不现实。另外,如果我尝试使用
sed
在正在运行的容器中,我最终遇到了一个错误:$ sed -i '/^127\.0\.0\.1.*/ s/$/ '$(hostname)'/' /etc/hosts
sed: cannot rename /etc/sedC5PkA2: Device or resource busy
我能做些什么 ?
最佳答案
docker run
命令有一个名为 --hostname=""
的选项照顾你的/etc/hostname
文件。/etc/hosts
中的主机到 IP 映射可以使用选项 --add-host=[]
管理文件然后。
关于docker - 如何将容器的主机名添加到/etc/hosts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28327458/