Skip to navigation

Docker and headless

Continuous integration runners are usually headless and often containerized. This guide covers running Doc Detective with the official Docker image, what changes when no display is available, and how to record video headlessly.

Before you begin

  • Install Docker if you plan to use the container image.
  • Have a spec or input directory you can run.

Step 1: Run the official container

The official image is docdetective/docdetective:linux on Docker Hub. Its entrypoint is the Doc Detective CLI, so anything you pass after the image name goes straight to doc-detective. Mount your project so the container can read your tests and write results back out:

docker run -v .:/app docdetective/docdetective:linux --input /app/api.spec.json

The -v .:/app flag mounts your current directory to /app inside the container. Because the working directory is /app, point --input and --output at paths under /app so results and artifacts land back in your project.

Inside the official image, unsafe steps run automatically without --allow-unsafe. Those include runCode snippets detected from Markdown code fences. The image sets "allowUnsafeSteps": true in a user config at /etc/doc-detective-xdg/doc-detective/config.json, and points XDG_CONFIG_HOME at it. A user config is the lowest config layer, so your project config, DOC_DETECTIVE_CONFIG, or --no-allow-unsafe overrides it. To opt out, set allowUnsafeSteps to false in your config. See Configuration precedence.

The image also sets DOC_DETECTIVE_CACHE_DIR=/opt/doc-detective, where its browsers are pre-installed, and DOC_DETECTIVE_SKIP_AUTO_UPDATE=1, because the image tag sets the version. For every variable Doc Detective reads, see Environment variables.

Step 2: Understand headless behavior

A headless environment has no display, which affects browser-driven tests:

  • Browser tests run headless automatically when no display is available. On Ubuntu CI runners, Chrome and Firefox contexts fall back to headless mode.
  • Video recording works headless in Chrome. A context that names no browser runs headless Chrome, and the browser recording engine records its tab with no display. The ffmpeg engine captures the screen, so it needs a headed context on a real or virtual display. Headless Firefox and Safari can’t be recorded.

To record Firefox, Safari, or a native app on a headless Linux runner, run the context headed on Xvfb. The official Linux image includes Xvfb, so headless Firefox records there by default. When autoRecord is on, Doc Detective runs it headed on an Xvfb display. A macOS or Windows runner has a display already. For engine details, see Record video.

Step 3: Warm runtime assets

Doc Detective installs heavy assets on first use, including browsers, drivers, and npm packages. In CI, install them ahead of time and cache them so each run starts fast.

Point the cache at a workspace path with DOC_DETECTIVE_CACHE_DIR, then pre-install everything with doc-detective install all:

export DOC_DETECTIVE_CACHE_DIR="$PWD/.doc-detective-cache"
doc-detective install all --yes

On a fresh npm install, Doc Detective also pre-warms these assets automatically. To skip that step, set DOC_DETECTIVE_AUTOINSTALL=0. That’s useful when you want to control exactly when assets download in CI. The official container ships with assets already warmed, so you don’t need this step inside it.

Each run front-loads whatever the cache is missing. Before executing tests, Doc Detective plans the installs, device boots, and downloads its tests need, then runs them concurrently as a warm phase. With a persisted cache the phase completes in milliseconds. On a cold cache it overlaps the downloads instead of paying them one test at a time. The per-task timings land in the JSON results. See Read the warm-phase timings.

docdetective/docdetective:linux is a multi-arch image covering linux/amd64 and linux/arm64. Chrome and ChromeDriver have no native linux/arm64 build upstream, because Google’s Chrome for Testing only publishes x64 binaries. On the linux/arm64 variant, Chrome and ChromeDriver pre-warming is skipped rather than failing the image. To run Chrome-driven tests on arm64 hosts, run the linux/amd64 variant under emulation, or use Firefox instead. Firefox and GeckoDriver both ship in the image for arm64 and run natively, so a Firefox context needs no extra setup.

For a GitHub Actions caching recipe, see Runtime caching.

Step 4: Run headless in CI

Put the pieces together in a generic, platform-agnostic pipeline step. The CLI exits non-zero when a test fails, so a failing spec fails the step:

docker run -v .:/app \
-e DOC_DETECTIVE_AUTOINSTALL=0 \
docdetective/docdetective:linux \
--input /app/docs --output /app/results.json

docker run returns the container’s exit code, so the step fails whenever any spec’s result is FAIL, or whenever nothing valid was tested. For the full list of exit codes and other ways to gate the build, see Reporters and artifacts.

Provide credentials from files

Containers and CI systems often mount secrets as files. Doc Detective can read credentials from a file, so they stay out of your config file and the environment:

SettingHolds
DOC_DETECTIVE_API_FILE environment variablePath to a JSON file with the same contents as DOC_DETECTIVE_API. When both are set, the file wins.
integrations.docDetectiveApi.apiKeyFile config keyPath to a file holding the Orchestration API key. Use it in place of apiKey.
integrations.heretto[].apiTokenFile config keyPath to a file holding a Heretto API token. Use it in place of apiToken. See Heretto.

A relative DOC_DETECTIVE_API_FILE resolves from the current directory. A relative apiKeyFile or apiTokenFile resolves against the config that sets it, as in Configuration precedence. Doc Detective trims whitespace around a key or token. If it can’t read a file, the run exits 2 and names the file.

docker run -v .:/app \
-v /run/secrets/doc-detective-api.json:/secrets/api.json:ro \
-e DOC_DETECTIVE_API_FILE=/secrets/api.json \
docdetective/docdetective:linux

Refuse local URLs on shared runners

Tests can reach localhost, a Docker network, or a private network with no setting. Doc Detective guards two kinds of fetch: screenshot reference images, and OpenAPI descriptions loaded from a URL along with their $refs. By default, it refuses only a hop from a public address to a private one. That’s a public URL that redirects to a private address, or a publicly hosted OpenAPI description whose $ref points at one.

Two environment variables change that. Each one accepts 1, true, yes, or on. Setting both is a usage error, and the CLI exits 2.

VariableEffect
DOC_DETECTIVE_ALLOW_LOCAL_URLSAllows public-to-private hops too, so the guard is off.
DOC_DETECTIVE_BLOCK_LOCAL_URLSRefuses every private and loopback address for the guarded fetches, including direct ones.

On a runner that executes specs you don’t control, set DOC_DETECTIVE_BLOCK_LOCAL_URLS=1. It doesn’t cover URLs that steps request directly, such as goTo, checkLink, and httpRequest, and runShell can reach any address. Pair it with network egress rules on the runner. Behind an HTTP proxy, Doc Detective can check addresses only before the request, so enforce the rules at the proxy too.

Verify it works

  • The container run prints a terminal summary and writes results to the path you passed to --output.
  • Browser tests complete without a display error.
  • If you pre-warmed assets, the run starts without downloading browsers or drivers.

Troubleshooting

A recording step is skipped or fails on a headless runner

Cause: The context is headless Firefox or Safari, or it uses the ffmpeg engine on a headless browser. A headless browser draws nothing on a display, so there’s nothing to capture.

Solution: Record in Chrome, which the browser engine records headless. Otherwise run the context headed: install Xvfb on a Linux runner, or move the test to a macOS or Windows runner. The official Linux image already includes Xvfb. See Record video.

Runs are slow because assets download every time

Cause: The runtime cache isn’t persisted between runs.

Solution: Set DOC_DETECTIVE_CACHE_DIR to a path your CI caches, and pre-install with doc-detective install all --yes. See Runtime caching.

What’s next