解决 Docker 容器在构建阶段无法解析域名

Dockerfile.rabbitmq

FROM rabbitmq:3.13-management

# 安装 wget 用于下载插件
RUN apt-get update && apt-get install -y wget

# 下载 rabbitmq_delayed_message_exchange 插件
RUN wget -P /plugins https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.13.0/rabbitmq_delayed_message_exchange-3.13.0.ez

# 启用插件
RUN rabbitmq-plugins enable --offline rabbitmq_delayed_message_exchange

使用docker compose up --build -d构建报错:

mth@vm001:~/workspace/payment-service$ docker compose up --build -d
Compose now can delegate build to bake for better performances
Just set COMPOSE_BAKE=true
[+] Building 274.6s (5/7)                                                                                           docker:default
 => [rabbitmq internal] load build definition from Dockerfile.rabbitmq                                                        0.0s
 => => transferring dockerfile: 450B                                                                                          0.0s
 => [rabbitmq internal] load metadata for docker.io/library/rabbitmq:3.13-management                                         14.8s
 => [rabbitmq internal] load .dockerignore                                                                                    0.0s
 => => transferring context: 2B                                                                                               0.0s
 => [rabbitmq 1/4] FROM docker.io/library/rabbitmq:3.13-management@sha256:cb5f2b8953eeb4ba986b9009ab2fec270d1c2d2999af3f4f2  38.4s
 => => resolve docker.io/library/rabbitmq:3.13-management@sha256:cb5f2b8953eeb4ba986b9009ab2fec270d1c2d2999af3f4f2ced3fb86d1  0.0s
 => => sha256:ca5c52c7bb7993d30a5336f3200e141844f1c0e69148594f93bc42e3dbbe0c69 2.89kB / 2.89kB                                0.0s
 => => sha256:5dbd81dc19b24758fac36b164d830b2a7b50393e6f8a431136bbcbc7620068fc 7.47MB / 7.47MB                               16.1s
 => => sha256:feca6e9fb024afed08de9008506928503cd4ae7fdaf50c7a77961a98e8b4cbc5 9.50kB / 9.50kB                                4.9s
 => => sha256:cb5f2b8953eeb4ba986b9009ab2fec270d1c2d2999af3f4f2ced3fb86d12661e 7.83kB / 7.83kB                                0.0s
 => => sha256:08afdf6b4533c28d03e87ec5c948325aacfb8ec56d367ffc94d2487af8150874 9.50kB / 9.50kB                                0.0s
 => => sha256:ec13ceee25cbb2b3985e8d542c4ed7b50fe306dbe5d837ae2550fdf0e0c62d2e 43.47MB / 43.47MB                             36.8s
 => => sha256:e2c9ac7522b1ba4990219f5da3fa8a237ad79e289642f3c59b34fd3eb497708e 21.31MB / 21.31MB                             31.1s
 => => sha256:bbf054ba0a5229fbc9686e9a3ae9ee4ddc66c6ed5b71b473d43281f59ccd07e8 187B / 187B                                   17.6s
 => => sha256:b97893c78bcf1e3fe16fbe404f8d5c531a54d3d511a5ad27f25b1af43535b917 109B / 109B                                   19.3s
 => => sha256:9deab862591af6e694ee8f4f9cea3d4259dfa2744e530454f82d556e6d6c09a9 614B / 614B                                   21.0s
 => => sha256:8f0b1300cb5d736735f61de981d04a30dcb1d9b0cc85d2435a7685099137f572 834B / 834B                                   22.6s
 => => sha256:68b633cb1e8f110539c15763dc44815880807dd7860abeefffc4267ab94eaacc 12.46MB / 12.46MB                             32.3s
 => => extracting sha256:ec13ceee25cbb2b3985e8d542c4ed7b50fe306dbe5d837ae2550fdf0e0c62d2e                                     0.4s
 => => extracting sha256:5dbd81dc19b24758fac36b164d830b2a7b50393e6f8a431136bbcbc7620068fc                                     0.1s
 => => extracting sha256:feca6e9fb024afed08de9008506928503cd4ae7fdaf50c7a77961a98e8b4cbc5                                     0.0s
 => => extracting sha256:e2c9ac7522b1ba4990219f5da3fa8a237ad79e289642f3c59b34fd3eb497708e                                     0.6s
 => => extracting sha256:bbf054ba0a5229fbc9686e9a3ae9ee4ddc66c6ed5b71b473d43281f59ccd07e8                                     0.0s
 => => extracting sha256:b97893c78bcf1e3fe16fbe404f8d5c531a54d3d511a5ad27f25b1af43535b917                                     0.0s
 => => extracting sha256:9deab862591af6e694ee8f4f9cea3d4259dfa2744e530454f82d556e6d6c09a9                                     0.0s
 => => extracting sha256:8f0b1300cb5d736735f61de981d04a30dcb1d9b0cc85d2435a7685099137f572                                     0.0s
 => => extracting sha256:68b633cb1e8f110539c15763dc44815880807dd7860abeefffc4267ab94eaacc                                     0.4s
 => ERROR [rabbitmq 2/4] RUN apt-get update && apt-get install -y wget                                                      221.3s
------                                                                                                                             
 > [rabbitmq 2/4] RUN apt-get update && apt-get install -y wget:                                                                   
18.54 Ign:1 http://security.ubuntu.com/ubuntu noble-security InRelease                                                             
18.54 Ign:2 http://archive.ubuntu.com/ubuntu noble InRelease                                                                       
36.97 Ign:1 http://security.ubuntu.com/ubuntu noble-security InRelease                                                             
36.97 Ign:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease                                                               
55.40 Ign:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
55.40 Ign:1 http://security.ubuntu.com/ubuntu noble-security InRelease
73.83 Ign:2 http://archive.ubuntu.com/ubuntu noble InRelease
76.90 Err:1 http://security.ubuntu.com/ubuntu noble-security InRelease
76.90   Temporary failure resolving 'security.ubuntu.com'
92.26 Ign:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease
110.7 Ign:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
129.1 Ign:2 http://archive.ubuntu.com/ubuntu noble InRelease
147.6 Ign:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease
166.0 Ign:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
184.4 Err:2 http://archive.ubuntu.com/ubuntu noble InRelease
184.4   Temporary failure resolving 'archive.ubuntu.com'
202.9 Err:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease
202.9   Temporary failure resolving 'archive.ubuntu.com'
221.3 Err:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
221.3   Temporary failure resolving 'archive.ubuntu.com'
221.3 Reading package lists...
221.3 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble/InRelease  Temporary failure resolving 'archive.ubuntu.com'
221.3 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
221.3 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
221.3 W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/noble-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
221.3 W: Some index files failed to download. They have been ignored, or old ones used instead.
221.3 Reading package lists...
221.3 Building dependency tree...
221.3 Reading state information...
221.3 E: Unable to locate package wget
------
failed to solve: process "/bin/sh -c apt-get update && apt-get install -y wget" did not complete successfully: exit code: 100
exit status 1

问题是 Docker 容器在构建阶段无法解析域名(DNS 解析失败),即便宿主机本身网络没问题。这是个非常常见的 Docker DNS 问题,尤其在公司内网、CI/CD、或海外镜像不通时。

解决:

编辑或创建文件 sudo vim /etc/docker/daemon.json

{
  "dns": ["8.8.8.8", "8.8.4.4"]
}


#############
国内建议:
{
  "dns": ["223.5.5.5", "8.8.8.8"]
}

3. 重启 Docker 服务

sudo systemctl daemon-reexec

sudo systemctl restart docker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值