Docker部署Laravel项目时安装PHP8.1出现依赖错误求助
Docker部署Laravel时PHP安装依赖错误解决建议
问题场景
使用Docker部署Laravel项目时,执行apt-get install安装PHP8.1相关扩展失败,此前相同配置可正常运行,已执行docker system prune -a和重装Docker,问题仍存在。
错误信息
=> ERROR [14/26] RUN apt-get install -y php8.1-fpm php8.1-common php8.1-curl php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-gd php8.1-xdebug php8.1-soap php8 2.3s ------ > [14/26] RUN apt-get install -y php8.1-fpm php8.1-common php8.1-curl php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-gd php8.1-xdebug php8.1-soap php8.1-zip: #18 0.621 Reading package lists... #18 1.822 Building dependency tree... #18 2.008 Reading state information... #18 2.106 Some packages could not be installed. This may mean that you have #18 2.106 requested an impossible situation or if you are using the unstable #18 2.106 distribution that some required packages have not yet been created #18 2.106 or been moved out of Incoming. #18 2.106 The following information may help to resolve the situation: #18 2.106 #18 2.106 The following packages have unmet dependencies: #18 2.255 php8.1-fpm : Depends: systemd but it is not going to be installed or #18 2.255 systemd-tmpfiles but it is not installable #18 2.275 E: Unable to correct problems, you have held broken packages. ------ executor failed running [/bin/sh -c apt-get install -y php8.1-fpm php8.1-common php8.1-curl php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-gd php8.1-xdebug php8.1-soap php8.1-zip]: exit code: 100
关联Dockerfile内容
#------------- Setup Environment ------------------------------------------------------------- # Pull base image FROM ubuntu:18.04 ENV TZ=America/Sao_Paulo RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # Install common tools RUN apt-get update RUN apt-get install -y wget curl nano htop git unzip bzip2 software-properties-common locales # Set evn var to enable xterm terminal ENV TERM=xterm # Set working directory WORKDIR /var/www/html COPY . ./ # Add repositories RUN LC_ALL=C.UTF-8 apt-add-repository ppa:ondrej/php RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62 RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C RUN echo "deb http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list RUN echo "deb-src http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list RUN apt-get update #------------- Application Specific Stuff ---------------------------------------------------- # Install PHP RUN apt-get update RUN apt-get install -y \ php8.1-fpm \ php8.1-common \ php8.1-curl \ php8.1-mysql \ php8.1-mbstring \ php8.1-xml \ php8.1-bcmath \ php8.1-gd \ php8.1-xdebug \ php8.1-soap \ php8.1-zip RUN echo "xdebug.mode=develop" >> /etc/php/8.1/mods-available/xdebug.ini #------------- FPM & Nginx configuration ---------------------------------------------------- # Config fpm to use TCP instead of unix socket ADD docker/www.conf /etc/php/8.1/fpm/pool.d/www.conf RUN mkdir -p /var/run/php # Install Nginx RUN apt-get install -y nginx ADD docker/game /etc/nginx/sites-enabled/ ADD docker/nginx.conf /etc/nginx/ #------------- Composer & laravel configuration ---------------------------------------------------- # Install composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer #------------- Supervisor Process Manager ---------------------------------------------------- # Install supervisor RUN apt-get install -y supervisor RUN mkdir -p /var/log/supervisor ADD docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf #------------- Container Config --------------------------------------------------------------- ARG ENV_KEY=dev COPY ./.${ENV_KEY}.env ./.env RUN chown -R www-data:www-data ./ # Expose port 80 EXPOSE 80 # Set supervisor to manage container processes ENTRYPOINT ["/usr/bin/supervisord"]
解决建议
- 替换基础镜像为支持PHP8.1的Ubuntu版本:Ubuntu 18.04已终止标准支持,ondrej/php PPA对其的PHP8.1支持可能已移除,建议替换为Ubuntu 20.04或更高版本:
FROM ubuntu:20.04 - 修复Nginx仓库配置:当前使用的
trusty版本仓库与Ubuntu 18.04/20.04不兼容,需修改为对应版本的仓库(以Ubuntu20.04为例):RUN echo "deb http://nginx.org/packages/ubuntu/ focal nginx" >> /etc/apt/sources.list RUN echo "deb-src http://nginx.org/packages/ubuntu/ focal nginx" >> /etc/apt/sources.list - 手动安装缺失依赖(仅适用于坚持用Ubuntu18.04的场景):强制安装
systemd和systemd-tmpfiles后再安装PHP扩展:RUN apt-get install -y systemd systemd-tmpfiles - 使用官方PHP-FPM镜像:直接基于
php:8.1-fpm镜像构建,避免手动处理依赖问题,简化Dockerfile配置。 - 清理缓存后重新构建:执行以下命令彻底清理镜像缓存并重新构建:
docker build --no-cache .
内容的提问来源于stack exchange,提问作者JohnSan
相关产品推荐
相关产品推荐

