Secure Docker Images with cnspec
Scan Docker images for security misconfigurations and CVEs with cnspec.
Scan Docker images to find CVEs, end-of-life operating systems, and security misconfigurations before pushing them to a registry. cnspec applies the built-in Mondoo OS security policies (or your own custom policies) to give you a CI/CD gate that prevents vulnerable images from reaching production.
cnspec recognizes the base image's distribution, including hardened and minimal images such as Wolfi, Chainguard OS, MinimOS, CleanStart OS, BellSoft Alpaquita Linux, and BellSoft Hardened Containers. It lists their packages even when the image ships without a package manager, because it reads the package database the image keeps. For the full list, read Supported Linux distributions.
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 images with cnspec, you must have:
- cnspec installed on your workstation
- Docker installed (for scanning local images)
Scanning an image from a registry doesn't need Docker. For local images, 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 Docker images
Scan Docker images in public or private container registries using their registry name:
cnspec scan docker ubuntu:latest
cnspec scan docker elastic/elasticsearch:7.2.0
cnspec scan docker gcr.io/google-containers/ubuntu:22.04
cnspec scan docker registry.access.redhat.com/ubi8/ubiScan 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.
Docker image scans also accept these extra options:
| Option | Description |
|---|---|
--container-proxy | HTTP proxy to use for container pulls |
--disable-cache | Turn off the on-disk image cache, so cnspec reads image data again each time it needs it (significantly slows scans) |
--sudo | Elevate privileges with sudo |
cnspec prints the results to your terminal. To control the output format or send results to a file or CI system, read Report Results.
Disk space for image scans
cnspec writes image data to temporary files on disk rather than holding the image in memory, so the machine running cnspec needs free disk space for the image. cnspec creates these files in the system temporary directory. To use a different directory, set the MONDOO_TMP_DIR environment variable:
export MONDOO_TMP_DIR=/path/with/free/space
cnspec scan docker ubuntu:latestAn image that cnspec reads from the local Docker engine is first saved to the system temporary directory (TMPDIR on Linux and macOS), even when MONDOO_TMP_DIR is set.
Scan a saved image (tar file)
To scan an image you exported with docker save, pass the tar file to cnspec:
docker save ubuntu:latest -o ubuntu.tar
cnspec scan container tar ubuntu.tarcnspec scan docker tar ubuntu.tar works the same way. cnspec scans the image's filesystem without Docker. The command also accepts a container filesystem exported with docker export.
Scan all images on a host
To scan every image in the local Docker engine in one run, scan the local system with image discovery:
cnspec scan local --discover container-imagescnspec scans the local system and each image as its own asset. To also scan every running container, use --discover container,container-images. To learn more, read Scan all containers on a host.
Learn more
-
To scan running containers instead of images, read Secure Docker Containers with cnspec.
-
To scan images already stored in a registry, read about scanning Docker Hub, Amazon ECR, Azure ACR, and Google Container Registry.
-
To learn more about how the MQL query language works, read Write Effective MQL.
Last updated on