docker 构建过程中常见问题
Docker 构建的问题指南
(一)docker镜像构建
问题1:解决 Docker 构建中的网络连接问题
Docker 构建过程中可能因网络配置或代理问题导致连接失败。以下是常见问题及解决方法:
1、检查基础网络连通性
确认宿主机可以正常访问互联网,执行 ping 8.8.8.8 或 curl -v https://www.google.com 测试基础网络。若宿主机无法联网,需先解决主机网络问题。
2、配置 Docker Daemon 代理
若企业网络需通过代理访问外网,需在 Docker 服务配置中添加代理设置:
- 创建或编辑
/etc/systemd/system/docker.service.d/http-proxy.conf - 添加以下内容:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080"
Environment="HTTPS_PROXY=http://proxy.example.com:8080"
Environment="NO_PROXY=localhost,127.0.0.1,.internal"
- 重载配置并重启服务:
sudo systemctl daemon-reload
sudo systemctl restart docker
3、使用国内镜像加速
国内用户可配置 Docker 镜像加速器提升下载速度:
- 编辑
/etc/docker/daemon.json - 添加镜像源(如阿里云):
{
"registry-mirrors": ["https://<your-aliyun-mirror>.mirror.aliyuncs.com"]
}
4、构建时指定 DNS
当默认 DNS 解析失败时,可通过 --dns 参数指定:
docker build --dns 8.8.8.8 -t myimage .
或在 Dockerfile 中临时覆盖 DNS:
RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf && \
apt-get update
5、处理容器内证书问题
若遇到 SSL 证书验证失败,可临时禁用验证(不推荐生产环境):
RUN echo "Acquire::https::Verify-Peer \"false\";" > /etc/apt/apt.conf.d/99verify-peer.conf
或正确安装 CA 证书:
COPY ./company-ca.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates
6、调整 MTU 值
在某些网络环境下需调整 MTU 避免分包问题:
docker daemon --mtu 1450
或通过 /etc/docker/daemon.json 配置:
{
"mtu": 1450
}
问题2: Docker 镜像构建无法连接github
1、问题分析:
尽管我们尝试了 GitHub 代理服务,但仍然遇到了网络连接问题,这表明直接通过 git clone 方式在 Docker 构建过程中获取 face_recognition_models 仓库存在困难。
2、解决方案:
为了避免网络问题,我修改了 Dockerfile,改用 wget 下载仓库的 ZIP 包,然后解压并安装。这种方式通常比 git clone 更稳定,尤其是在网络条件不佳的情况下。同时我也添加了 wget 和 unzip 工具的安装,确保这些工具在容器中可用。
原始内容:
# 克隆并安装face_recognition_models
RUN git clone https://github.com/ageitgey/face_recognition_models && \
cd face_recognition_models && \
pip install -e .
修改后的内容:
# 下载并安装face_recognition_models
RUN apt-get update && apt-get install -y wget unzip && \
wget https://github.com/ageitgey/face_recognition_models/archive/master.zip && \
unzip master.zip && \
cd face_recognition_models-master && \
pip install -e .
6万+

被折叠的 条评论
为什么被折叠?



