docker build alpine dns error

使用 alpine 构建 Ddockerfile ,导致 docker 打包失败,错误如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fetch https://mirrors.aliyun.com/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
v3.13.4-69-g5bcff43ec5 [https://mirrors.aliyun.com/alpine/v3.13/main]
v3.13.4-66-g32aee0eba0 [https://mirrors.aliyun.com/alpine/v3.13/community]
OK: 13892 distinct packages available
fetch https://mirrors.aliyun.com/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.13/main: DNS lookup error
fetch https://mirrors.aliyun.com/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.13/community: DNS lookup error
ERROR: unable to select packages:
bash (no such package):
required by: world[bash]
curl (no such package):
required by: world[curl]
ERROR: Service 'microservice-users-rpc' failed to build : The command '/bin/sh -c apk update && apk add --no-cache curl bash' returned a non-zero code: 2
make: *** [docker-compose-up] Error 1

用于打包的 Dockerfile 非常的简单,这里去掉了其他无关的内容。

1
2
3
4
FROM alpine:latest
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update

Dockerfile 使用了 alpine 的镜像,将源替换为了阿里的源,最后执行 apk update 更新。

在执行 api update 时出错。

这里报错提示为 DNS lookup error .

在 docker 文档 [https://docs.docker.com/config/containers/container-networking/](https://docs.docker.com/config/containers/container-networking/)中关于dns的描述

The IP address of a DNS server. To specify multiple DNS servers, use multiple –dns flags. If the container cannot reach any of the IP addresses you specify, Google’s public DNS server 8.8.8.8 is added, so that your container can resolve internet domains.

也就是说,容器在 build 的时候可能会使用 8.8.8.8 这个地址作为 dns 服务。而google 的 dns 服务在国内基本上都是处于不可用的状态。

解决方法

/etc/docker/daemon.json 配置文件中指定 dns 配置,比如

1
2
"dns":["223.5.5.5"]

作者

张巍

发布于

2021-04-15

更新于

2021-04-15

许可协议

评论