一、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

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

1万+

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



