gitea_runner内使用docker
目录
最近在用 gitea 搞自动化部署 但是发现 gitea_runner 内使用 docker 有问题,即使已经添加了 docker.sock
遂记录
2026-1 更新
gitea runner 里面是一个 alpine/dind/dind-rootless 的镜像 + gitea runner 二进制
runner 本省不具备 docker 管理的功能,workflow 实际上就是在一个 临时的目录下 执行类似 脚本化 的操作
也可以使用label 指定需要的 docker环境,但是似乎可用的镜像 | Github很少
所以默认情况去执行 无论是 ssh docker 甚至是 checkout 脚本的时候 都会失败
可以在 容器运行之后 去执行脚本来 安装需要的包
但也可以 选择手动打包 runner 镜像
Dockerfile
FROM gitea/act_runner:latest AS basic
# 安装需要的包
RUN apk add --no-cache docker-cli nodejs
VOLUME /data
# 复用原来的运行脚本
ENTRYPOINT ["/sbin/tini","--","run.sh"]
- 可以放到自己的 gitea 上设置自动运行
- 注意: 如果把 docker.sock 映射到容器中,那么在流水线中将能直接操作 host 的 docker ! 自用问题不大但是公用则可能导致安全风险
参考workflow 配置
yaml
name: Build and Push Docker Image
on:
push:
branches:
- main
pull_request:
branches:
- main
types:
- closed # PR 关闭事件(包含合并)
jobs:
build:
runs-on: [deploy]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build & deploy Docker image
run: |
docker build -t <cname>:latest .
docker compose -p <composename> down
docker compose -p <composename> up -d踩坑
- gitea runner master 分支最新提交无法和 gitea 兼容,建于选择最新版本 tag 来打包,或者直接用
gitea/act_runner:latest来封装 - 安装 docker 也无法使用 docker compose 需要单独处理 docker compose 插件
下为第一版
在 runner 的 compose 中添加如下内容
yaml
version: "3"
services:
runner:
image: docker.io/gitea/act_runner:latest
environment:
GITEA_INSTANCE_URL: http://xxx
GITEA_RUNNER_REGISTRATION_TOKEN: xxxx
GITEA_RUNNER_NAME: action_runner_prod
GITEA_RUNNER_LABELS: prod,ubuntu-latest:docker://node:16-bullseye,ubuntu-22.04:docker://node:16-bullseye,ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster,self-hosted:host
network_mode: host
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command: /bin/bash -c "export https_proxy=http://x.x.x.x:7890 http_proxy=http://x.x.x.x:7890 all_proxy=http://x.x.x.x:7890 && \ # 如果觉得下载慢 可以设置代理
apk add --no-cache docker-cli nodejs && \
echo 'Node:' && node -v && \
echo 'Docker:' && docker -v && \
tail -f /dev/null"- 设置
self-hosted:host这个 label - 在启动时安装 docker cli 和 node 用来运行一些脚本 可以不装
注意事项
这样操作之后 工作流里面就可以直接控制 宿主环境的 docker 警惕操作
