【问题标题】:After Docker Commit my changes are not saving to Image在 Docker Commit 之后,我的更改没有保存到 Image
【发布时间】:2020-08-21 04:20:20
【问题描述】:

我的Docker Version - 19.03.8, build afacb8b

我已经从DockerHub 中提取了ubuntu:18.04。然后按照以下步骤将两个新行添加到我的 docker 映像的 /etc/hosts 文件中。

docker images
docker run --name ubuntu-18-1 -idt 8b353a2e5d1b /bin/bash
docker ps
# Executed the Container
docker exec -it 985ae774a352 /bin/bash
root@985ae774a352:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      985ae774a352

在我的容器的/etc/hosts 文件中添加以下两行(56.57.58.59 example1.com56.57.58.60 example2.com)后,保存后退出,最后我提交了我的容器。

docker commit 985ae774a352 ubuntu-18-2
# Even after commit i can able to view the changes i made.
docker exec 985ae774a352 cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      985ae774a352
56.57.58.59 example1.com
56.57.58.60 example2.com

# Stopped & Removed the Container
docker container stop 985ae774a352
docker container rm 985ae774a352

# Launched the Container with udpated Image:-
docker run --name ubuntu-18-2 -idt 0ebc2d94a384 /bin/bash
docker ps
docker exec a8c1fa1dd65f cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      a8c1fa1dd65f

所以在新图像中我看不到我在/etc/hosts 文件中所做的更改。如果上述步骤有问题,请纠正我。

【问题讨论】:

  • 你几乎不想使用docker commit;使用docker build 从 Dockerfile 构建映像是更好的做法,并且您将写下当一年内基础映像中存在必须修复的关键安全问题时如何重建映像。同样,设置 DNS 系统(如 BIND、DNSMasq 或像 Consul 这样的服务发现系统)将比尝试在多个位置复制 /etc/hosts 文件更易于维护。

标签: docker


【解决方案1】:

问题是每次执行docker run时都会重新生成/etc/hosts。

要添加新主机,您可以使用选项--add-host。例如

docker run \
    --name ubuntu-18-1 \
    --add-host example1.com:56.57.58.59 \
    --add-host example2.com:56.57.58.60 \
    -idt ubuntu:18.04 \
    /bin/bash

更多信息可以在 docker 文档的 Managing /etc/hosts 部分找到。

【讨论】:

  • 感谢 Adii 和 Stefano 的快速支持。我已经改变了逻辑,它现在可以工作了。谢谢
  • 只是另一个注意事项,您也可以在 docker run 中使用标签代替 id。例如docker run -it --rm ubuntu:18.04 bash 等同于 docker run -it --rm 8b353a2e5d1b bash
【解决方案2】:

你不能提交更改到/etc/hosts,因为当你启动容器时docker维护/etc/hosts,你可能会在启动容器时注意到一件事,docker generate /etc/host

例如

::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  c71f7cfa8140

您可以查看/etc/hosts 的底部,您可以看到最后一个条目或文件中的某处您会看到正在运行的容器ID,您可以交叉检查

docker ps

所以你应该改变你的逻辑,比如here,或者更好地使用--add-host

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2011-12-04
    • 2021-01-03
    • 2017-09-23
    • 1970-01-01
    • 2017-07-15
    • 2015-12-04
    相关资源
    最近更新 更多