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: Hardens Dockerfiles, images, and per-container runtime settings against the CIS Docker Benchmark v1.8.0: non-root users, dropped capabilities, read-only root filesystem, seccomp and AppArmor profiles, and minimal multi-stage builds, validated with docker-bench-security, Hadolint, and Dockle. Use when preparing a container or Dockerfile for production, or auditing images and runtime flags against CIS Docker controls. Keywords: Dockerfile, USER, --cap-drop, read-only rootfs, seccomp, AppArmor, multi-stage, Hadolint, Dockle. Do not use for the Docker daemon's own configuration - use hardening-docker-daemon-configuration.4license: Apache-2.05---6# Hardening Docker Containers for Production
7
8## Overview
9
10Hardening 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.
11
12
13## When to Use
14
15- When deploying or configuring hardening docker containers for production capabilities in your environment
16- When establishing security controls aligned to compliance requirements
17- When building or improving security architecture for this domain
18- When conducting security assessments that require this implementation
19
20## Prerequisites
21
22- Docker Engine 24.0+ installed
23- Docker Compose v2
24- Linux host with kernel 5.10+
25- Root or sudo access on Docker host
26- docker-bench-security tool
27- Hadolint for Dockerfile linting
28- Dockle for image linting
29
30## Core Concepts
31
32### CIS Docker Benchmark Sections
33
341. **Host Configuration** - Audit Docker daemon files, restrict access to /var/run/docker.sock
352. **Docker Daemon Configuration** - Enable TLS, restrict inter-container communication, configure logging
363. **Docker Daemon Configuration Files** - Set ownership and permissions on daemon.json
374. **Container Images and Build File** - Use trusted base images, scan for vulnerabilities, multi-stage builds
385. **Container Runtime** - Drop capabilities, read-only rootfs, restrict syscalls
396. **Docker Security Operations** - Monitor, audit, and rotate credentials
40
41### Key Hardening Principles
42
43- **Least Privilege**: Run containers as non-root, drop all capabilities except required
44- **Immutability**: Use read-only root filesystem, tmpfs for writable directories
45- **Minimalism**: Use distroless or Alpine base images, multi-stage builds
46- **Isolation**: Apply seccomp profiles, AppArmor/SELinux, namespace restrictions
47- **Auditability**: Enable content trust, log all container activity
48
49## Workflow
50
51### Step 1: Harden the Dockerfile
52
53```dockerfile
54# Use specific digest for reproducibility
55FROM python:3.12-slim@sha256:abc123... AS builder
56
57WORKDIR /app
58COPY requirements.txt .
59RUN pip install --no-cache-dir --user -r requirements.txt
60
61# Production stage - minimal image
62FROM gcr.io/distroless/python3-debian12
63
64# Copy only necessary artifacts
65COPY --from=builder /root/.local /root/.local
66COPY --from=builder /app /app
67
68WORKDIR /app
69
70# Create non-root user
71USER 65534:65534
72
73# Set read-only filesystem expectation
74LABEL org.opencontainers.image.source="https://github.com/org/app"
75
76ENTRYPOINT ["python", "app.py"]
77```
78
79### Step 2: Harden Docker Daemon Configuration
80
81```json
82{
83 "icc": false,
84 "log-driver": "json-file",
85 "log-opts": {
86 "max-size": "10m",
87 "max-file": "3"
88 },
89 "live-restore": true,
90 "userland-proxy": false,
91 "no-new-privileges": true,
92 "default-ulimits": {
93 "nofile": {
94 "Name": "nofile",
95 "Hard": 64000,
96 "Soft": 64000
97 },
98 "nproc": {
99 "Name": "nproc",
100 "Hard": 1024,
101 "Soft": 1024
102 }
103 },
104 "seccomp-profile": "/etc/docker/seccomp-default.json",
105 "tls": true,
106 "tlscacert": "/etc/docker/tls/ca.pem",
107 "tlscert": "/etc/docker/tls/server-cert.pem",
108 "tlskey": "/etc/docker/tls/server-key.pem",
109 "tlsverify": true
110}
111```
112
113### Step 3: Harden Container Runtime
114
115```bash
116docker run -d \
117 --name production-app \
118 --read-only \
119 --tmpfs /tmp:rw,noexec,nosuid,size=100m \
120 --tmpfs /var/run:rw,noexec,nosuid,size=10m \
121 --cap-drop ALL \
122 --cap-add NET_BIND_SERVICE \
123 --security-opt no-new-privileges:true \
124 --security-opt seccomp=/etc/docker/seccomp-default.json \
125 --security-opt apparmor=docker-default \
126 --pids-limit 100 \
127 --memory 512m \
128 --memory-swap 512m \
129 --cpus 1.0 \
130 --user 65534:65534 \
131 --network custom-bridge \
132 --restart on-failure:3 \
133 --health-cmd "curl -f http://localhost:8080/health || exit 1" \
134 --health-interval 30s \
135 --health-timeout 10s \
136 --health-retries 3 \
137 myapp:latest
138```
139
140### Step 4: Enable Docker Content Trust
141
142```bash
143export DOCKER_CONTENT_TRUST=1
144export DOCKER_CONTENT_TRUST_SERVER=https://notary.example.com
145
146# Sign and push image
147docker trust sign myregistry.com/myapp:v1.0.0
148
149# Verify image signature before pull
150docker trust inspect --pretty myregistry.com/myapp:v1.0.0
151```
152
153### Step 5: Configure Host-Level Auditing
154
155```bash
156# Add audit rules for Docker files and directories
157cat >> /etc/audit/rules.d/docker.rules << 'EOF'
158-w /usr/bin/docker -k docker
159-w /var/lib/docker -k docker
160-w /etc/docker -k docker
161-w /lib/systemd/system/docker.service -k docker
162-w /lib/systemd/system/docker.socket -k docker
163-w /etc/default/docker -k docker
164-w /etc/docker/daemon.json -k docker
165-w /usr/bin/containerd -k docker
166-w /usr/bin/runc -k docker
167EOF
168
169systemctl restart auditd
170```
171
172## Validation Commands
173
174```bash
175# Run Docker Bench Security
176docker run --rm --net host --pid host \
177 --userns host --cap-add audit_control \
178 -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
179 -v /etc:/etc:ro \
180 -v /usr/bin/containerd:/usr/bin/containerd:ro \
181 -v /usr/bin/runc:/usr/bin/runc:ro \
182 -v /usr/lib/systemd:/usr/lib/systemd:ro \
183 -v /var/lib:/var/lib:ro \
184 -v /var/run/docker.sock:/var/run/docker.sock:ro \
185 docker/docker-bench-security
186
187# Lint Dockerfile
188hadolint Dockerfile
189
190# Lint built image
191dockle myapp:latest
192
193# Verify no containers running as root
194docker ps -q | xargs docker inspect --format '{{.Id}}: User={{.Config.User}}'
195```
196
197## Key Security Controls
198
199| Control | Implementation | CIS Section |
200|---------|---------------|-------------|
201| Non-root user | USER instruction in Dockerfile | 4.1 |
202| Read-only rootfs | --read-only flag | 5.12 |
203| Drop capabilities | --cap-drop ALL | 5.3 |
204| Resource limits | --memory, --cpus, --pids-limit | 5.10 |
205| No new privileges | --security-opt no-new-privileges | 5.25 |
206| Content trust | DOCKER_CONTENT_TRUST=1 | 4.5 |
207| TLS for daemon | daemon.json TLS config | 2.6 |
208| Audit logging | auditd rules | 1.1 |
209
210## References
211
212- [CIS Docker Benchmark](https://www.cisecurity.org/benchmark/docker)
213- [Docker Security Best Practices](https://docs.docker.com/engine/security/)
214- [Docker Bench Security Tool](https://github.com/docker/docker-bench-security)
215- [Hadolint - Dockerfile Linter](https://github.com/hadolint/hadolint)