如何在“ docker build”期间更新Docker映像中的/ etc / hosts文件。 [英] How to update /etc/hosts file in Docker image during "docker build"

查看:144
本文介绍了如何在“ docker build”期间更新Docker映像中的/ etc / hosts文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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及其< a href = https://docs.docker.com/compose/compose-file/#extra_hosts rel = nofollow noreferrer> extra_hosts 指令

With a more recent version of docker, this could be done with docker-compose and its extra_hosts directive


添加主机名映射。

使用与 docker run 客户端-添加主机参数(对于docker 1.8应该已经可用)。

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

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 和< a href = https://stackoverflow.com/a/53723940/6309>此答案:

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组成他们需要设置的所有选项的容器。

  • 这篇关于如何在“ docker build”期间更新Docker映像中的/ etc / hosts文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆