You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac上Docker构建为何尝试使用代理导致失败?

问题描述

我有如下Dockerfile:

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y cmake 

通过brew install --cask docker在Mac上安装了Docker Desktop,执行docker build .时构建失败,报错如下:

> [2/2] RUN apt-get update && apt-get install -y cmake:
0.100 Err:1 http://deb.debian.org/debian bullseye InRelease
0.100   Could not connect to localhost:8080 (127.0.0.1). - connect (111: Connection refused)
0.100 Err:2 http://deb.debian.org/debian-security bullseye-security InRelease
0.100   Unable to connect to localhost:8080:
0.100 Err:3 http://deb.debian.org/debian bullseye-updates InRelease
0.100   Unable to connect to localhost:8080:
0.101 Reading package lists...
0.105 W: Failed to fetch http://deb.debian.org/debian/dists/bullseye/InRelease  Could not connect to localhost:8080 (127.0.0.1). - connect (111: Connection refused)
0.105 W: Failed to fetch http://deb.debian.org/debian-security/dists/bullseye-security/InRelease  Unable to connect to localhost:8080:
0.105 W: Failed to fetch http://deb.debian.org/debian/dists/bullseye-updates/InRelease  Unable to connect to localhost:8080:
0.105 W: Some index files failed to download. They have been ignored, or old ones used instead.

看起来构建尝试通过代理访问,但本地执行env | grep -i proxy无输出,~/.docker/daemon.json内容如下:

{
 "builder": {
  "gc": {
   "defaultKeepStorage": "20GB",
   "enabled": true
  }
 },
 "experimental": false
}

补充信息:

  • 同一Dockerfile在Linux上可正常构建
  • Docker构建中执行cargo install或apt get install等网络操作均出现类似问题,都会尝试通过localhost:8080访问
  • docker pull等Docker构建外的网络操作无异常,未使用代理
  • 已尝试Docker Desktop恢复出厂设置及完全卸载重装

请问为何构建会出现这种疑似使用代理的失败情况?


问题原因与解决思路

核心原因

Mac上的Docker Desktop在构建容器时,会默认继承宿主系统的系统级代理设置,而非仅读取shell环境变量。即使终端中看不到proxy相关环境变量,系统偏好设置里的代理配置(比如HTTP/HTTPS代理指向localhost:8080)仍会被容器内部的网络请求继承,导致容器尝试连接宿主机的8080端口,但该端口没有运行代理服务,从而报错连接拒绝。

另外,也可能是Docker Desktop的构建缓存或内部网络配置残留——即使恢复出厂设置,部分深层网络配置(比如构建守护进程的代理参数)可能未被完全清除;或者是宿主系统的DNS/网络配置间接导致请求被转发到localhost代理。

解决步骤

  1. 检查系统代理设置
    打开Mac系统偏好设置→网络→高级→代理,确认是否开启了HTTP/HTTPS代理且地址为localhost:8080。如果是,临时关闭代理后重新执行docker build。

  2. 强制禁用容器构建时的代理
    在构建命令中显式清除代理环境变量:

    docker build --build-arg HTTP_PROXY="" --build-arg HTTPS_PROXY="" --build-arg NO_PROXY="" .
    

    也可以在Dockerfile中添加以下行,确保容器内部不使用代理:

    FROM debian:bullseye-slim
    ENV HTTP_PROXY=""
    ENV HTTPS_PROXY=""
    ENV NO_PROXY=""
    RUN apt-get update && apt-get install -y cmake 
    
  3. 检查Docker Desktop的代理配置
    打开Docker Desktop偏好设置→资源→代理,确认是否开启了“使用系统代理设置”或手动配置了代理。如果是,切换为“无代理”并重启Docker Desktop。

  4. 重置Docker构建缓存
    执行以下命令清除所有构建缓存,避免残留的代理配置影响:

    docker builder prune -a
    

内容的提问来源于stack exchange,提问作者rcorre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 22:40:13