现象

  • docker build时,如果里面有在容器安装软件的命令,会报错

# 步骤 1: 选择一个稳定且包含完整工具链的基础镜像
# openjdk:8-jdk-buster 是一个基于 Debian 10 (Buster) 的镜像,稳定且软件源丰富
FROM openjdk:8-jdk-buster

# ==================== [核心修改] ====================
# 更换为国内镜像源,这将使后续的 apt 命令不再需要任何代理,
# 并且速度飞快、连接稳定。
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
# ===================================================


# 步骤 2: 设置环境变量,避免 apt-get 在构建过程中出现交互式提示
ENV DEBIAN_FRONTEND=noninteractive

# 步骤 3: 更新软件源并安装所有需要的工具
# 将所有安装命令合并到-一个 RUN 指令中,以减少镜像层数
# 同时,在安装后清理 apt 缓存,以减小最终镜像体积
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    net-tools \
    vim \
    tree \
    grep \
    coreutils \
    curl \
    wget \
    iputils-ping \
    tcpdump \
    dnsutils && \
    rm -rf /var/lib/apt/lists/*

# 说明:
# - net-tools: 提供 netstat 命令
# - vim: 强大的文本编辑器
# - tree: 树形目录查看工具
# - grep: 文本搜索工具 (通常已内置,显式安装确保存在)
# - coreutils: 提供 tail, find 等核心工具 (通常已内置,显式安装确保存在)
# - curl, wget: 网络请求工具
# - iputils-ping: 提供 ping 命令
# - tcpdump: 网络抓包工具
# - dnsutils: 提供 nslookup, dig 等DNS查询工具

# 步骤 4: (可选) 设置工作目录
WORKDIR /apps

# 步骤 5: (可选) 设置默认启动命令
# 这里设置为 bash,这样 `docker run -it` 启动容器后会直接进入交互式 shell
CMD ["bash"]

报错如下:

base git:(develop) ✗ docker build -t iflow-openjdk8:latest .

[+] Building 31.4s (6/7)                                                                                                                                                                                              docker:orbstack
 => [internal] load build definition from Dockerfile                                                                                                                                                                             0.0s
 => => transferring dockerfile: 1.87kB                                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/openjdk:8-jdk-buster                                                                                                                                                          0.7s
 => [internal] load .dockerignore                                                                                                                                                                                                0.0s
 => => transferring context: 2B                                                                                                                                                                                                  0.0s
 => [1/4] FROM docker.io/library/openjdk:8-jdk-buster@sha256:9babc11daaca6927dd9977d9e945b27550ddaa20a4de7743527b23fe3a079523                                                                                                    0.0s
 => CACHED [2/4] RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list &&     sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list                                   0.0s
 => ERROR [3/4] RUN apt-get update &&     apt-get install -y --no-install-recommends     net-tools     vim     tree     grep     coreutils     curl     wget     iputils-ping     tcpdump     dnsutils &&     rm -rf /var/lib/  30.6s
------
 > [3/4] RUN apt-get update &&     apt-get install -y --no-install-recommends     net-tools     vim     tree     grep     coreutils     curl     wget     iputils-ping     tcpdump     dnsutils &&     rm -rf /var/lib/apt/lists/*:
30.51 Err:1 http://mirrors.tuna.tsinghua.edu.cn/debian buster InRelease
30.51   Could not connect to 192.168.5.2:7890 (192.168.5.2), connection timed out
30.51 Err:2 http://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates InRelease
30.51   Unable to connect to 192.168.5.2:7890:
30.51 Err:3 http://mirrors.tuna.tsinghua.edu.cn/debian buster-updates InRelease
30.51   Unable to connect to 192.168.5.2:7890:
30.52 Reading package lists...
30.54 W: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster/InRelease  Could not connect to 192.168.5.2:7890 (192.168.5.2), connection timed out
30.54 W: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/debian-security/dists/buster/updates/InRelease  Unable to connect to 192.168.5.2:7890:
30.54 W: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster-updates/InRelease  Unable to connect to 192.168.5.2:7890:
30.54 W: Some index files failed to download. They have been ignored, or old ones used instead.
30.55 Reading package lists...
30.55 Building dependency tree...
30.55 Reading state information...
30.56 E: Unable to locate package net-tools
30.56 E: Unable to locate package vim
30.56 E: Unable to locate package tree
30.56 E: Unable to locate package tcpdump
30.56 E: Unable to locate package dnsutils
------
Dockerfile:19
--------------------
  18 |     # 同时,在安装后清理 apt 缓存,以减小最终镜像体积
  19 | >>> RUN apt-get update && \
  20 | >>>     apt-get install -y --no-install-recommends \
  21 | >>>     net-tools \
  22 | >>>     vim \
  23 | >>>     tree \
  24 | >>>     grep \
  25 | >>>     coreutils \
  26 | >>>     curl \
  27 | >>>     wget \
  28 | >>>     iputils-ping \
  29 | >>>     tcpdump \
  30 | >>>     dnsutils && \
  31 | >>>     rm -rf /var/lib/apt/lists/*
  32 |
--------------------
ERROR: failed to solve: process "/bin/sh -c apt-get update &&     apt-get install -y --no-install-recommends     net-tools     vim     tree     grep     coreutils     curl     wget     iputils-ping     tcpdump     dnsutils &&     rm -rf /var/lib/apt/lists/*" did not complete successfully: exit code: 100

原因及解决方案

  • 原因是旧的docker配置中硬编码了proxy

➜  ~ cd .docker
➜  .docker ls
buildx          cli-plugins     config.json     config.json.bak contexts        daemon.json     devenvironments features.json   machine         run             scan            scout
➜  .docker cat config.json.bak
{
  "auths": {},
  "credsStore": "osxkeychain",
  "currentContext": "orbstack",
  "proxies": {
    "default": {
      "httpProxy": "http://192.168.5.2:7890",
      "httpsProxy": "http://192.168.5.2:7890"
    }
  }
}%
  • 将上面的proxies删掉,然后重启orbstack就可以解决问题



登陆发表评论