New to Claude Skills? Learn how to install them →

mukul975 on GitHub

Hardening Docker Containers

Free

Secure your Docker containers against vulnerabilities.

Get this skill

Free · Opens the source repo

What Hardening Docker Containers does

The Hardening Docker Containers for Production skill provides developers and DevOps engineers with a comprehensive approach to securing Docker environments in accordance with the CIS Docker Benchmark v1.8.0. This skill focuses on implementing security best practices that minimize the attack surface of Docker containers, prevent privilege escalation, and enforce the principle of least privilege across Docker daemons, images, and runtime configurations. By utilizing this skill, users can ensure that their containerized applications are not only functional but also secure when deployed in production environments.

This skill guides users through the hardening process, which includes configuring Dockerfiles, securing the Docker daemon, and applying runtime security measures. It emphasizes critical hardening principles such as running containers as non-root users, using read-only file systems, and applying security profiles like seccomp and AppArmor. The skill also incorporates tools like docker-bench-security for auditing and validating the security posture of Docker images and runtime settings, ensuring compliance with established security standards.

Ideal for teams looking to establish robust security measures in their containerized applications, this skill is particularly useful during the deployment phase or when conducting security assessments. By following the provided workflows and examples, users can systematically harden their Docker configurations, ensuring that their applications are resilient against potential threats. The skill is designed for those who are familiar with Docker and are looking to enhance their security practices with actionable guidance and best practices.

Whether you are preparing to deploy a new application or auditing existing Docker configurations, this skill provides the necessary tools and knowledge to implement effective security controls and maintain a secure container environment.

When to use it

Use this skill when deploying Docker containers in production or when auditing existing configurations for security compliance.

When not to use it

This skill may not be suitable for environments where Docker is not used or where security hardening is not a priority.

What you can build with it

Deploying a New Application

When deploying a new application in Docker, use this skill to ensure that your Dockerfiles and runtime configurations adhere to security best practices.

Auditing Existing Docker Configurations

Utilize this skill to audit and improve the security posture of existing Docker containers and images, ensuring compliance with CIS standards.

Establishing Security Controls

Implement this skill as part of your security architecture to establish solid controls for Docker container security in your organization.

How to install Hardening Docker Containers

View source

1. Install with the skills CLI

npx skills add mukul975/anthropic-cybersecurity-skills/hardening-docker-containers-for-production --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by mukul975

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

  1. Host Configuration - Audit Docker daemon files, restrict access to /var/run/docker.sock
  2. Docker Daemon Configuration - Enable TLS, restrict inter-container communication, configure logging
  3. Docker Daemon Configuration Files - Set ownership and permissions on daemon.json
  4. Container Images and Build File - Use trusted base images, scan for vulnerabilities, multi-stage builds
  5. Container Runtime - Drop capabilities, read-only rootfs, restrict syscalls
  6. 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

ControlImplementationCIS Section
Non-root userUSER instruction in Dockerfile4.1
Read-only rootfs--read-only flag5.12
Drop capabilities--cap-drop ALL5.3
Resource limits--memory, --cpus, --pids-limit5.10
No new privileges--security-opt no-new-privileges5.25
Content trustDOCKER_CONTENT_TRUST=14.5
TLS for daemondaemon.json TLS config2.6
Audit loggingauditd rules1.1

References

Frequently asked questions about Hardening Docker Containers

Similar skills