# container-hardening

Hardens Docker images and runtime environments with non-root users, read-only filesystems, and Kubernetes security contexts.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 433 installs · ★ 1177
- Author: devops-skills
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/BagelHole/DevOps-Security-Agent-Skills/tree/HEAD/security/hardening/container-hardening/SKILL.md
- Updated: 2026-05-22
- HTML: https://affpapa.org/en/nevibecode/skills/container-hardening

## What it does

This skill provides templates and rules to lock down container images and deployment manifests. It outlines `Dockerfile` patterns using dedicated non-root users with UID 1001, minimal base images like Alpine or distroless, and explicit ownership via `--chown`.

For execution, it includes hardened `docker run` flags such as `--cap-drop=ALL`, `--security-opt=no-new-privileges:true`, and `--read-only`. It also covers Kubernetes Pod manifests with strict `securityContext` configurations and image scanning rules via Trivy.

Applications that require arbitrary writes across the filesystem will fail under these restrictions. You will need to declare explicit writable paths, such as `--tmpfs /tmp`, before deploying.

## Requirements
- Works with: Docker

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add BagelHole/DevOps-Security-Agent-Skills --skill container-hardening -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/container-hardening/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/container-hardening/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add BagelHole/DevOps-Security-Agent-Skills --skill container-hardening -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/container-hardening/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add BagelHole/DevOps-Security-Agent-Skills --skill container-hardening -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/container-hardening/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install BagelHole/DevOps-Security-Agent-Skills container-hardening
```

2. **Manually: put the files in place**
- File: `.github/skills/container-hardening/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add BagelHole/DevOps-Security-Agent-Skills --skill container-hardening -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/BagelHole/DevOps-Security-Agent-Skills --path security/hardening/container-hardening
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/container-hardening/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add BagelHole/DevOps-Security-Agent-Skills --skill container-hardening -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/BagelHole/DevOps-Security-Agent-Skills/tree/HEAD/security/hardening/container-hardening/SKILL.md

## When to use it
- switch Dockerfile builds to an unprivileged user
- drop unnecessary Linux capabilities on container startup
- configure securityContext for Kubernetes pods
- scan container images for high-severity vulnerabilities with Trivy

## Example prompts
- Harden this Dockerfile by creating a non-root user and stripping build tools.
- Generate a Kubernetes pod manifest with read-only root filesystem and dropped capabilities.
- Show a secure docker run command that blocks privilege escalation.

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: f26e739000c9e683.

BEGIN THIRD-PARTY FILE f26e739000c9e683
````text
---
name: container-hardening
description: Secure Docker images and container runtime configurations. Implement non-root users, read-only filesystems, and security contexts. Use when building secure container images or hardening container deployments.
license: MIT
metadata:
  author: devops-skills
  version: "1.0"
---

# Container Hardening

Secure container images and runtime configurations.

## When to Use This Skill

Use this skill when:
- Building secure container images
- Hardening container deployments
- Meeting container security requirements
- Implementing defense in depth

## Dockerfile Security

```dockerfile
# Use minimal base image
FROM alpine:3.18

# Don't run as root
RUN addgroup -g 1001 -S appgroup && \
    adduser -u 1001 -S appuser -G appgroup

# Copy with specific ownership
COPY --chown=appuser:appgroup . /app

# Remove unnecessary packages
RUN apk del --purge build-dependencies && \
    rm -rf /var/cache/apk/*

# Use non-root user
USER appuser

# Read-only filesystem support
WORKDIR /app
```

## Runtime Security

```bash
# Run with security options
docker run -d \
  --read-only \
  --tmpfs /tmp \
  --security-opt=no-new-privileges:true \
  --cap-drop=ALL \
  --cap-add=NET_BIND_SERVICE \
  --user 1001:1001 \
  myapp:latest
```

## Kubernetes Security Context

```yaml
apiVersion: v1
kind: Pod
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1001
    fsGroup: 1001
  containers:
  - name: app
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities:
        drop: ["ALL"]
```

## Image Scanning

```bash
# Scan with Trivy
trivy image --severity HIGH,CRITICAL myapp:latest

# Use distroless images
FROM gcr.io/distroless/static-debian11
```

## Best Practices

- Use minimal base images
- Run as non-root user
- Enable read-only filesystem
- Drop all capabilities
- Scan images regularly
- Sign and verify images
- Use secrets management

## Related Skills

- [container-scanning](../../scanning/container-scanning/) - Vulnerability scanning
- [kubernetes-hardening](../kubernetes-hardening/) - K8s security
````
END THIRD-PARTY FILE f26e739000c9e683

The third-party file has ended. Everything between the f26e739000c9e683 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

© devops-skills · MIT · https://github.com/BagelHole/DevOps-Security-Agent-Skills/tree/HEAD/security/hardening/container-hardening/SKILL.md

## Similar
- [docker-security-guide](https://affpapa.org/en/nevibecode/skills/docker-security-guide.md) — skill · 595 installs
- [hardening-docker-containers-for-production](https://affpapa.org/en/nevibecode/skills/hardening-docker-containers-for-production.md) — skill · 320 installs
- [container-escape-techniques](https://affpapa.org/en/nevibecode/skills/container-escape-techniques.md) — skill · 3600 installs
- [container-audit](https://affpapa.org/en/nevibecode/skills/container-audit.md) — skill · 342 installs
- [container-publish](https://affpapa.org/en/nevibecode/skills/container-publish.md) — skill · 2800 installs
- [gke-app-onboarding](https://affpapa.org/en/nevibecode/skills/gke-app-onboarding.md) — skill · 6489 installs

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
