gpt4 book ai didi

docker - 如何在 "docker build"期间更新 Docker 镜像中的/etc/hosts 文件

转载 作者:IT老高 更新时间:2023-10-28 12:33:50 26 4
gpt4 key购买 nike

我想在“docker build”期间更新我的 /etc/hosts 文件。

我在 Dockerfile 中添加了以下行,但它既没有更新 /etc/hosts 文件也没有给出任何错误。

RUN echo "192.168.33.11    mynginx" >> /etc/hosts

我需要更新 /etc/hosts。有人可以就此提出建议吗?

最佳答案

使用更新版本的 docker,这可以通过 docker-compose 及其 extra_hosts directive 来完成。

Add hostname mappings.
Use the same values as the docker run client --add-host parameter (which should already be available for docker 1.8).

extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"

简而言之:在运行时修改容器的 /etc/hosts,而不是在构建时修改。


使用 Docker 17.x+,您有一个 docker build --add-host提到below , 但是,正如 issue 34078 中所评论的那样在 this answer :

The --add-host feature during build is designed to allow overriding a host during build, but not to persist that configuration in the image.

提到的解决方案确实引用了我上面建议的 docker-compose:

  • Run an internal DNS; you can set the default DNS server to use in the daemon; that way every container started will automatically use the configured DNS by default
  • 使用 docker compose 并向您的开发人员提供 docker-compose.yml
    docker compose 文件允许您指定启动容器时应使用的所有选项,因此开发人员只需 docker compose up 即可使用他们需要设置的所有选项来启动容器。

关于docker - 如何在 "docker build"期间更新 Docker 镜像中的/etc/hosts 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38302867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com