【已解决】docker build操作时出现的问题以及解决办法

该文章已生成可运行项目,

一、build的环境

使用uv包管理软件的python3.10虚拟环境下。使用一个简单的flask接口练习打包功能,项目结构如下:

(1)app.py文件

(2)Dockerfile

(3)requirements.txt文件

由于只是练手小demo,依赖文件可能不全。

二、遇到的问题

(1)使用命令docker build -t flaskdemo .出现报错的多种情况

1)ERROR: failed to build: failed to solve: failed to fetch oauth token: Post "https://auth.docker.io/token": dial tcp 108.160.166.49:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这个错误是由于 Docker 无法连接到 Docker Hub 获取基础镜像导致的,主要可能是网络连接问题。

2)ERROR: failed to build: failed to solve: python:3.10-slim: failed to resolve source metadata for docker.io/library/python:3.10-slim: failed to do request: Head "https://hub-mirror.c.cmyun.xyz/v2/library/python/manifests/3.10-slim?ns=docker.io": dialing hub-mirror.c.cmyun.xyz:443 container via direct connection because disabled has no HTTPS proxy: connecting to hub-mirror.c.cmyun.xyz:443: dial tcp: lookup hub-mirror.c.cmyun.xyz: no such host

这个错误提示显示在连接 Docker 镜像仓库时出现了 TLS 连接问题,具体是连接到host.docker.internal:7890时出现了 EOF(文件结束符)错误,这通常与代理配置有关。

3)ERROR: failed to build: failed to solve: python:3.11-slim: failed to resolve source metadata for docker.io/library/python:3.11-slim: failed commit on ref "unknown-sha256:1f23c790fa0a29a82bcd74d5afef7394e463afb0a66e2007ee10cc35cf31bb80": commit failed: "unknown-sha256:1f23c790fa0a29a82bcd74d5afef7394e463afb0a66e2007ee10cc35cf31bb80" failed size validation: 7632 != 7324: failed precondition

这个错误是说从云端下载下来后和hub上面的大小不匹配无法安装,和网络有一定关系。

4)下载依赖时过于缓慢。

如上图所示 我的依赖其实只有 一个flask,但是安装了87s其实还有更长时间的我没有截图,下载了三个小时...显然是有问题的,正常下完之后应该很快 如下图所示:

一共才15.6s。

为了解决上述报错问题,我进行了如下操作:

1)打开Docker Engine,添加仓库镜像,我使用了很多大学的镜像发现没有解决,在修改为下列镜像仓库后解决。

  "registry-mirrors": [
    "https://docker.xuanyuan.me",
    "https://docker.m.daocloud.io",
    "https://docker.1panel.live",
    "https://hub.rat.dev"
  ]

命令来源https://github.com/tech-shrimp/docker_installer小破站的皮皮虾大神。

2)修改docker中的代理proxies

这个修改有点玄学,因为改完之后会出现偶尔下载不了依赖的问题,要看科学上网的方法是否稳定。

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值