基于Debian的postgres:10镜像安装curl和grep失败,求解决方法
问题描述
我正在使用postgres:10官方镜像(需要恢复包含OID的数据库,而OID在最新版本中不受支持),同时需要安装curl和grep。
官方文档指出(至少新版本)默认使用Debian作为基础镜像,因此我编写了以下Dockerfile:
FROM postgres:10 WORKDIR /db COPY ./start.sh /docker-entrypoint-initdb.d/ RUN apt-get update && apt-get install -y grep RUN apt-get update && apt-get install -y curl EXPOSE 5432 CMD ["postgres"]
但构建时出现大量404错误(使用Apple M3设备),核心错误日志如下:
=> ERROR [db 4/5] RUN apt-get update && apt-get install -y grep 0.7s ------ > [db 4/5] RUN apt-get update && apt-get install -y grep: 0.269 Err:10 http://security.debian.org/debian-security stretch/updates/main arm64 Packages 0.269 404 Not Found 0.305 Err:9 http://apt.postgresql.org/pub/repos/apt stretch-pgdg/main Sources 0.305 404 Not Found [IP: 72.32.157.246 80] 0.633 Err:16 http://deb.debian.org/debian stretch/main arm64 Packages 0.633 404 Not Found 0.695 E: Some index files failed to download. They have been ignored, or old ones used instead.
解决建议
1. 替换Debian Stretch的源为归档源
Debian Stretch已经停止官方维护,原源地址已失效,需替换为Debian归档源:
修改Dockerfile,在安装依赖前替换源配置:
FROM postgres:10 WORKDIR /db COPY ./start.sh /docker-entrypoint-initdb.d/ # 替换为Debian归档源 RUN echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list && \ echo "deb http://archive.debian.org/debian-security stretch/updates main" >> /etc/apt/sources.list && \ echo "deb http://archive.debian.org/debian stretch-backports main" >> /etc/apt/sources.list && \ # 合并安装命令,减少镜像层数 apt-get update && apt-get install -y grep curl --no-install-recommends && \ # 清理apt缓存缩小镜像体积 rm -rf /var/lib/apt/lists/* EXPOSE 5432 CMD ["postgres"]
2. 优化Dockerfile的镜像构建效率
原Dockerfile中两次apt-get update会增加镜像层数,合并为单个RUN命令,同时添加--no-install-recommends避免安装不必要的依赖,最后清理apt缓存。
3. 适配Apple M3的arm64架构
postgres:10官方镜像可能没有预构建的arm64版本,Docker会自动交叉编译,但可能出现源地址的架构适配问题。可以在构建时指定使用amd64架构:
docker build --platform linux/amd64 -t my-postgres:10 .
注:使用amd64架构在M3设备上会通过Rosetta模拟运行,性能略有下降,但能解决源地址的架构兼容性问题。
内容的提问来源于stack exchange,提问作者sam
相关产品推荐
相关产品推荐

