dockerfile 从基础镜像python-slim构建时,使用apt 等包管理工具升级和安装好像常规的配镜像都不起作用,试了好多方法最后都是从官方镜像源拉取的,下面是我使用的dockerfile
# 使用Python 3.13 slim镜像
FROM python:3.13-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
# 配置pip使用腾讯云镜像源
RUN pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple/ \
&& pip config set global.trusted-host mirrors.cloud.tencent.com
# 复制requirements.txt
COPY requirements.txt .
# 安装Python依赖
RUN pip install --no-cache-dir -r requirements.txt
# 复制应用代码
COPY . .
# 创建uploads目录
RUN mkdir -p uploads
# 暴露端口
EXPOSE 8000
# 启动命令
CMD ["python", "main.py"]
终端输出包含以下内容
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
7 个赞
你就没有设置修改镜像源,怎么会有。我的是正常的,dockerfile没写对
4 个赞
大佬能分享以下完整的dockerfile吗,试了好多种方法,替换源配置文件,临时使用镜像等,虽然有输出尝试从配置的镜像中拉取,但最后还是从官方源拉。如果大佬愿意分享一下完整的dockerfile,将感激不尽
2 个赞
apt源要修改/etc/apt/sources.list
你把需求告诉llm,会帮你写好的
3 个赞
你直接在前面加上
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
5 个赞
执行报错
sed: can't read /etc/apt/sources.list: No such file or directory
ai的解决方案
原因分析:
Python官方的slim镜像(如python:3.13-slim)有时并不包含/etc/apt/sources.list,而是将其内容拆分到了/etc/apt/sources.list.d/目录下的若干.list文件中。
所以直接修改/etc/apt/sources.list会报找不到文件。
解决方法:
推荐直接批量替换/etc/apt/sources.list和/etc/apt/sources.list.d/*.list下的内容。
你可以用如下命令替换所有相关文件:
RUN find /etc/apt/ -name "*.list" -exec sed -i 's/deb.debian.org/mirrors.aliyun.com/g' {} \; \
&& find /etc/apt/ -name "*.list" -exec sed -i 's/security.debian.org/mirrors.aliyun.com/g' {} \;
RUN apt-get update && apt-get install -y \
替换之后的dockerfile
# 使用Python 3.13 slim镜像
FROM python:3.13-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN find /etc/apt/ -name "*.list" -exec sed -i 's/deb.debian.org/mirrors.aliyun.com/g' {} \; \
&& find /etc/apt/ -name "*.list" -exec sed -i 's/security.debian.org/mirrors.aliyun.com/g' {} \;
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
输出
C:\temp\test>docker build -t aptmirro:1.0 .
[+] Building 13.2s (6/7) docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 449B 0.0s
=> [internal] load metadata for docker.io/library/python:3.13-slim 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/python:3.13-slim 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> CACHED [3/4] RUN find /etc/apt/ -name "*.list" -exec sed -i 's/deb.debian.org/mirrors.aliyun.com/g' {} ; && find /etc/apt/ - 0.0s
=> [4/4] RUN apt-get update && apt-get install -y gcc && rm -rf /var/lib/apt/lists/* 13.2s
=> => # 0 upgraded, 77 newly installed, 0 to remove and 0 not upgraded.
=> => # Need to get 67.9 MB of archives.
=> => # After this operation, 262 MB of additional disk space will be used.
=> => # Get:1 http://deb.debian.org/debian bookworm/main amd64 krb5-locales all 1.20.1-2+deb12u3 [63.1 kB]
=> => # Get:2 http://deb.debian.org/debian bookworm/main amd64 manpages all 6.03-2 [1332 kB]
=> => # Get:3 http://deb.debian.org/debian bookworm/main amd64 binutils-common amd64 2.40-2 [2487 kB
看起来还是没起作用,ai给出的解决方案
# 安装系统依赖
RUN find /etc/apt/ -name "*.list" -exec sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g; s|https://deb.debian.org|http://mirrors.aliyun.com|g; s|http://security.debian.org|http://mirrors.aliyun.com|g; s|https://security.debian.org|http://mirrors.aliyun.com|g' {} \; \
&& cat /etc/apt/sources.list || true && cat /etc/apt/sources.list.d/*.list || true
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
运行后输出
C:\temp\test>docker build -t aptmirro:1.0 .
[+] Building 23.7s (6/7) docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 622B 0.0s
=> [internal] load metadata for docker.io/library/python:3.13-slim 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/python:3.13-slim 0.0s
=> CACHED [2/4] WORKDIR /app 0.0s
=> [3/4] RUN find /etc/apt/ -name "*.list" -exec sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g; s|https://deb.debian. 0.3s
=> [4/4] RUN apt-get update && apt-get install -y gcc && rm -rf /var/lib/apt/lists/* 23.4s
=> => # Get:7 http://deb.debian.org/debian bookworm/main amd64 libgprofng0 amd64 2.40-2 [812 kB]
=> => # Get:8 http://deb.debian.org/debian bookworm/main amd64 libjansson4 amd64 2.14-2 [40.8 kB]
=> => # Get:9 http://deb.debian.org/debian bookworm/main amd64 binutils-x86-64-linux-gnu amd64 2.40-2 [2246 kB]
=> => # Get:10 http://deb.debian.org/debian bookworm/main amd64 binutils amd64 2.40-2 [65.0 kB]
=> => # Get:11 http://deb.debian.org/debian bookworm/main amd64 libisl23 amd64 0.25-1.1 [683 kB]
=> => # Get:12 http://deb.debian.org/debian bookworm/main amd64 libmpfr6 amd64 4.2.0-1 [701 kB]
下载速度依然很慢,好像还是没起作用
3 个赞
你仅仅需要修改一个位置
FROM python:3.9-slim-buster
,我的测试文件是这样的
FROM python:3.9-slim-buster
# 设置国内镜像源 (以阿里云为例)
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
# 更新 apt 缓存并安装软件包zsh
RUN apt-get update && apt-get install --no-install-recommends -y zsh
2 个赞
所以python 所有slim镜像都无法换apt源吗
其实都差不太多,之前报错只是没有source.list文件,你就是新建一个也没有任何问题。主要是有一些追求镜像极度精简,确实会影响一部分命令。有时候直接起一个简单的镜像,exec进去然后直接安装完毕,保存镜像可能更快(相比Dockerfile调试)。
1 个赞
用 slim 也可以,主要是 debian12 之后 apt 系统源换地方了,参考这个:
测试 Dockerfile
FROM python:3.9-slim
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources
RUN sed -i 's/http:/https:/g' /etc/apt/sources.list.d/debian.sources
RUN apt update && apt install --no-install-recommends -y zsh
2 个赞
可惜只能给一个解决方案,只能先来后到委屈大佬了。建议始皇增加多解决方案标记功能