基于myoung34/github-runner的Dockerfile升级PHP版本求助
在GitHub Action Runner镜像中升级PHP版本
你无法直接将php:8.1-fpm作为基础镜像,因为myoung34/github-runner已经包含了GitHub Action Runner的核心依赖与配置,切换基础镜像会丢失这些关键环境。正确的做法是在现有runner镜像基础上,通过添加第三方PHP源来安装高版本PHP,具体方案如下:
核心思路
利用Ubuntu官方维护的PHP第三方PPA(Ondřej Surý维护的源),该源提供全系列PHP版本的安装包,可在现有runner镜像上直接叠加安装高版本PHP,同时清理旧版本避免冲突。
修改后的Dockerfile
# base FROM myoung34/github-runner:latest # Let's create a nice FAT image with all bells & whistles RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \ && \ apt-key adv --fetch-keys https://dl.yarnpkg.com/debian/pubkey.gpg \ && \ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list \ && \ add-apt-repository -y ppa:apt-fast/stable \ && \ # 添加PHP官方PPA源,获取高版本PHP包 add-apt-repository -y ppa:ondrej/php \ && \ apt-get update \ && \ # 清理旧版PHP(若镜像中已存在),避免版本冲突 apt purge -y php-cli php-curl php-xml php-zip php-mbstring || true \ && apt autoremove -y \ && \ apt install -y --no-install-recommends \ vim \ apt-utils \ apt-fast \ rsync \ gcc \ g++ \ make \ dh-autoreconf \ zip \ # 替换为PHP 8.1及对应扩展(如需其他版本,替换版本号即可,比如8.2) php8.1-cli \ php8.1-curl \ php8.1-xml \ php8.1-zip \ php8.1-mbstring \ python \ unzip \ nodejs \ yarn \ phantomjs \ && \ apt clean \ && \ # 指定用新安装的PHP 8.1安装composer curl -sS https://getcomposer.org/installer | php8.1 -- --install-dir=/usr/local/bin/ --filename=composer \ && \ npm i -g gulp \ && \ mkdir -p /actions-runner/installers WORKDIR /actions-runner COPY ./installers /actions-runner/installers COPY ./ssh /actions-runner/ssh RUN chmod +x /actions-runner/installers/ssh-keys.sh \ && chmod +x /actions-runner/installers/google-chrome.sh \ && /actions-runner/installers/ssh-keys.sh \ && /actions-runner/installers/google-chrome.sh \ && rm -rf /actions-runner/installers \ && rm -rf /actions-runner/ssh \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* ENV QT_QPA_PLATFORM=offscreen RUN echo 'alias gsed="sed"' >> ~/.bashrc RUN echo -e '#!/bin/bash\nsed "$@"' > /usr/bin/gsed && \ chmod +x /usr/bin/gsed
关键修改说明
- 添加PHP源:新增
add-apt-repository -y ppa:ondrej/php,引入高版本PHP的安装源 - 清理旧版本:加入
apt purge和apt autoremove命令,确保旧版PHP被完全移除,避免版本冲突 - 替换PHP包:将所有
php-*格式的包名替换为php8.1-*,若需要其他版本(如8.2),直接修改版本号即可 - 关联composer:安装composer时指定用
php8.1执行,确保composer使用新安装的PHP版本
验证版本
构建镜像后,可进入容器执行以下命令确认PHP版本:
php -v
内容的提问来源于stack exchange,提问作者Mr. Sam
相关产品推荐
相关产品推荐

