runc creates a session keyring per container, and affected gVisor releases never release them. After roughly 200 containers, new containers fail with EDQUOT from KEYCTL_JOIN_SESSION_KEYRING. Raise gVisor's /proc/sys/kernel/keys/maxkeys at dind startup as a temporary workaround, guarded so a runc DinD does not change the host-wide limit.
369 lines
11 KiB
Markdown
369 lines
11 KiB
Markdown
# gitea-ci-in-gvisor
|
||
|
||
Gitea Actions deployment with a **gVisor-sandboxed Docker-in-Docker daemon** for
|
||
CI jobs. The runner talks to that daemon over a private Compose network; it does
|
||
not receive a host Docker socket.
|
||
|
||
This repository mirrors the deployment layout used on the target host:
|
||
|
||
```text
|
||
Gitea (Caddy + gitea networks)
|
||
│
|
||
├── act_runner (gitea + ci networks)
|
||
│ │ Docker API: tcp://dind:2375
|
||
│ ▼
|
||
└── dind (ci network, runtime: runsc-docker)
|
||
│ Docker daemon inside gVisor
|
||
▼
|
||
CI job containers
|
||
```
|
||
|
||
## Container nesting
|
||
|
||
```text
|
||
physical host
|
||
├── gitea container [host default runtime] networks: caddy, gitea
|
||
├── runner container [host default runtime] networks: gitea, ci
|
||
└── dind container [runsc-docker] network: ci
|
||
└── CI job container [runc inside gVisor] created by dind
|
||
```
|
||
|
||
The target host defaults to `runsc`, so Gitea and the runner also run under
|
||
gVisor unless the host configuration says otherwise. `dind` explicitly requests
|
||
the `runsc-docker` runtime because it runs Docker inside the sandbox.
|
||
|
||
## Compose layout
|
||
|
||
```text
|
||
networks:
|
||
caddy external
|
||
gitea bridge
|
||
ci bridge
|
||
|
||
services:
|
||
gitea Gitea, on caddy + gitea
|
||
dind gVisor Docker-in-Docker, on ci
|
||
runner act_runner, on gitea + ci
|
||
```
|
||
|
||
### Persistent paths
|
||
|
||
```text
|
||
./gitea -> /data Gitea data
|
||
./dind-data -> /var/lib/docker dind storage
|
||
./dind-runsc/resolv.conf -> /etc/resolv.conf:ro dind DNS
|
||
./runner -> /data act_runner config/state
|
||
/etc/timezone -> /etc/timezone:ro Gitea timezone
|
||
/etc/localtime -> /etc/localtime:ro Gitea timezone
|
||
```
|
||
|
||
The Gitea HTTP port is intentionally not published; it is expected to be
|
||
fronted by Caddy through the external `caddy` network. SSH is published on
|
||
host port `222`.
|
||
|
||
## Requirements
|
||
|
||
- Docker with the `runsc-docker` runtime configured on the host.
|
||
- The host `daemon.json` must define a runtime equivalent to:
|
||
|
||
```json
|
||
{
|
||
"runtimes": {
|
||
"runsc-docker": {
|
||
"path": "/usr/bin/runsc",
|
||
"runtimeArgs": [
|
||
"--net-raw",
|
||
"--allow-packet-socket-write"
|
||
]
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
- An external Docker network named `caddy`.
|
||
- A Caddy configuration that routes the public Gitea hostname to the `gitea`
|
||
container on port 3000.
|
||
- `./dind-runsc/resolv.conf` with usable DNS servers.
|
||
|
||
## Usage
|
||
|
||
```bash
|
||
# Create the external reverse-proxy network once.
|
||
docker network create caddy
|
||
|
||
# Build the gVisor dind image and start the stack.
|
||
docker compose up -d --build
|
||
```
|
||
|
||
Check the stack:
|
||
|
||
```bash
|
||
docker compose ps
|
||
docker compose logs -f dind
|
||
docker compose logs -f runner
|
||
```
|
||
|
||
### Runner registration
|
||
|
||
`./runner` is mounted as `/data` and must contain:
|
||
|
||
- `config.yaml` — act_runner configuration (included here).
|
||
- `.runner` — registration state, created on first registration.
|
||
|
||
The Compose file passes `GITEA_RUNNER_REGISTRATION_TOKEN` into the runner
|
||
container. On first boot, put the token in `.env`:
|
||
|
||
```bash
|
||
cp .env.example .env
|
||
# edit GITEA_RUNNER_REGISTRATION_TOKEN
|
||
docker compose up -d runner
|
||
```
|
||
|
||
Then confirm registration:
|
||
|
||
```bash
|
||
docker compose logs -f runner
|
||
# look for: Runner registered successfully
|
||
ls -l runner/.runner
|
||
```
|
||
|
||
Remove the token from `.env` after `.runner` exists if you prefer. If a stale
|
||
or invalid `./runner/.runner` exists, remove it before retrying:
|
||
|
||
```bash
|
||
rm -f runner/.runner
|
||
docker compose up -d --force-recreate runner
|
||
```
|
||
|
||
If the public URL is not reachable from inside the runner container, use the
|
||
internal Gitea address instead:
|
||
|
||
```yaml
|
||
GITEA_INSTANCE_URL: http://gitea:3000
|
||
```
|
||
|
||
The public Gitea instance is expected at:
|
||
|
||
```text
|
||
https://gitea.ct.cozytren.ch
|
||
```
|
||
|
||
## Runner configuration
|
||
|
||
`runner/config.yaml` defines:
|
||
|
||
- capacity `1`
|
||
- labels:
|
||
- `ubuntu-latest` → `docker.gitea.com/runner-images:ubuntu-latest`
|
||
- `gvisor` → `alpine:3.20`
|
||
- `docker` → `docker:28-cli`
|
||
- runner-side Docker host: `tcp://dind:2375`
|
||
- job environment: `DOCKER_HOST=tcp://127.0.0.1:2375`
|
||
- job networking: `host` inside the dind sandbox
|
||
|
||
Jobs run with host networking inside the gVisor sandbox, so loopback points at
|
||
the dind daemon listening inside that sandbox. The runner itself reaches dind
|
||
through the `ci` network.
|
||
|
||
Example workflows are in `scripts/`:
|
||
|
||
```text
|
||
scripts/workflow-gvisor.yml basic gVisor / isolation checks
|
||
scripts/workflow-docker.yml Docker CLI inside the sandbox
|
||
scripts/workflow-compose.yml a Compose stack inside the sandbox
|
||
scripts/workflow-resource.yml disk / memory / CPU stress
|
||
```
|
||
|
||
## dind storage and overlayfs
|
||
|
||
The dind image comes from the gVisor Docker-in-gVisor example under
|
||
`gvisor/images/basic/docker`.
|
||
|
||
The default Compose configuration starts dind with:
|
||
|
||
```yaml
|
||
command: --disk-store
|
||
```
|
||
|
||
This skips the RAM-backed tmpfs at `/var/lib/docker` and uses the disk-backed
|
||
`./dind-data` volume. It keeps Docker 29's containerd snapshotter enabled, which
|
||
supports image layers containing `security.capability` xattrs.
|
||
|
||
The dind image is built from `gvisor/images/basic/docker`. Its startup script
|
||
supports three modes:
|
||
|
||
```text
|
||
(no flag) RAM-backed tmpfs at /var/lib/docker
|
||
--disk-store disk-backed volume, containerd snapshotter enabled
|
||
--no-overlay disk-backed volume, containerd snapshotter disabled
|
||
```
|
||
|
||
If you prefer a bounded RAM-backed store, remove `command: --disk-store` and
|
||
set `DOCKER_TMPFS_SIZE` explicitly:
|
||
|
||
```yaml
|
||
environment:
|
||
DOCKER_TMPFS_SIZE: 5G
|
||
```
|
||
|
||
### Image pull fails with `security.capability` / `lsetxattr`
|
||
|
||
Example:
|
||
|
||
```text
|
||
failed to register layer: lsetxattr /usr/bin/ping:
|
||
xattr "security.capability": operation not supported
|
||
```
|
||
|
||
This happens when dind uses `--no-overlay` (Docker 29 classic overlay2/vfs)
|
||
on a host-backed volume. gVisor's host-backed filesystem path cannot set the
|
||
`security.capability` xattr in that mode.
|
||
|
||
Use `--disk-store` instead. It keeps the containerd snapshotter enabled and the
|
||
disk-backed volume, and image pulls with file capabilities work. The old
|
||
`--no-overlay` mode is retained only for compatibility and is not recommended
|
||
for runner images containing files such as `/usr/bin/ping`.
|
||
|
||
If you switch an existing deployment from `--no-overlay` to `--disk-store`,
|
||
Docker may keep using the previous `overlay2` storage driver because
|
||
`./dind-data` already contains initialized state. In that case `docker info`
|
||
inside dind still shows:
|
||
|
||
```text
|
||
Storage Driver: overlay2
|
||
```
|
||
|
||
and the xattr failure continues. Reset the dind store once:
|
||
|
||
```bash
|
||
docker compose stop dind
|
||
mv dind-data dind-data.old
|
||
mkdir dind-data
|
||
docker compose up -d dind
|
||
docker exec dind docker info | grep 'Storage Driver'
|
||
```
|
||
|
||
Expected after a clean start:
|
||
|
||
```text
|
||
Storage Driver: overlayfs
|
||
```
|
||
|
||
Cached images and builds in the old dind store are discarded by this reset.
|
||
|
||
## gVisor session keyring exhaustion
|
||
|
||
A long-running dind sandbox can eventually fail to start any new container:
|
||
|
||
```text
|
||
failed to create task for container: failed to create shim task:
|
||
OCI runtime create failed: runc create failed: unable to start container
|
||
process: error during container init: unable to join session keyring:
|
||
unable to create session key: disk quota exceeded
|
||
```
|
||
|
||
This is a gVisor keyring leak, not a disk-space problem. runc creates a new
|
||
session keyring (`_ses.<container-id>`) for every container, and affected
|
||
gVisor releases keep those keyrings forever. gVisor limits a user namespace to
|
||
200 keys, so after roughly 200 container creations every further
|
||
`KEYCTL_JOIN_SESSION_KEYRING` fails with `EDQUOT`. Restarting dind clears the
|
||
sandbox's key set temporarily.
|
||
|
||
The dind startup script raises gVisor's `/proc/sys/kernel/keys/maxkeys` to
|
||
`50000` before starting dockerd. This only delays exhaustion; it does not fix
|
||
the leak. Override the limit with `GVISOR_KEY_MAXKEYS`, for example:
|
||
|
||
```yaml
|
||
services:
|
||
dind:
|
||
environment:
|
||
GVISOR_KEY_MAXKEYS: "100000"
|
||
```
|
||
|
||
The script only changes the sysctl when `/proc/version` identifies gVisor, so
|
||
running the same image under a runc dind does not modify the host-wide limit.
|
||
Remove this workaround once the dind host uses a gVisor release that releases
|
||
session keyrings when their last holder exits. Upstream discussion:
|
||
|
||
- https://github.com/google/gvisor/pull/16231
|
||
- https://github.com/google/gvisor/pull/16232
|
||
|
||
## Workspace bind mounts inside jobs
|
||
|
||
A workflow step such as:
|
||
|
||
```bash
|
||
docker run -v "$repo_dir:/app" ...
|
||
```
|
||
|
||
fails if `$repo_dir` only exists in the job container. The Docker daemon doing the
|
||
bind mount is dind, and it resolves the source path in **its own** filesystem.
|
||
|
||
The deployment prevents this by sharing a workspace volume at the same path in
|
||
both containers:
|
||
|
||
```yaml
|
||
volumes:
|
||
ci-workspace:
|
||
|
||
services:
|
||
dind:
|
||
volumes:
|
||
- ci-workspace:/workspace
|
||
|
||
runner:
|
||
volumes:
|
||
- ci-workspace:/workspace
|
||
```
|
||
|
||
Runner config:
|
||
|
||
```yaml
|
||
container:
|
||
bind_workdir: true
|
||
workdir_parent: workspace
|
||
```
|
||
|
||
With this, the job workspace is under `/workspace/...` on the runner, the same
|
||
path is visible to dind, and dind can bind that path into nested containers.
|
||
|
||
Without this shared path, use Docker named volumes or avoid bind mounts that
|
||
refer to job-only paths.
|
||
|
||
## Networking details
|
||
|
||
- `dind` has no published Docker API port.
|
||
- `runner` reaches `dind:2375` only over the private `ci` network.
|
||
- CI jobs reach the dind daemon at `127.0.0.1:2375` because they share the dind
|
||
sandbox network namespace.
|
||
- `dind` has a custom `resolv.conf` mounted read-only. Keep it aligned with the
|
||
DNS setup of the deployment host.
|
||
- Docker’s embedded DNS inside nested user-defined networks can be limited
|
||
because Docker-managed iptables is disabled inside gVisor. The Compose test
|
||
uses a static IP plus `extra_hosts` for this reason.
|
||
|
||
## Security model
|
||
|
||
- No host `/var/run/docker.sock` is mounted into the runner or CI jobs.
|
||
- The Docker daemon used by CI is itself inside a gVisor sandbox.
|
||
- The Docker API is not published on a host port; it is reachable only from the
|
||
`ci` Compose network.
|
||
- CI job containers are created by the sandboxed dind daemon and remain inside
|
||
that gVisor sandbox.
|
||
- The `dind` container needs `cap_add: ALL` so Docker can operate inside the
|
||
sandbox. Those capabilities are perceived inside gVisor, not granted on the
|
||
host kernel.
|
||
- The remaining security boundary is gVisor itself. Use a current gVisor
|
||
release, a dedicated Docker host, and host firewall rules for untrusted CI.
|
||
|
||
## Files
|
||
|
||
```text
|
||
docker-compose.yml
|
||
runner/config.yaml
|
||
gvisor/images/basic/docker/Dockerfile
|
||
gvisor/images/basic/docker/start-dockerd.sh
|
||
dind-runsc/resolv.conf
|
||
scripts/workflow-*.yml
|
||
```
|