Docker构建PHP-FPM+Nginx镜像时无法获取Debian包的解决方法
问题
我尝试构建php:8.1-fpm + Nginx Web服务器镜像,对应的Dockerfile如下:
# 使用官方PHP-FPM镜像 FROM php:8.1-fpm RUN apt-get update && apt-get install -y nginx COPY . /var/www/html/ COPY nginx.conf /etc/nginx/sites-available/default # 安装Composer及项目依赖 RUN apt-get install -y unzip \ && curl -sS https://getcomposer.org/installer | php \ && php composer.phar install RUN docker-php-ext-install pdo pdo_mysql CMD service nginx start && php-fpm
构建过程中出现无法获取Debian包的错误,具体错误信息如下:
17.12 E: 无法获取 http://deb.debian.org/debian/pool/main/e/elfutils/libelf1_0.188-2.1_amd64.deb 连接失败 [IP: 146.75.118.132 80] 17.12 E: 无法获取 http://deb.debian.org/debian/pool/main/libb/libbpf/libbpf1_1.1.0-1_amd64.deb 连接失败 [IP: 146.75.118.132 80] 17.12 E: 无法获取 http://deb.debian.org/debian/pool/main/libb/libbsd/libbsd0_0.11.7-2_amd64.deb 连接失败 [IP: 146.75.118.132 80] 17.12 E: 无法获取 http://deb.debian.org/debian/pool/main/libm/libmnl/libmnl0_1.0.4-3_amd64.deb 连接失败 [IP: 146.75.118.132 80] ############# 更多同类错误 8.725 E: 无法获取 http://deb.debian.org/debian/pool/main/u/unzip/unzip_6.0-28_amd64.deb 连接失败 [IP: 151.101.246.132 80]
我已尝试以下方法修复,但均无效:
- 更换PHP镜像,尝试了bullseye版本、8.2新版本、8.1.2版本;
- 将源地址从deb.debian.org改为archive.debian.org;
- 删除Docker缓存。
请问如何正确获取Debian包以完成镜像构建?
解决方案
1. 替换为国内Debian源
直接在Dockerfile中把默认官方源替换为国内镜像站(如阿里云、清华),解决网络连通问题:
在apt-get update前添加源替换命令:
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
修改后的完整Dockerfile示例:
FROM php:8.1-fpm # 替换为阿里云Debian源 RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ && apt-get update && apt-get install -y nginx COPY . /var/www/html/ COPY nginx.conf /etc/nginx/sites-available/default # 安装Composer及项目依赖 RUN apt-get install -y unzip \ && curl -sS https://getcomposer.org/installer | php \ && php composer.phar install RUN docker-php-ext-install pdo pdo_mysql CMD service nginx start && php-fpm
2. 合并RUN指令优化构建
把多个apt-get相关命令合并,减少缓存层同时避免重复更新源,还能清理缓存减小镜像体积:
FROM php:8.1-fpm RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ && sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list \ && apt-get update && apt-get install -y --no-install-recommends nginx unzip \ && rm -rf /var/lib/apt/lists/* COPY . /var/www/html/ COPY nginx.conf /etc/nginx/sites-available/default RUN curl -sS https://getcomposer.org/installer | php \ && php composer.phar install \ && docker-php-ext-install pdo pdo_mysql \ && rm composer.phar CMD service nginx start && php-fpm
3. 检查Docker网络连通性
- 测试Docker容器能否访问国内源:执行
docker run --rm busybox ping -c 3 mirrors.aliyun.com,如果不通需检查主机网络或Docker daemon配置; - 若处于代理环境,需给Docker配置代理:在
/etc/docker/daemon.json中添加以下内容,然后重启Docker服务:
{ "proxies": { "default": { "httpProxy": "http://你的代理地址:端口", "httpsProxy": "http://你的代理地址:端口" } } }
重启命令:systemctl restart docker
4. 指定Debian版本的源
如果镜像基于Debian bullseye,直接写入对应版本的源,避免源版本跳转问题:
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main contrib non-free" > /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main contrib non-free" >> /etc/apt/sources.list \ && echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list
内容的提问来源于stack exchange,提问作者Alma Z
相关产品推荐
相关产品推荐

