Docker in CI pipelines

Advanced · 1 min

How a CI job gets a Docker engine (socket, Docker-in-Docker, hosted VMs, remote builders), and a pipeline that tests, tags, pushes and signs images.

Before thisBuildKit and docker buildxBuild caches in CIThe Docker socket is root

Docker · 10 min · from 10:48

Docker in CI pipelines

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by DockerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A CI job that builds images needs an engine. The main options are the host's socket, a Docker-in-Docker service, a fresh VM per job, or a remote BuildKit builder.
  • Mounting the host socket into CI jobs (Docker-outside-of-Docker) is fast but gives every job root on the runner host and lets jobs see each other's containers.
  • Docker-in-Docker runs a separate daemon per job. It isolates jobs from each other but needs --privileged and starts with a cold cache.
  • Hosted runners such as GitHub's ubuntu-latest give each job its own VM with Docker installed, so the socket is safe to use there.
  • Tag images with the commit SHA so every build is traceable, and promote that same image through environments instead of rebuilding it.
  • Run integration tests with Compose: `up --abort-on-container-exit --exit-code-from tests` makes the job's result the test container's exit code.
  • Pin third-party actions and tool images by commit SHA or digest. CI holds registry and cloud credentials, which is what the March 2026 trivy-action compromise went after.

The pipeline needs an engine

Most images are built by CI, not by people. A pipeline checks out the code, builds an image, tests it, pushes it and hands it to a deploy. Every one of those steps assumes a Docker engine, or at least a BuildKit builder, is available to the job. Where that engine lives is the most consequential choice in the setup, and it is mostly a security choice.

Four ways to give a job an engine

1. The runner host’s socket (Docker-outside-of-Docker). The job container mounts /var/run/docker.sock and talks to the host’s daemon. It’s fast, and builds share the host’s image and build cache. It also gives every job root on the host, and jobs can see, and stop, each other’s containers. On a self-hosted runner that builds pull requests from strangers, that is a takeover waiting to happen.

2. Docker-in-Docker (DinD). Each job gets its own daemon, running in a docker:dind container alongside it. In GitLab CI:

build:
  image: docker:29
  services:
    - docker:29-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA" .

Jobs no longer share a daemon. But the inner daemon creates namespaces and mounts filesystems, so the dind container must run --privileged, which removes most of the protections from the isolation lesson. And every job starts with an empty cache. Projects such as Sysbox exist to run nested daemons without --privileged.

3. A fresh VM per job. GitHub’s hosted ubuntu-latest runners, and similar offerings elsewhere, give each job its own virtual machine with Docker installed and throw it away afterwards. Using the socket there is fine, because the host is the job.

4. A remote builder. For building alone, the job doesn’t need a local daemon at all. Buildx can send the build to a BuildKit instance elsewhere, a shared builder you run or a hosted service, which also keeps a warm cache. The setup is in BuildKit and docker buildx.

A reasonable default: hosted VM runners for public or untrusted code, remote or rootless builders on self-hosted infrastructure, and never a shared host socket for jobs you don’t fully trust.

A pipeline, step by step

A GitHub Actions workflow that builds, tests, pushes and signs. The action versions are the current majors (checked against each repository’s latest release): checkout v7, setup-buildx-action v4, login-action v4, metadata-action v6, build-push-action v7, cosign-installer v4.

name: image
on:
  push:
    branches: [main]
permissions:
  contents: read
  packages: write
  id-token: write                      # keyless signing
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/acme/api
          tags: |
            type=sha
            type=ref,event=branch
      - name: Integration tests
        run: |
          docker compose -f compose.yaml -f compose.ci.yaml up --build \
            --abort-on-container-exit --exit-code-from tests
          docker compose down -v
      - id: build
        uses: docker/build-push-action@v7
        with:
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          sbom: true
          provenance: mode=max
      - uses: sigstore/cosign-installer@v4
      - run: cosign sign --yes ghcr.io/acme/api@${{ steps.build.outputs.digest }}

What each part is for:

  • Log in before anything pulls. Anonymous pulls from Docker Hub are rate-limited per IP address, and CI runners share addresses. Authenticated pulls, or a pull-through mirror, avoid surprise failures.
  • Tags from metadata. type=sha gives every image a tag like sha-4f2a9c1, so any running container traces back to a commit. Build once and promote that image through staging and production rather than rebuilding per environment.
  • Integration tests with Compose. compose.ci.yaml adds a tests service that runs the test suite against the real database and app. --exit-code-from tests stops everything when the tests finish and returns their exit code, so a failed test fails the job. down -v cleans up volumes. Testcontainers is the alternative when tests should start their own dependencies from code, as in the backend lesson on testing with real dependencies.
  • Attestations. sbom: true and provenance: mode=max attach the SBOM and SLSA provenance that scanning and signing build on.
  • Sign the digest. build-push-action outputs the pushed digest, and keyless signing uses the job’s OIDC token from id-token: write.

Caching layers between runs is the subject of build caches in CI. Add a scan step, failing on fixable high-severity findings, before the push if your policy requires it.

Pin what you run

The workflow above uses version tags for readability. In a real pipeline, pin third-party actions to a full commit SHA, as GitHub’s own hardening guidance and hardening your workflows recommend. In March 2026 an attacker force-pushed 76 of 77 version tags of aquasecurity/trivy-action to credential-stealing code. GitHub’s advisory for the incident lists who was safe: those who referenced the scanner’s images by digest or used immutable releases. Workflows that referenced the action by tag ran the attacker’s code with whatever secrets the job had. The same applies to tool images: aquasec/trivy@sha256:..., not aquasec/trivy:latest.

About the video

The embed is a clip, 10:48 to 20:38, of “Docker-in-Docker: Containerized CI Workflows” from DockerCon 2023, on Docker’s channel. The chapters in the clip are “DooD vs DinD patterns”, “Security risks” and “Sysbox solution”: the trade-off at the heart of this lesson, explained by people who work on it. The talk’s earlier chapters introduce DinD and demonstrate its problems, and later ones cover CI optimisation. Sysbox is a Docker-owned runtime, so the clip is partly a product pitch. It predates the current action versions and the 2026 supply-chain incident. YouTube reports no audio language, so English was judged from the title, description and English auto-captions. Rishab in Cloud’s video in deeper builds a GitHub Actions push pipeline step by step.

What was run: the workflow was parsed with Python’s YAML library, the action versions were read from each action repository’s latest-release redirect on GitHub, and build-push-action’s action.yml was checked for the sbom, provenance and digest names. No pipeline was run. DinD details are from Docker’s and GitLab’s documentation and the incident from GitHub’s advisory, checked 2026-11-28.

Where this fits

CI talks to an engine it was given. The last lesson shows how you point your own CLI at engines on other machines: Docker contexts and remote engines.

Check yourself

  1. 01A self-hosted runner mounts /var/run/docker.sock into every job container. What can a malicious pull request's build do?

    Use the socket to start a container that mounts the runner host's filesystem as root, read other jobs' secrets or tamper with their containers. Socket access is root on the host.

  2. 02Why does Docker-in-Docker usually need --privileged, and what does it cost besides security?

    The inner daemon must create namespaces, mount filesystems and manage cgroups, which the default container profile forbids. Each job also starts with an empty image and build cache unless you add a cache backend.

  3. 03Your test stage runs docker compose up and the job always passes even when tests fail. What is missing?

    --exit-code-from tests (which implies --abort-on-container-exit), so Compose stops when the test container exits and returns its exit code as the job's result.

  4. 04Why tag CI images with the commit SHA rather than only latest?

    A SHA tag ties the image to the exact source it came from and never needs to move, so you can trace, reproduce and promote it. latest is overwritten by every build.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark