如何在“docker build"期间更新 Docker 镜像中的/etc/hosts 文件 [英] How to update /etc/hosts file in Docker image during "docker build"
问题描述
我想在docker build"期间更新我的 /etc/hosts
文件.
I want to update my /etc/hosts
file during "docker build".
我在 Dockerfile 中添加了以下行,但它既没有更新 /etc/hosts
文件,也没有给出任何错误.
I added below line in Dockerfile but it's neither updating /etc/hosts
file nor giving any error.
RUN echo "192.168.33.11 mynginx" >> /etc/hosts
我需要更新/etc/hosts
.有人可以对此提出建议吗?
I need to update /etc/hosts
. Can anyone suggest on this?
推荐答案
有了更新版本的 docker,这可以通过 docker-compose 和它的 extra_hosts
指令
With a more recent version of docker, this could be done with docker-compose and its extra_hosts
directive
添加主机名映射.
使用与 docker run
客户端 --add-host
参数(应该已经可用于 docker 1.8).
Add hostname mappings.
Use the same values as thedocker 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
,而不是在构建容器时修改.
In short: modify /etc/hosts
of your container when running it, instead of when building it.
使用 Docker 17.x+,你有一个 docker build --add-host
提到了下面,但是,正如issue 34078 和这个答案:
With Docker 17.x+, you have a docker build --add-host
mentioned below, but, as commented in issue 34078 and in this answer:
构建期间的 --add-host
功能旨在允许在构建期间覆盖主机,但不会将该配置保留在映像中.
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:
The solutions mentioned do refer the docker-compose I was suggesting above:
- 运行内部 DNS;您可以设置要在守护程序中使用的默认 DNS 服务器;这样每个启动的容器都会默认自动使用配置的 DNS
- 使用 docker compose 并向您的开发人员提供
docker-compose.yml
.
docker compose 文件允许您指定启动容器时应使用的所有选项,因此开发人员只需 docker compose up 即可使用他们需要设置的所有选项启动容器.
这篇关于如何在“docker build"期间更新 Docker 镜像中的/etc/hosts 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!