---
title: "Image Security Baselines: From Dockerfile to Registry"
date: "2026-11-18T17:00:00+07:00"
description: "End-to-end container security baselines: hardening Dockerfiles, enforcing non-root execution, scanning for CVEs, signing at registry, and runtime policy gates."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/image-security-baselines-dockerfile-to-registry.jpg"
alt: "End-to-end container security pipeline diagram covering Dockerfile hardening, CI scanning, registry signing, and Kubernetes admission policy"
tags: ['docker', 'security', 'devsecops', 'best-practices']
---

Container security cannot be treated as a single checkpoint at deployment time. Securing containerized software requires a continuous, defense-in-depth security baseline spanning every stage of the lifecycle—from initial Dockerfile authoring to registry storage and runtime execution.

In this guide, we will establish an enterprise **Container Image Security Baseline**, covering Dockerfile hardening rules, automated vulnerability scanning, supply chain attestations, registry security policies, and admission control gates.

## The End-to-End Container Security Lifecycle

```text
+-----------------------------------------------------------------------------------+
| Stage 1: AUTHOR (Dockerfile Hardening)                                           |
|   - Minimal base images, non-root USER, no hardcoded secrets, read-only root.     |
+-----------------------------------------------------------------------------------+
                                         │
                                         v
+-----------------------------------------------------------------------------------+
| Stage 2: BUILD & SCAN (DevSecOps CI Pipeline)                                     |
|   - Hadolint Dockerfile linting, Trivy CVE vulnerability scans, SBOM generation.  |
+-----------------------------------------------------------------------------------+
                                         │
                                         v
+-----------------------------------------------------------------------------------+
| Stage 3: PUBLISH & SIGN (Registry Storage)                                        |
|   - Image signing (Cosign / DCT), immutable tags, scoped RBAC tokens.              |
+-----------------------------------------------------------------------------------+
                                         │
                                         v
+-----------------------------------------------------------------------------------+
| Stage 4: DEPLOY & VERIFY (Runtime Admission Control)                              |
|   - Kyverno / OPA policy gates verify signature & scan compliance before Pod start|
+-----------------------------------------------------------------------------------+
```

## Baseline 1: Dockerfile Hardening Controls

### 1. Never Run as Root (`USER 65534` or custom user)
By default, containers run as root (`UID 0`). If an attacker escapes a container process running as root, they gain root access to the host kernel!

```dockerfile
# Create unprivileged group & user
RUN groupadd -g 10001 appgroup && \
    useradd -u 10001 -g appgroup -s /bin/false appuser

USER 10001:10001
```

### 2. Use Minimal Base Images
Avoid full OS images like `ubuntu:22.04` if your application only requires a runtime. Use `alpine`, `distroless`, or `scratch` to minimize installed package count and CVE exposure.

### 3. Use Read-Only Root Filesystem Where Possible
Instruct the container runtime to mount the root filesystem as read-only, restricting temporary writes to explicit `tmpfs` mounts:

```bash
docker run --read-only --tmpfs /tmp --tmpfs /run myapp:v1
```

### 4. Hardened Production Dockerfile Baseline Template

```dockerfile
# Hardened Multi-Stage Production Baseline
FROM golang:1.21-alpine AS builder

WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o app .

# Minimal Production Stage
FROM gcr.io/distroless/static-debian12:nonroot

WORKDIR /
COPY --from=builder /build/app /app

# Non-root user provided by distroless
USER nonroot:nonroot

EXPOSE 8080
ENTRYPOINT ["/app"]
```

## Baseline 2: CI Pipeline Vulnerability Gates

Integrate automated scanners into your CI pipeline to fail builds if high or critical vulnerabilities are detected:

```yaml
# GitHub Actions Security Scan Step (Trivy)
- name: Run Trivy Vulnerability Scanner
  uses: aquasecurity/trivy-action@master
  with:
    image-ref: 'myregistry.com/app:${{ github.sha }}'
    format: 'table'
    exit-code: '1' # Fails build if CRITICAL vulnerabilities exist
    ignore-unfixed: true
    severity: 'CRITICAL,HIGH'
```

## Baseline 3: Supply Chain Integrity & Registry Policies

1. **Sign Images at Registry**: Use **Cosign** or **Docker Content Trust** to attach digital signatures to image manifests after successful security scans.
2. **Enforce Tag Immutability**: Configure registry repository rules to prevent overwriting existing tags.
3. **Use Scoped Service Credentials**: Restrict CI push tokens strictly to target repositories (`repository:<repo>:push`).

## Baseline 4: Kubernetes Admission Policy Enforcement

In Kubernetes clusters, deploy admission controllers (such as **Kyverno** or **OPA Gatekeeper**) to enforce security baselines prior to pod scheduling:

```yaml
# Kyverno Policy: Disallow Privileged Containers & Enforce Signed Images
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: enforce-container-security-baseline
spec:
  validationFailureAction: Enforce
  rules:
    - name: check-non-root
      match:
        resources:
          kinds: [Pod]
      validate:
        message: "Running as root is forbidden. Set runAsNonRoot: true."
        pattern:
          spec:
            securityContext:
              runAsNonRoot: true
```

## Summary Checklist Matrix

| Lifecycle Stage | Security Control | Verification Tool |
| :--- | :--- | :--- |
| **Authoring** | Non-root `USER`, minimal base, no secrets | `hadolint` |
| **Building** | Multi-stage isolation, package cache cleanup | `docker buildx` |
| **Scanning** | CVE detection, SBOM generation | `trivy`, `syft` |
| **Publishing** | Image signing, tag immutability | `cosign`, `dct` |
| **Runtime** | Read-only root, non-root enforcement | `kyverno`, `falco` |

Adopting this end-to-end container security baseline ensures that every image deployed to production is hardened, scanned, signed, and compliant.
