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

GitHub Actions自托管runner找不到Docker?EC2 Ubuntu环境求助

问题描述

我在EC2 Ubuntu环境部署容器化React/Next.js应用,使用GitHub Actions自托管runner,配置文件node.js.yaml如下:

name: Node.js CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: self-hosted

    strategy:
      matrix:
        node-version: [18.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - name: Check out code
      uses: actions/checkout@v2
      
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-2

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
#       with:
#         context: ./devops/docker
#         file: ./devops/docker/Dockerfile.local
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: docker_nodejs_demo
        IMAGE_TAG: nodejs_demo_image
      run: |
        docker build . -f devops/docker/dockerfile.local -t recipe-1
        docker tag recipe-1:latest ******.dkr.ecr.us-east-2.amazonaws.com/recipe-1:latest 
        docker push ******.dkr.ecr.us-east-2.amazonaws.com/recipe-1:latest

EC2 Ubuntu目录结构:

  • 项目文件夹:/home/ubuntu/actions-runner/_work/recipe-1/recipe-1
  • Dockerfile路径:/home/ubuntu/actions-runner/_work/recipe-1/recipe-1/devops/docker/Dockerfile.local

遇到的报错:

Error: Unable to locate executable file: docker. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.

已尝试但无效的操作:

  • 使用命令docker build . -f devops/docker/dockerfile.local -t recipe-1
  • 将Dockerfile.local移至根目录并重命名为Dockerfile,执行docker build . -t recipe-1
  • 尝试在配置中用with指定file路径(已注释),再执行docker build . -t recipe-1
解决方案

问题核心是自托管runner的用户无Docker执行权限,或Docker未正确配置到PATH中,按以下步骤修复:

1. 确认EC2上已安装Docker

登录EC2实例,执行命令检查Docker状态:

docker --version

若未安装,执行以下命令安装:

sudo apt update
sudo apt install docker.io -y

2. 将runner用户加入docker用户组

GitHub Actions自托管runner默认以ubuntu用户运行,需将该用户加入docker组以获取执行权限:

sudo usermod -aG docker ubuntu

执行后重启runner服务,让权限生效:

sudo systemctl restart actions-runner.service

(若为手动启动的runner,需先停止进程再重新启动)

3. 确认Docker路径在PATH中

检查Docker可执行文件的路径:

which docker

通常输出为/usr/bin/docker,确认该路径在runner用户的PATH中:

echo $PATH

若不在,可修改~/.bashrc添加路径:

echo 'export PATH="$PATH:/usr/bin"' >> ~/.bashrc
source ~/.bashrc

4. 优化GitHub Actions配置(可选)

在run步骤中可直接指定Docker绝对路径测试:

/usr/bin/docker build . -f devops/docker/dockerfile.local -t recipe-1

同时建议使用ECR输出变量拼接镜像地址,避免硬编码:

docker tag recipe-1:latest ${{ steps.login-ecr.outputs.registry }}/recipe-1:latest
docker push ${{ steps.login-ecr.outputs.registry }}/recipe-1:latest

内容的提问来源于stack exchange,提问作者KD.S.T.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 12:40:50