Hardening Docker Containers for Production
Overview
Hardening Docker containers for production involves applying security best practices aligned with CIS Docker Benchmark v1.8.0 to minimize attack surface, prevent privilege escalation, and enforce least-privilege principles across Docker daemon, images, containers, and runtime configurations.
When to Use
- When deploying or configuring hardening docker containers for production capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Prerequisites
- Docker Engine 24.0+ installed
- Docker Compose v2
- Linux host with kernel 5.10+
- Root or sudo access on Docker host
- docker-bench-security tool
- Hadolint for Dockerfile linting
- Dockle for image linting
Core Concepts
CIS Docker Benchmark Sections
- Host Configuration - Audit Docker daemon files, restrict access to /var/run/docker.sock
- Docker Daemon Configuration - Enable TLS, restrict inter-container communication, configure logging
- Docker Daemon Configuration Files - Set ownership and permissions on daemon.json
- Container Images and Build File - Use trusted base images, scan for vulnerabilities, multi-stage builds
- Container Runtime - Drop capabilities, read-only rootfs, restrict syscalls
- Docker Security Operations - Monitor, audit, and rotate credentials
Key Hardening Principles
- Least Privilege: Run containers as non-root, drop all capabilities except required
- Immutability: Use read-only root filesystem, tmpfs for writable directories
- Minimalism: Use distroless or Alpine base images, multi-stage builds
- Isolation: Apply seccomp profiles, AppArmor/SELinux, namespace restrictions
- Auditability: Enable content trust, log all container activity
Workflow
Step 1: Harden the Dockerfile
# Use specific digest for reproducibility
FROM python:3.12-slim@sha256:abc123... AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt
# Production stage - minimal image
FROM gcr.io/distroless/python3-debian12
# Copy only necessary artifacts
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app /app
WORKDIR /app
# Create non-root user
USER 65534:65534
# Set read-only filesystem expectation
LABEL org.opencontainers.image.source="https://github.com/org/app"
ENTRYPOINT ["python", "app.py"]
Step 2: Harden Docker Daemon Configuration
{
"icc": false,
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"live-restore": true,
"userland-proxy": false,
"no-new-privileges": true,
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 64000,
"Soft": 64000
},
"nproc": {
"Name": "nproc",
"Hard": 1024,
"Soft": 1024
}
},
"seccomp-profile": "/etc/docker/seccomp-default.json",
"tls": true,
"tlscacert": "/etc/docker/tls/ca.pem",
"tlscert": "/etc/docker/tls/server-cert.pem",
"tlskey": "/etc/docker/tls/server-key.pem",
"tlsverify": true
}
Step 3: Harden Container Runtime
docker run -d \
--name production-app \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=100m \
--tmpfs /var/run:rw,noexec,nosuid,size=10m \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges:true \
--security-opt seccomp=/etc/docker/seccomp-default.json \
--security-opt apparmor=docker-default \
--pids-limit 100 \
--memory 512m \
--memory-swap 512m \
--cpus 1.0 \
--user 65534:65534 \
--network custom-bridge \
--restart on-failure:3 \
--health-cmd "curl -f http://localhost:8080/health || exit 1" \
--health-interval 30s \
--health-timeout 10s \
--health-retries 3 \
myapp:latest
Step 4: Enable Docker Content Trust
export DOCKER_CONTENT_TRUST=1
export DOCKER_CONTENT_TRUST_SERVER=https://notary.example.com
# Sign and push image
docker trust sign myregistry.com/myapp:v1.0.0
# Verify image signature before pull
docker trust inspect --pretty myregistry.com/myapp:v1.0.0
Step 5: Configure Host-Level Auditing
# Add audit rules for Docker files and directories
cat >> /etc/audit/rules.d/docker.rules << 'EOF'
-w /usr/bin/docker -k docker
-w /var/lib/docker -k docker
-w /etc/docker -k docker
-w /lib/systemd/system/docker.service -k docker
-w /lib/systemd/system/docker.socket -k docker
-w /etc/default/docker -k docker
-w /etc/docker/daemon.json -k docker
-w /usr/bin/containerd -k docker
-w /usr/bin/runc -k docker
EOF
systemctl restart auditd
Validation Commands
# Run Docker Bench Security
docker run --rm --net host --pid host \
--userns host --cap-add audit_control \
-e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
-v /etc:/etc:ro \
-v /usr/bin/containerd:/usr/bin/containerd:ro \
-v /usr/bin/runc:/usr/bin/runc:ro \
-v /usr/lib/systemd:/usr/lib/systemd:ro \
-v /var/lib:/var/lib:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
docker/docker-bench-security
# Lint Dockerfile
hadolint Dockerfile
# Lint built image
dockle myapp:latest
# Verify no containers running as root
docker ps -q | xargs docker inspect --format '{{.Id}}: User={{.Config.User}}'
Key Security Controls
| Control |
Implementation |
CIS Section |
| Non-root user |
USER instruction in Dockerfile |
4.1 |
| Read-only rootfs |
--read-only flag |
5.12 |
| Drop capabilities |
--cap-drop ALL |
5.3 |
| Resource limits |
--memory, --cpus, --pids-limit |
5.10 |
| No new privileges |
--security-opt no-new-privileges |
5.25 |
| Content trust |
DOCKER_CONTENT_TRUST=1 |
4.5 |
| TLS for daemon |
daemon.json TLS config |
2.6 |
| Audit logging |
auditd rules |
1.1 |
References
1---2name: hardening-docker-containers-for-production3description: >-4 Hardens Dockerfiles, images, and per-container runtime settings against the CIS Docker5 Benchmark v1.8.0: non-root users, dropped capabilities, read-only root filesystem, seccomp6 and AppArmor profiles, and minimal multi-stage builds, validated with docker-bench-security,7 Hadolint, and Dockle. Use when preparing a container or Dockerfile for production, or8 auditing images and runtime flags against CIS Docker controls. Keywords: Dockerfile, USER,9 --cap-drop, read-only rootfs, seccomp, AppArmor, multi-stage, Hadolint, Dockle. Do not use10 for the Docker daemon's own configuration - use hardening-docker-daemon-configuration.11domain: cybersecurity12subdomain: container-security13tags:14- containers15- docker16- security17- hardening18- CIS-benchmark19version: '1.0'20author: mahipal21license: Apache-2.022nist_csf:23- PR.PS-0124- PR.IR-0125- ID.AM-0826- DE.CM-0127mitre_attack:28- T161029- T161130- T160931- T152532- T106833metadata:34 source: https://skillmd.com/skills/26zl/hardening-docker-containers-for-production35---3637# Hardening Docker Containers for Production3839## Overview4041Hardening Docker containers for production involves applying security best practices aligned with CIS Docker Benchmark v1.8.0 to minimize attack surface, prevent privilege escalation, and enforce least-privilege principles across Docker daemon, images, containers, and runtime configurations.424344## When to Use4546- When deploying or configuring hardening docker containers for production capabilities in your environment47- When establishing security controls aligned to compliance requirements48- When building or improving security architecture for this domain49- When conducting security assessments that require this implementation5051## Prerequisites5253- Docker Engine 24.0+ installed54- Docker Compose v255- Linux host with kernel 5.10+56- Root or sudo access on Docker host57- docker-bench-security tool58- Hadolint for Dockerfile linting59- Dockle for image linting6061## Core Concepts6263### CIS Docker Benchmark Sections64651. **Host Configuration** - Audit Docker daemon files, restrict access to /var/run/docker.sock662. **Docker Daemon Configuration** - Enable TLS, restrict inter-container communication, configure logging673. **Docker Daemon Configuration Files** - Set ownership and permissions on daemon.json684. **Container Images and Build File** - Use trusted base images, scan for vulnerabilities, multi-stage builds695. **Container Runtime** - Drop capabilities, read-only rootfs, restrict syscalls706. **Docker Security Operations** - Monitor, audit, and rotate credentials7172### Key Hardening Principles7374- **Least Privilege**: Run containers as non-root, drop all capabilities except required75- **Immutability**: Use read-only root filesystem, tmpfs for writable directories76- **Minimalism**: Use distroless or Alpine base images, multi-stage builds77- **Isolation**: Apply seccomp profiles, AppArmor/SELinux, namespace restrictions78- **Auditability**: Enable content trust, log all container activity7980## Workflow8182### Step 1: Harden the Dockerfile8384```dockerfile85# Use specific digest for reproducibility86FROM python:3.12-slim@sha256:abc123... AS builder8788WORKDIR /app89COPY requirements.txt .90RUN pip install --no-cache-dir --user -r requirements.txt9192# Production stage - minimal image93FROM gcr.io/distroless/python3-debian129495# Copy only necessary artifacts96COPY --from=builder /root/.local /root/.local97COPY --from=builder /app /app9899WORKDIR /app100101# Create non-root user102USER 65534:65534103104# Set read-only filesystem expectation105LABEL org.opencontainers.image.source="https://github.com/org/app"106107ENTRYPOINT ["python", "app.py"]108```109110### Step 2: Harden Docker Daemon Configuration111112```json113{114 "icc": false,115 "log-driver": "json-file",116 "log-opts": {117 "max-size": "10m",118 "max-file": "3"119 },120 "live-restore": true,121 "userland-proxy": false,122 "no-new-privileges": true,123 "default-ulimits": {124 "nofile": {125 "Name": "nofile",126 "Hard": 64000,127 "Soft": 64000128 },129 "nproc": {130 "Name": "nproc",131 "Hard": 1024,132 "Soft": 1024133 }134 },135 "seccomp-profile": "/etc/docker/seccomp-default.json",136 "tls": true,137 "tlscacert": "/etc/docker/tls/ca.pem",138 "tlscert": "/etc/docker/tls/server-cert.pem",139 "tlskey": "/etc/docker/tls/server-key.pem",140 "tlsverify": true141}142```143144### Step 3: Harden Container Runtime145146```bash147docker run -d \148 --name production-app \149 --read-only \150 --tmpfs /tmp:rw,noexec,nosuid,size=100m \151 --tmpfs /var/run:rw,noexec,nosuid,size=10m \152 --cap-drop ALL \153 --cap-add NET_BIND_SERVICE \154 --security-opt no-new-privileges:true \155 --security-opt seccomp=/etc/docker/seccomp-default.json \156 --security-opt apparmor=docker-default \157 --pids-limit 100 \158 --memory 512m \159 --memory-swap 512m \160 --cpus 1.0 \161 --user 65534:65534 \162 --network custom-bridge \163 --restart on-failure:3 \164 --health-cmd "curl -f http://localhost:8080/health || exit 1" \165 --health-interval 30s \166 --health-timeout 10s \167 --health-retries 3 \168 myapp:latest169```170171### Step 4: Enable Docker Content Trust172173```bash174export DOCKER_CONTENT_TRUST=1175export DOCKER_CONTENT_TRUST_SERVER=https://notary.example.com176177# Sign and push image178docker trust sign myregistry.com/myapp:v1.0.0179180# Verify image signature before pull181docker trust inspect --pretty myregistry.com/myapp:v1.0.0182```183184### Step 5: Configure Host-Level Auditing185186```bash187# Add audit rules for Docker files and directories188cat >> /etc/audit/rules.d/docker.rules << 'EOF'189-w /usr/bin/docker -k docker190-w /var/lib/docker -k docker191-w /etc/docker -k docker192-w /lib/systemd/system/docker.service -k docker193-w /lib/systemd/system/docker.socket -k docker194-w /etc/default/docker -k docker195-w /etc/docker/daemon.json -k docker196-w /usr/bin/containerd -k docker197-w /usr/bin/runc -k docker198EOF199200systemctl restart auditd201```202203## Validation Commands204205```bash206# Run Docker Bench Security207docker run --rm --net host --pid host \208 --userns host --cap-add audit_control \209 -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \210 -v /etc:/etc:ro \211 -v /usr/bin/containerd:/usr/bin/containerd:ro \212 -v /usr/bin/runc:/usr/bin/runc:ro \213 -v /usr/lib/systemd:/usr/lib/systemd:ro \214 -v /var/lib:/var/lib:ro \215 -v /var/run/docker.sock:/var/run/docker.sock:ro \216 docker/docker-bench-security217218# Lint Dockerfile219hadolint Dockerfile220221# Lint built image222dockle myapp:latest223224# Verify no containers running as root225docker ps -q | xargs docker inspect --format '{{.Id}}: User={{.Config.User}}'226```227228## Key Security Controls229230| Control | Implementation | CIS Section |231|---------|---------------|-------------|232| Non-root user | USER instruction in Dockerfile | 4.1 |233| Read-only rootfs | --read-only flag | 5.12 |234| Drop capabilities | --cap-drop ALL | 5.3 |235| Resource limits | --memory, --cpus, --pids-limit | 5.10 |236| No new privileges | --security-opt no-new-privileges | 5.25 |237| Content trust | DOCKER_CONTENT_TRUST=1 | 4.5 |238| TLS for daemon | daemon.json TLS config | 2.6 |239| Audit logging | auditd rules | 1.1 |240241## References242243- [CIS Docker Benchmark](https://www.cisecurity.org/benchmark/docker)244- [Docker Security Best Practices](https://docs.docker.com/engine/security/)245- [Docker Bench Security Tool](https://github.com/docker/docker-bench-security)246- [Hadolint - Dockerfile Linter](https://github.com/hadolint/hadolint)