首页 > 解决方案 > 为什么我的 RUN 对构建的 docker 镜像没有影响?

问题描述

这是简单的 Dockerfile

FROM phusion/baseimage:0.9.19
RUN echo nameserver 10.0.0.1 > /etc/resolv.conf
RUN cat /etc/resolv.conf 

我给出这个命令:

docker build .

我得到这个输出:

Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM phusion/baseimage:0.9.19
 ---> c39664f3d4e5
Step 2/3 : RUN echo nameserver 193.205.160.3 > /etc/resolv.conf
 ---> Running in ffa4ed664323
Removing intermediate container ffa4ed664323
 ---> 91d2456bb9d7
Step 3/3 : RUN cat /etc/resolv.conf
 ---> Running in cd6463980f69
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

# No DNS servers known.

nameserver 8.8.8.8
nameserver 8.8.4.4
Removing intermediate container cd6463980f69
 ---> 089e36d3d140
Successfully built 089e36d3d140

显然,该文件不包含我期望的文本。

对不起,我是 Docker 新手。有人知道我在做什么错吗?谢谢你。

我正在运行 Ubuntu 18.04,并使用 snap 安装了 docker。

标签: dockerubuntudns

解决方案


/etc/resolv.conf文件由 Docker 管理,并被注入到它启动的每个容器中。每个 RUN 行都在一个新容器中执行。因此,在第二个 RUN 行期间,创建了一个新容器,并将 /etc/resolv.conf 的新副本注入到容器中。要在衍生容器中管理 DNS,有几个选项:

  1. docker run --dns 10.0.0.1 your_image将 DNS 条目添加到单个容器
  2. 添加{ "dns": ["10.0.0.1"] }/etc/docker/daemon.json重新启动 docker 引擎(通常使用systemctl restart docker)将为所有 docker 容器设置新的默认值,并且可以使用上述选项覆盖。
  3. 更新主机/etc/resolv.conf将影响它启动的容器,假设上述任何一项尚未完成。

免责声明,第二个和第三个选项适用于安装在主机上的 docker,基于 snap 的安装可能会影响它们。


推荐阅读