Sub topic 11
Isolation: choosing between VMs and containers
This chapter assumes you have read the earlier chapters on ephemeral runners and just-in-time configuration. You should be comfortable with basic workflow syntax, a Linux shell, and the idea of a container versus a virtual machine.
An ephemeral runner gives each job a clean start. It does not by itself limit what a job can do to the machine it runs on. Isolation is the boundary between the job and the host, and between one job and its neighbors. This chapter covers how to choose that boundary.
What the boundary has to protect
A job runs arbitrary commands. Even trusted workflows run third-party actions and install packages, so assume any job can run hostile code. The boundary should protect three things:
- The host, so a job cannot read host files, other jobs' data, or cloud metadata credentials.
- Neighboring jobs, so one job cannot watch or tamper with another running at the same time.
- Future jobs, so nothing a job writes survives to affect the next one.
Ephemerality handles the third item. Isolation handles the first two.
Containers: fast, but they share a kernel
A container is a set of processes isolated by the host's kernel using namespaces and cgroups. Every container on a host runs on the same kernel.
Containers start in seconds and use little memory. Images are easy to build and version. Kubernetes-based autoscaling, such as Actions Runner Controller, creates one pod per job, which fits this model well.
The cost is the shared kernel. A kernel vulnerability or a misconfiguration can let code escape to the host. Common misconfigurations in runner setups are:
- Running the container with
--privileged. - Mounting
/var/run/docker.sockso jobs can build images. Access to that socket is effectively root on the host. - Mounting host directories into the container.
Many CI jobs need to build images or run containers themselves. That need pushes teams toward the risky options above. Safer routes include a rootless container engine, a separate build service, or running the job inside a stronger boundary.
Virtual machines: slower, with a harder boundary
A virtual machine (VM) runs its own kernel on top of a hypervisor. Escaping a VM means defeating the hypervisor, which has a much smaller attack surface than a full kernel.
A VM can run Docker, systemd, and nested tooling as root without exposing the host. It also behaves like the GitHub-hosted runners that workflows are written for. The costs are slower startup (tens of seconds, or less with pre-warmed pools), higher memory use, and larger images to maintain. Lightweight VM technologies such as Firecracker reduce the startup and memory cost.
Comparing the options
| Property | Container | Virtual machine |
|---|---|---|
| Kernel | Shared with the host | Separate per job |
| Startup time | Seconds | Tens of seconds, less with warm pools |
| Overhead | Low | Higher |
| Docker-in-job | Needs privileged mode, a socket mount, or rootless setup | Works normally |
| Escape risk | Kernel bugs and misconfiguration | Hypervisor bugs |
| Best fit | Trusted code, high job volume | Untrusted or mixed code, jobs needing root |
Choosing
Use these rules:
- If jobs can run code from forks, contributors you do not control, or mixed repositories, use a VM per job.
- If jobs build images or need root, prefer a VM over exposing the Docker socket.
- If all code is reviewed and internal, containers are reasonable and cheaper.
- Different runner groups can use different boundaries. Send sensitive deploy jobs and untrusted jobs to separate pools.
Hardening whichever you pick
Isolation settings do not replace the other controls. Combine them with the following:
- Keep the label set specific, such as
ephemeral, so jobs reach only the intended pool. - Restrict network access. Block runners from reaching the cloud metadata endpoint unless a job needs it.
- Give the runner no standing credentials. Use OIDC for short-lived cloud access, as covered in a later chapter.
- Destroy the VM or container after one job, using
--ephemeralor a JIT configuration.
Exercise
A team runs pull request builds from public forks. Jobs also run docker build. They plan to use ephemeral containers with the Docker socket mounted. What is wrong with this plan, and what would you use instead?
Show answer
The mounted socket gives any job root-equivalent control of the host, and fork code is untrusted, so a hostile pull request could take over the host and see other jobs. Use one VM per job, so Docker runs inside a separate kernel. Also review whether fork pull requests should reach self-hosted runners at all.
Exercise
Which of the three things the boundary protects (host, neighboring jobs, future jobs) does --ephemeral address, and why is it not enough alone?
Show answer
It addresses future jobs, because the runner takes one job and is removed. It does not stop the running job from attacking the host or from affecting jobs running at the same time, so you still need a VM or a carefully configured container.