Secure Docker Containers with cnspec
Scan running and stopped Docker containers for CVEs and security misconfigurations with cnspec.
Scan running or stopped Docker containers to find CVEs and security misconfigurations before they cause incidents. cnspec connects to containers directly and applies the OS security policies that match the container's platform (typically Linux), so you can verify that what is actually running matches your security baseline.
This page is part of scanning your supply chain with cnspec. If you're new to cnspec, start with the Quickstart to install cnspec and run your first scan.
Prerequisites
To scan Docker containers with cnspec, you must have:
- cnspec installed on your workstation
- Docker installed and running
cnspec connects to the Docker engine that the DOCKER_HOST environment variable names. If DOCKER_HOST isn't set, cnspec uses the Docker context that DOCKER_CONTEXT names, or else the active Docker CLI context, and otherwise the default Docker socket. This means cnspec reaches a rootless Docker daemon or a remote Docker context without extra flags, for example after you run docker context use rootless.
Scan a Docker container
First, find the container ID of the container you want to scan:
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
3b8f29c5e0a2 nginx:latest "/docker…" 10 minutes ago Up 10 minutes web-serverThen scan the container using its ID:
cnspec scan docker 3b8f29c5e0a2Scan options
| Option | Description |
|---|---|
--asset-name | Override the asset name |
--annotation | Add an annotation to the asset (key=value) |
--incognito | Run in incognito mode (do not report results to Mondoo Platform) |
-o, --output | Set the output format (compact, csv, full, hdf, json, junit, ocsf-json, ocsf-parquet, report, sarif, summary, yaml) |
-f, --policy-bundle | Path to a policy file (local path, s3:// URI, or http(s):// URL) |
--policy | Specify policies to execute (requires --policy-bundle) |
--risk-threshold | Exit with status 1 if any risk meets or exceeds this value (0-100) |
To gate a CI/CD pipeline, combine --risk-threshold with a report file, for example --risk-threshold 90 --output junit --output-target cnspec-junit.xml. 90 fails the job only on critical risks, 70 also fails on high risks, and 40 also fails on medium risks. To scan every infrastructure as code entry point in a repository in one step, use cnspec scan iac. For complete pipeline examples, read Integrate Mondoo with CI/CD Platforms.
The Docker scan also accepts --sudo to elevate privileges when reading host-level state.
cnspec prints the results to your terminal. To control the output format or send results to a file or CI system, read Report Results.
Scan all containers on a host
To scan every running container on a Docker host in one run, scan the local system with container discovery:
cnspec scan local --discover containercnspec scans the local system and each running container as its own asset. The Docker engine must be running and reachable.
These --discover values select what cnspec finds through the Docker engine:
| Value | What cnspec scans |
|---|---|
container | Every running container |
container-images | Every image in the local Docker engine |
all | Every running container and every image, plus MCP servers configured on the host |
To scan running containers and local images together, pass both values: --discover container,container-images. A local scan without one of these values doesn't scan containers or images.
Learn more
-
To scan Docker images instead of containers, read Secure Docker Images with cnspec.
-
To learn more about how the MQL query language works, read Write Effective MQL.
Last updated on