You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose构建镜像时apt-utils安装失败求助

解决Docker Compose构建镜像时apt-utils 404错误

问题重现

构建时触发如下错误:

E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/a/apt/apt-utils_2.4.7_amd64.deb  404  Not Found [IP: ]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
The command '/bin/sh -c apt-get install -y apt-utils' returned a non-zero code: 100
ERROR: Service 'nginx-service' failed to build : Build failed

用户提供的Dockerfile:

FROM ubuntu:latest
RUN apt-get update 
RUN apt-get upgrade -y
RUN apt-get install -y nginx 
RUN apt-get clean 
RUN apt-get install -y curl
RUN apt-get install -y unzip
RUN apt-get install -y wget
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
RUN apt-get install -y php php-xml php-curl php-fpm php-mysql 
RUN apt-get install -y apt-utils --fix-missing
RUN apt-get install -y php-zip --fix-missing
RUN apt-get install -y php-gd --fix-missing
RUN apt-get install -y mysql-server
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y nano
RUN apt-get install -y mc
RUN apt-get install -y systemctl
RUN systemctl start nginx.service

问题原因

  1. APT缓存过期:仅在Dockerfile开头执行一次apt-get update,后续多次apt-get install间隔了大量操作,中途还执行apt-get clean清理了缓存,导致软件包索引过期,对应版本的apt-utils已从源仓库移除,触发404。
  2. Dockerfile设计缺陷:拆分过多apt-get install命令,既增加镜像层数,又加剧缓存失效问题;systemctl无法在Docker默认环境中运行,后续启动服务的命令必然失败。

解决方案

1. 合并APT命令并统一管理缓存

将所有APT相关操作合并为一个RUN指令,确保每次安装都基于最新索引,同时清理冗余文件减小镜像体积:

FROM ubuntu:22.04  # 固定版本替代latest,提升构建稳定性
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC \
    apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
        nginx \
        curl \
        unzip \
        wget \
        tzdata \
        php php-xml php-curl php-fpm php-mysql \
        apt-utils \
        php-zip \
        php-gd \
        mysql-server \
        nano \
        mc && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

2. 移除systemctl相关操作

Docker容器默认没有systemd服务管理环境,无法使用systemctl启动服务。改用CMD直接启动nginx:

# 替换原有的systemctl启动命令
CMD ["nginx", "-g", "daemon off;"]

若需同时运行php-fpm和nginx,可编写启动脚本或使用supervisor管理多进程。

3. 调整Composer安装步骤

将Composer安装放在APT操作之后,避免缓存影响:

RUN curl -sS https://getcomposer.org/installer -o composer-setup.php && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    rm composer-setup.php

修改后的完整Dockerfile

FROM ubuntu:22.04
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC \
    apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
        nginx \
        curl \
        unzip \
        wget \
        tzdata \
        php php-xml php-curl php-fpm php-mysql \
        apt-utils \
        php-zip \
        php-gd \
        mysql-server \
        nano \
        mc && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN curl -sS https://getcomposer.org/installer -o composer-setup.php && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    rm composer-setup.php

CMD ["nginx", "-g", "daemon off;"]

内容的提问来源于stack exchange,提问作者Yorkata008

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 01:41:31