为了在 Docker
中使用指定 DNS
,我们需要相应修改 /etc/resolv.conf
。现在遇到的问题是
无法在 Dockerfile 里“永久”覆盖 /etc/resolv.conf;
无论 Classic 还是 BuildKit,Docker 都会在启动阶段把运行时生成的 resolv.conf 挂到容器里,覆盖镜像中原有内容。
因此,我们需要在 build
阶段,把 DNS
「传送」到 docker
里面。下面介绍具体的方法。
使用宿主机
让 builder 使用宿主机网络(简单,但依赖宿主机 DNS
修改宿主机 DNS
这里我们添加了 nameserver 10.32.255.254
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# 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.
nameserver 10.32.255.254
nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 192.168.10.1
|
重新编译 buildx
只要宿主机的 /etc/resolv.conf 正确即可。
1
2
3
4
5
6
7
8
|
docker buildx rm mybuilder 2>/dev/null
docker buildx create \
--name mybuilder \
--driver docker-container \
--driver-opt network=host \
--bootstrap --use
docker buildx build -t myimage .
|