Sub topic 11
Isolation: Choosing Between VMs and Containers
This chapter assumes you can write basic workflows and know the runs-on syntax. It also assumes you know how self-hosted runners register, why a reused runner leaks state, and how an --ephemeral runner takes one job and then exits. Here the question is where that single job should run, and how strongly that place is walled off from everything else.
Why isolation is a separate decision
An ephemeral runner limits how long a job's leftovers survive. It does not limit what the job can reach while it runs. A workflow step is arbitrary code, so it can read any file the runner user can read, call any network address the machine can reach, and use any credential the machine can obtain.
Isolation is the boundary around that code. When you choose one, you are deciding what an attacker gets after a malicious or compromised job runs.
- Host compromise: the job escapes its environment and controls the machine underneath.
- Neighbor compromise: the job reaches other jobs running on the same host at the same time.
- Network pivot: the job uses the runner's network position to reach internal services.
- Metadata theft: the job asks the cloud instance metadata service for the machine's credentials.
Ephemerality handles none of these. A fresh runner on a shared, privileged host is still a foothold on that host.
The isolation spectrum
Most setups fall into one of four places on a spectrum.
- Bare metal or a long-lived VM, one runner process. There is no isolation beyond the operating system user. Ephemeral registration still helps, but the filesystem and host persist.
- Several runner processes on one host. Jobs run side by side. They share the kernel, the Docker daemon if there is one, and often the work directory layout. This is the weakest arrangement and the one to avoid.
- One container per job. Each job gets its own filesystem, process namespace, and resource limits. All containers share the host kernel.
- One VM (or microVM) per job. Each job gets its own kernel, behind a hypervisor boundary. The VM is destroyed afterwards.
What a container actually isolates
A container is a normal Linux process with restricted views. Namespaces give it its own process list, mount table, and network stack. Control groups (cgroups) cap its CPU, memory, and process count. Capabilities and seccomp filters restrict which system calls it may make.
All of that is enforced by the one kernel that the host also runs. A kernel vulnerability, or a misconfiguration that hands the container too much power, lets the job step outside. Containers are strong against accidents and weak against a determined attacker who has a kernel exploit.
A VM adds a second kernel. The guest kernel is the one the job attacks, and the attacker must then also break the hypervisor to reach the host. The hypervisor exposes a much smaller interface than the Linux system call table, which is why the boundary is considered stronger.
The Docker problem
Many workflows build images or use docker run in steps. That requires a container runtime inside the job environment, and this is where container isolation most often fails.
| Approach | What the job gets | Risk |
|---|---|---|
| Mount `/var/run/docker.sock` into the runner container | Control of the host's Docker daemon | Equivalent to root on the host. The job can start a privileged container that mounts the host filesystem. |
| `--privileged` container (Docker-in-Docker) | Nearly all kernel capabilities and device access | Escape is straightforward. Isolation is mostly gone. |
| Rootless Docker or Podman inside the job | A daemon running as an unprivileged user | Fewer features, but a much smaller blast radius. |
| Daemon inside a per-job VM | A full Docker daemon on a disposable kernel | A compromised daemon only owns a VM that is about to be deleted. |
If your workflows need Docker, this requirement is often the strongest argument for per-job VMs. The VM gives the job real root inside a sandbox, and you stop trying to restrict root inside a shared kernel.
Running a runner in a hardened container
If you choose containers, start from restrictive settings and loosen them only when a job needs it. The following command runs one runner from the official runner image with a just-in-time configuration (created as shown in the earlier chapter on JIT configuration). The container removes itself when the single job ends.
docker run --rm \
--name "runner-$(date +%s)" \
--cap-drop ALL \
--security-opt no-new-privileges \
--pids-limit 512 \
--memory 4g \
--cpus 2 \
--tmpfs /tmp \
-e JITCONFIG="$(cat jitconfig.txt)" \
ghcr.io/actions/actions-runner:latest \
/bin/bash -c '/home/runner/run.sh --jitconfig "$JITCONFIG"'Each option has a purpose:
--rmdeletes the container filesystem when the process exits.--cap-drop ALLremoves Linux capabilities the job does not need.--security-opt no-new-privilegesstops processes from gaining privileges through setuid binaries.--pids-limit,--memory, and--cpusstop one job from starving the host through a fork bomb or memory exhaustion.- No Docker socket and no
--privilegedflag are passed.
This container will fail for jobs that call sudo, install system packages, or run Docker. Those jobs belong in a different, VM-based pool with its own label.
Per-job VMs and microVMs
A per-job VM can be a full cloud instance, a virtual machine on your own hypervisor, or a microVM such as one started by Firecracker. The pattern is always the same:
- A controller receives a signal that a job is queued.
- It requests a JIT configuration for that job.
- It boots a VM from a prepared image and passes in the configuration.
- The runner takes the job, finishes, and exits.
- The controller destroys the VM and its disk.
Cost and startup time are the trade. A cloud instance can take tens of seconds to minutes to boot. MicroVMs start in a fraction of a second but require hosts with hardware virtualization, and nested virtualization is unavailable or slow on many cloud instance types. Warm pools of pre-booted VMs cut the wait, but a pre-booted VM must not have a configuration baked in until a job is assigned.
Container-level hardening exists between the two. Sandboxed runtimes such as gVisor or Kata Containers keep the container workflow while putting a user-space kernel or a lightweight VM between the job and the host kernel. They suit teams that run on Kubernetes and want stronger separation without changing how jobs are scheduled. Check compatibility, since some workloads that need unusual system calls behave differently.
Choosing: decision criteria
| Question | Leans toward containers | Leans toward VMs |
|---|---|---|
| Who writes the workflows? | Your own team, reviewed code | Many teams, external contributors, or less-reviewed code |
| Do jobs need Docker or root? | No | Yes |
| Is the repository public, or are forks involved? | No | Yes, though it is better to avoid self-hosted runners for forks entirely |
| Is startup time critical? | Yes, seconds matter | Boot time of tens of seconds is acceptable |
| What can the job reach? | Little of value | Production networks or sensitive data |
| How dense must the fleet be? | High density to control cost | Per-job cost is acceptable |
A common compromise is two pools. A container pool with strict limits serves routine build and test jobs, and a VM pool serves jobs that need Docker, root, or access to sensitive systems. Routing between them uses labels, covered earlier, with access to each pool restricted by runner groups.
Isolation beyond the compute layer
A strong compute boundary can be undone by what surrounds it.
- Network egress: place runners in a subnet whose outbound rules allow only what builds need. Block access to internal management networks.
- Instance metadata: on cloud VMs, require the token-based metadata service version where available, and set the hop limit so that containers on the VM cannot reach it. Otherwise a job can fetch the instance role's credentials.
- Image contents: a runner image should not contain credentials, SSH keys, or cached cloud configuration files.
- Shared caches: a cache directory mounted from the host into many jobs reintroduces the cross-job leak that ephemeral runners were meant to remove. Use the platform cache, which is scoped by repository and branch rules, or a read-only mirror.
- Permissions: keep the
permissionsblock in each workflow minimal, so theGITHUB_TOKENis not a second path to damage.
Common errors
- Many runner processes in one container or VM. They share one filesystem and process space, so ephemeral registration no longer isolates jobs from each other.
- Privileged containers "just to get Docker working." The temporary fix tends to stay permanent.
- Reusing a VM after a job. If the VM is not destroyed, the isolation benefit disappears. Make destruction part of the controller, not a manual step.
- Forgetting resource limits. One runaway job can slow every other job on a shared host.
- Treating containers as sufficient for untrusted code. Where external or unreviewed code runs, assume an attacker has a kernel exploit and use a VM boundary or avoid the self-hosted pool.
Exercise
Your team runs a self-hosted pool where each job gets a fresh container started with --rm. Some workflows build Docker images, so an engineer proposes mounting /var/run/docker.sock into the runner container. What is wrong with this, and what are two better options?
Show answer
Mounting the host's Docker socket gives the job control of the host's Docker daemon, which is equivalent to root on the host. A job can start a privileged container that mounts the host filesystem, so the container boundary and the ephemeral container do nothing to stop it. Better options: run the Docker-needing jobs in a separate pool where each job gets its own VM or microVM with its own daemon, so a compromised daemon only owns a disposable machine. Or use a rootless build tool or rootless Docker or Podman inside the job so no privileged daemon is exposed. Route those jobs with a dedicated label and restrict that pool with a runner group.
Exercise
A job on a per-job VM can still call the cloud instance metadata service and receive the instance role's credentials. Which isolation boundary failed, and what would you change?
Show answer
The compute boundary worked, but the network and credential boundary did not. The VM is disposable, yet the instance role it carries is available to any code running in it. Change it by giving the instance no role, or only a minimal one. Require the token-based metadata service and restrict access to it where possible. Move deployment access to short-lived OIDC credentials issued to specific workflows instead of to the machine.