Docker (containers and images)¶
Scope: building container images with Docker, running them securely, and running multi-container applications with Docker Compose, mostly for local development and single-host deployments. For running containers on a cluster, see Kubernetes; for building and publishing images in CI, see GitHub Actions. Secrets and supply-chain rules in general/security.md also apply.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 10 Building best practices (Docker Docs): the baseline for Dockerfiles: multi-stage builds, base images,
.dockerignore, ephemeral containers, pinning, rebuilding, and advice for each Dockerfile instruction. - 8 Docker Security Cheat Sheet (OWASP): the baseline for container security: the daemon socket, users, capabilities, privilege escalation, read-only file systems, resource limits, scanning and supply chain.
- 8 Docker Compose (Docker Docs): defining and running multi-container applications, including startup order, secrets, multiple Compose files and production use.
- 7 CIS Docker Benchmark: a consensus hardening benchmark for the Docker host, daemon and containers.
- 7 NIST SP 800-190, Application Container Security Guide (2017): the security concerns that containers bring, with recommendations for addressing them.
Key rules¶
Images¶
- 9
DOCKER-1Run the application as a non-root user: create a user in the Dockerfile and switch to it withUSER. Running as an unprivileged user is the best way to prevent privilege escalation if the container is compromised. (Docker, Building best practices: USER, OWASP, Rule #2: Set a user) - 9
DOCKER-2Never pass secrets to a build throughARG,ENVor copied files. Use secret mounts or SSH mounts. Build arguments and environment variables persist in the final image. (Docker, Build secrets) - 8
DOCKER-3Pin base images to a tag plus digest, and let a bot such as Dependabot update both. Tags are mutable: the same tag can point to a different image when you rebuild. The general rule isSEC-15in general/security.md. (Docker, Pin base image versions, GitHub, Dependabot supported ecosystems: Docker) - 7
DOCKER-4Use multi-stage builds so that the final image contains only what the application needs to run, on a small base image from a trusted source. The final image is smaller, and it leaves out build tools and packages it doesn't need. (Docker, Multi-stage builds, Building best practices: choose the right base image) - 7
DOCKER-5Rebuild images often, with--pullto fetch fresh base images, so that security fixes in the base image reach production. (Docker, Rebuild your images often) - 7
DOCKER-6Start the main process with the exec form, such asENTRYPOINT ["app"], so that it runs as PID 1 and receivesSIGTERM. The shell form runs it under/bin/sh -c, which doesn't pass signals, so the app can't shut down gracefully. If the process doesn't reap child processes, run the container with--init. (Dockerfile reference, ENTRYPOINT, docker container run) - 6
DOCKER-7Order Dockerfile instructions from least to most often changed: copy the dependency manifests and install dependencies before copying the source code. Otherwise every code change reinstalls all dependencies. (Docker, Optimize cache usage: order your layers) - 6
DOCKER-8Keep the build context small with a.dockerignorefile. It keeps unwanted files out of the build and speeds it up, especially with a remote builder. (Docker, Build context: .dockerignore files, Building best practices: exclude with .dockerignore) - 6
DOCKER-9Give each container one concern: run the web app, the database and the cache as separate containers. Separate containers are easier to scale horizontally and to reuse. (Docker, Decouple applications)
Supply chain¶
- 8
DOCKER-10Scan images for known vulnerabilities in CI, fail the build above an agreed severity, and keep scanning the images you have deployed. (OWASP, Rule #9: Integrate container scanning tools into your CI/CD pipeline, Trivy, Container image) - 6
DOCKER-11Attach SBOM and provenance attestations to the images you publish, and sign them so that deployments can verify where an image came from. (Docker, Build attestations, OWASP, Rule #13: Enhance supply chain security, Sigstore, Signing containers)
Running containers¶
- 9
DOCKER-12Never mount the Docker socket into a container, and never expose the daemon over unauthenticated TCP. Access to the socket is root access to the host. (OWASP, Rule #1: Do not expose the Docker daemon socket, Docker Engine security) - 7
DOCKER-13Run containers with least privilege: never use--privileged, drop all capabilities and add back only those needed, setno-new-privileges, and use a read-only root file system where possible. (OWASP, Rule #3: Limit capabilities, Rule #4: Prevent in-container privilege escalation, Rule #8: Set filesystem and volumes to read-only) - 7
DOCKER-14Give containers runtime secrets as Compose or orchestrator secrets, mounted as files, rather than as environment variables. Environment variables are visible to all processes and can end up in logs. (Docker, Manage secrets securely in Docker Compose, OWASP, Rule #12: Utilize Docker Secrets)
Compose¶
- 6
DOCKER-15Make services wait for their dependencies to be ready, not only started: give the dependency ahealthcheckand usedepends_onwithcondition: service_healthy. Compose otherwise waits only until a container is running. (Docker, Control startup and shutdown order in Compose, Dockerfile reference, HEALTHCHECK) - 6
DOCKER-16Keep one basecompose.yamland put environment-specific changes in extra files, such ascompose.override.yamlfor development andcompose.production.yamlfor production. In production, don't bind-mount application code. (Docker, Use Compose in production, Merge Compose files)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 Docker build checks: built into
docker build. Rundocker build --check, and add# check=error=trueto the Dockerfile so that violations fail the build.JSONArgsRecommendedflags shell-formENTRYPOINTandCMD, andSecretsUsedInArgOrEnvflags secret-like names inARGandENV. ChecksDOCKER-6, and partlyDOCKER-2. - 6 Trivy: scans images for known vulnerabilities and embedded secrets.
trivy image --exit-code 1 --severity HIGH,CRITICALfails CI on serious findings. ChecksDOCKER-10, and partlyDOCKER-2. - 6 Docker Scout policy evaluation:
docker scout policyevaluates images locally or in CI. Its Severity-Based Vulnerability, Default Non-Root User, No outdated base images and Supply Chain Attestations policies partly checkDOCKER-10,DOCKER-1,DOCKER-5andDOCKER-11. - 6 Hadolint: a Dockerfile linter that also runs ShellCheck on
RUNinstructions, with--failure-thresholdto choose which severities fail CI. DL3025 flags shell-formCMDandENTRYPOINT, DL3002 a finalUSER root, and DL3006 and DL3007 untagged orlatestbase images. ChecksDOCKER-6, and partlyDOCKER-1andDOCKER-3.
Tutorials and further reading¶
- 7 Docker Learn (Docker Docs): the official learning paths, from containers, images, volumes and Compose to building images, multi-stage builds, the build cache and sharing images on Docker Hub.
- 7 Docker guides (Docker Docs): language-specific guides, such as Node.js, Python, Go and .NET, that containerize an app, set up local development in containers and run tests.
- 6 Docker Hardened Images (Docker Docs): minimal, continuously patched base images with SBOMs and provenance. The core images are free under Apache 2.0.
- 5 Use Compose Watch (Docker Docs): sync or rebuild services automatically while you edit code.
- 5 Development Containers: an open specification for defining a full development environment as a container.