Harness Containerization
Dockerfile review, Kubernetes manifest validation, and container optimization. Smaller images, safer containers, correct orchestration.
When to Use
- When reviewing Dockerfiles for image size, security, and layer efficiency
- When auditing Kubernetes manifests, Helm charts, or docker-compose files
- On PRs that modify container configuration files
- NOT for CI/CD pipeline design (use harness-deployment)
- NOT for infrastructure provisioning (use harness-infrastructure-as-code)
- NOT for application-level security review (use harness-security-review)
Process
Phase 1: SCAN -- Discover Container Configuration
Locate container files. Search the project for container-related configuration:
Dockerfile, Dockerfile.* (multi-target builds)
docker-compose.yml, docker-compose.*.yml (override files)
.dockerignore
k8s/, kubernetes/, manifests/ directories
helm/, charts/ directories
skaffold.yaml, tilt.json (dev tooling)
Identify base images. Parse each Dockerfile for FROM directives:
- Record base image name, tag, and digest (if pinned)
- Flag images using
latest tag
- Flag images from untrusted registries
- Note multi-stage build structure (builder vs. runtime stages)
Inventory Kubernetes resources. Parse manifest files and record:
- Resource types (Deployment, Service, ConfigMap, Secret, Ingress, HPA)
- Namespaces used
- Image references in pod specs
- Resource requests and limits
- Volume mounts and persistent volume claims
Detect Helm usage. If Helm charts exist:
- Parse
Chart.yaml for version and dependencies
- Parse
values.yaml for configurable parameters
- Identify template files and their output resource types
Present scan summary:
Container Scan:
Dockerfiles: 2 (app, worker)
Compose files: 1 (docker-compose.yml + docker-compose.dev.yml)
K8s manifests: 8 resources across 2 namespaces
Helm charts: 1 (app chart with 3 subcharts)
Base images: node:20-alpine, python:3.12-slim
Phase 2: ANALYZE -- Evaluate Best Practices
Analyze Dockerfile layer efficiency. Check each Dockerfile for:
- COPY/ADD placement relative to dependency installation (cache busting)
- Multi-stage builds separating build dependencies from runtime
- Layer count optimization (combining related RUN commands)
- Unnecessary files copied into the image (node_modules, .git, tests)
.dockerignore completeness
Check container security posture. Evaluate:
- Running as non-root user (USER directive present)
- No secrets in build args or environment variables
- Base image currency (is the tag reasonably current)
- HEALTHCHECK directive present
- Read-only filesystem where possible
- No privileged mode in compose or K8s specs
- Security contexts in Kubernetes pod specs (runAsNonRoot, readOnlyRootFilesystem)
Evaluate Kubernetes resource definitions. For each Deployment/StatefulSet:
- Resource requests and limits are set (CPU and memory)
- Liveness and readiness probes are configured
- Pod disruption budgets exist for production workloads
- Horizontal pod autoscaler is configured where appropriate
- Image pull policy is set (Always for mutable tags, IfNotPresent for digests)
Analyze docker-compose configuration. Check for:
- Service dependency ordering (depends_on with health checks)
- Volume mount correctness (host paths vs. named volumes)
- Network isolation between services
- Environment variable management (env_file vs. inline)
- Port mapping conflicts
Check image tag strategy. Verify:
- Production images use immutable tags (semver or digest)
- Development images use descriptive tags (branch name, commit SHA)
- No
latest tag in production manifests
- Registry URL is consistent across all references
Phase 3: OPTIMIZE -- Recommend Improvements
Recommend image size reduction. For each Dockerfile:
- Switch to minimal base images (alpine, distroless, scratch)
- Remove build-only dependencies in multi-stage builds
- Use
.dockerignore to exclude test files, docs, and dev configs
- Estimate size savings for each recommendation
Recommend build performance improvements.
Reorder COPY directives to maximize layer cache hits
Use BuildKit features (cache mounts for package managers)
Split slow-changing layers (OS packages) from fast-changing layers (app code)
Example for Node.js:
# Good: dependency layer cached separately
COPY package.json package-lock.json ./
RUN npm ci --production
COPY src/ ./src/
Recommend Kubernetes improvements.
- Add missing resource limits with reasonable defaults
- Configure probes with appropriate initial delays and periods
- Add pod anti-affinity for high-availability workloads
- Recommend namespace isolation for multi-tenant clusters
- Add network policies to restrict pod-to-pod communication
Recommend security hardening.
- Add non-root USER directive with specific UID
- Add security context to Kubernetes pods
- Pin base images to digest for supply chain security
- Remove unnecessary capabilities (drop ALL, add only what is needed)
Generate optimization summary with estimated impact:
Optimization Summary:
Image size: 850MB -> ~180MB (switch to alpine + multi-stage)
Build time: ~4m -> ~2m (layer reordering + cache mounts)
Security: 3 findings (non-root, capabilities, image pinning)
K8s: 5 resources missing resource limits
Phase 4: VALIDATE -- Verify Configuration Correctness
Validate Dockerfile syntax. Run docker build --check or parse for common errors:
- Invalid instruction ordering (e.g., CMD before COPY)
- Missing required arguments
- Deprecated instructions (MAINTAINER)
- Shell form vs. exec form for CMD/ENTRYPOINT
Validate Kubernetes manifests. Check for:
- Valid YAML structure
- Required fields present (apiVersion, kind, metadata, spec)
- Label selectors match between Deployment and Service
- Port numbers are consistent across Service and container specs
- ConfigMap and Secret references resolve to existing resources
Validate Helm charts. If Helm is used:
helm lint passes
- Template rendering with default values produces valid manifests
- Values schema matches actual usage in templates
- Dependencies are declared and version-locked
Validate docker-compose. Check for:
- Valid YAML and compose file version
- All referenced images exist or have build contexts
- Port mappings do not conflict
- Named volumes are declared in the top-level volumes section
- Networks are declared before use
Generate validation report:
Container Validation: [PASS/WARN/FAIL]
Dockerfiles: PASS (2/2 valid)
K8s manifests: WARN (label mismatch in worker-service.yaml)
Helm chart: PASS (lint clean)
Compose: PASS (valid structure)
Issues:
1. k8s/worker-service.yaml: selector "app: worker" does not match
deployment label "app: worker-v2" -- requests will not route
Harness Integration
harness skill run harness-containerization -- Primary invocation for container review.
harness validate -- Run after configuration changes to verify project health.
harness check-deps -- Verify container tooling dependencies are available.
emit_interaction -- Present optimization recommendations and gather decisions.
Success Criteria
- All container configuration files in the project are discovered and cataloged
- Dockerfiles are analyzed for layer efficiency, security, and size
- Kubernetes manifests are validated for correctness and best practices
- Resource requests and limits are verified for all production workloads
- Image tag strategy is evaluated (no
latest in production)
- Optimization recommendations include estimated impact
Examples
Example: Node.js Monorepo with Docker and Kubernetes
Phase 1: SCAN
Found: Dockerfile (app), Dockerfile.worker, docker-compose.dev.yml
K8s: 12 manifests in k8s/ (2 Deployments, 2 Services, 2 ConfigMaps,
2 HPA, 2 Ingress, 2 PDB)
Base images: node:20 (not alpine), node:20 (worker)
Phase 2: ANALYZE
Dockerfile issues:
- node:20 full image (940MB) -- use node:20-alpine (180MB)
- No .dockerignore -- node_modules and .git copied into image
- No USER directive -- running as root
- No HEALTHCHECK
K8s issues:
- worker deployment missing memory limits
- No network policies defined
- Liveness probe on /healthz but no readiness probe
Phase 3: OPTIMIZE
1. Switch to node:20-alpine -- saves ~760MB per image
2. Add .dockerignore with node_modules, .git, tests, docs
3. Add multi-stage build: builder stage for npm ci, runtime for app
4. Add USER node (UID 1000) after COPY
5. Add readiness probe on /ready endpoint
6. Add memory limit of 512Mi to worker deployment
Phase 4: VALIDATE
Dockerfiles: WARN (2 security findings, 1 size finding)
K8s manifests: WARN (missing limits, missing readiness probe)
Compose: PASS
Result: WARN -- 6 actionable improvements identified
Example: Python FastAPI with Helm and Distroless
Phase 1: SCAN
Found: Dockerfile (multi-stage with distroless runtime)
Helm chart: charts/api/ with values.yaml
Base images: python:3.12-slim (builder), gcr.io/distroless/python3 (runtime)
Phase 2: ANALYZE
Dockerfile: Well-structured multi-stage build
- Builder installs dependencies, runtime copies only venv
- Distroless base (no shell, minimal attack surface)
- Non-root user configured
Helm:
- Resource limits set in values.yaml
- Probes configured with appropriate timeouts
- HPA configured for 2-10 replicas
Phase 3: OPTIMIZE
Minor recommendations only:
- Pin distroless image to digest for reproducibility
- Add --mount=type=cache for pip downloads in builder stage
- Add pod anti-affinity to spread replicas across nodes
Phase 4: VALIDATE
Dockerfile: PASS
Helm lint: PASS
Template render: PASS (all values resolve)
Result: PASS -- well-configured container setup
Rationalizations to Reject
| Rationalization |
Reality |
"We use latest in production because we always want the most recent image" |
latest is a mutable pointer. A deployment at 9am and a rollback at 9pm may pull different image contents with the same tag. Immutable tags (semver or digest) are required for reproducible deployments and reliable rollbacks. |
| "The container runs as root because it needs to bind to port 80" |
Binding to a privileged port requires elevated capability, not full root access. The correct solution is to run the container as a non-root user and use --cap-add NET_BIND_SERVICE, or to expose a high port and use a load balancer or ingress for port translation. |
| "We don't set resource limits because we want the container to use whatever it needs" |
Containers without memory limits can exhaust node memory, triggering OOM kills of other containers on the same node. Kubernetes uses resource requests for scheduling and limits for safety; omitting limits transfers the risk of one container onto the entire node. |
| "Our image is 2GB but it only takes a few seconds to pull in our CI" |
Image size multiplies across every developer pull, every CI run, and every Kubernetes pod startup. A 2GB image that takes 3 seconds to pull in CI with a warm cache takes 90 seconds on a cold node during an autoscaling event at peak traffic. |
| "We don't need liveness and readiness probes — the container exits if it crashes" |
Process exit is a coarse health signal. A process that is running but deadlocked, stuck in an infinite retry loop, or unable to connect to its database will never exit. Kubernetes will continue routing traffic to it. Readiness probes prevent traffic routing to unhealthy containers that are still running. |
Gates
- No
latest tag in production manifests. Production Kubernetes manifests or compose files using latest image tags are blocking findings. Immutable tags or digests are required.
- No containers running as root in production. Missing USER directive in Dockerfiles or missing security context in K8s pods targeting production are blocking findings.
- No missing resource limits in production. Kubernetes Deployments without CPU and memory limits are blocking warnings for production namespaces.
- No invalid manifest references. Label selector mismatches between Services and Deployments, or ConfigMap/Secret references to nonexistent resources, are blocking errors.
Escalation
- When base images have known CVEs: Flag the specific CVEs and recommend upgrading to a patched version. If no patched version exists, recommend an alternative base image and document the migration path.
- When Kubernetes manifest complexity exceeds review scope: For clusters with 50+ resources, recommend focusing on changed resources only (
--changed-only flag) and scheduling a full audit separately.
- When Helm chart dependencies are outdated: Report the version gap and recommend updating. If the update includes breaking changes, flag it as a decision point and present the changelog.
- When docker-compose is used for production: Flag this as an architectural concern. Docker Compose is appropriate for development but production workloads should use an orchestrator (Kubernetes, ECS, Cloud Run). Present migration options.
1---2name: harness-containerization3description: Harness Containerization4metadata:5 source: https://skillmd.com/skills/intense-visions/harness-containerization6---7# Harness Containerization89> Dockerfile review, Kubernetes manifest validation, and container optimization. Smaller images, safer containers, correct orchestration.1011## When to Use1213- When reviewing Dockerfiles for image size, security, and layer efficiency14- When auditing Kubernetes manifests, Helm charts, or docker-compose files15- On PRs that modify container configuration files16- NOT for CI/CD pipeline design (use harness-deployment)17- NOT for infrastructure provisioning (use harness-infrastructure-as-code)18- NOT for application-level security review (use harness-security-review)1920## Process2122### Phase 1: SCAN -- Discover Container Configuration23241. **Locate container files.** Search the project for container-related configuration:25 - `Dockerfile`, `Dockerfile.*` (multi-target builds)26 - `docker-compose.yml`, `docker-compose.*.yml` (override files)27 - `.dockerignore`28 - `k8s/`, `kubernetes/`, `manifests/` directories29 - `helm/`, `charts/` directories30 - `skaffold.yaml`, `tilt.json` (dev tooling)31322. **Identify base images.** Parse each Dockerfile for FROM directives:33 - Record base image name, tag, and digest (if pinned)34 - Flag images using `latest` tag35 - Flag images from untrusted registries36 - Note multi-stage build structure (builder vs. runtime stages)37383. **Inventory Kubernetes resources.** Parse manifest files and record:39 - Resource types (Deployment, Service, ConfigMap, Secret, Ingress, HPA)40 - Namespaces used41 - Image references in pod specs42 - Resource requests and limits43 - Volume mounts and persistent volume claims44454. **Detect Helm usage.** If Helm charts exist:46 - Parse `Chart.yaml` for version and dependencies47 - Parse `values.yaml` for configurable parameters48 - Identify template files and their output resource types49505. **Present scan summary:**5152 ```53 Container Scan:54 Dockerfiles: 2 (app, worker)55 Compose files: 1 (docker-compose.yml + docker-compose.dev.yml)56 K8s manifests: 8 resources across 2 namespaces57 Helm charts: 1 (app chart with 3 subcharts)58 Base images: node:20-alpine, python:3.12-slim59 ```6061---6263### Phase 2: ANALYZE -- Evaluate Best Practices64651. **Analyze Dockerfile layer efficiency.** Check each Dockerfile for:66 - COPY/ADD placement relative to dependency installation (cache busting)67 - Multi-stage builds separating build dependencies from runtime68 - Layer count optimization (combining related RUN commands)69 - Unnecessary files copied into the image (node_modules, .git, tests)70 - `.dockerignore` completeness71722. **Check container security posture.** Evaluate:73 - Running as non-root user (USER directive present)74 - No secrets in build args or environment variables75 - Base image currency (is the tag reasonably current)76 - HEALTHCHECK directive present77 - Read-only filesystem where possible78 - No privileged mode in compose or K8s specs79 - Security contexts in Kubernetes pod specs (runAsNonRoot, readOnlyRootFilesystem)80813. **Evaluate Kubernetes resource definitions.** For each Deployment/StatefulSet:82 - Resource requests and limits are set (CPU and memory)83 - Liveness and readiness probes are configured84 - Pod disruption budgets exist for production workloads85 - Horizontal pod autoscaler is configured where appropriate86 - Image pull policy is set (Always for mutable tags, IfNotPresent for digests)87884. **Analyze docker-compose configuration.** Check for:89 - Service dependency ordering (depends_on with health checks)90 - Volume mount correctness (host paths vs. named volumes)91 - Network isolation between services92 - Environment variable management (env_file vs. inline)93 - Port mapping conflicts94955. **Check image tag strategy.** Verify:96 - Production images use immutable tags (semver or digest)97 - Development images use descriptive tags (branch name, commit SHA)98 - No `latest` tag in production manifests99 - Registry URL is consistent across all references100101---102103### Phase 3: OPTIMIZE -- Recommend Improvements1041051. **Recommend image size reduction.** For each Dockerfile:106 - Switch to minimal base images (alpine, distroless, scratch)107 - Remove build-only dependencies in multi-stage builds108 - Use `.dockerignore` to exclude test files, docs, and dev configs109 - Estimate size savings for each recommendation1101112. **Recommend build performance improvements.**112 - Reorder COPY directives to maximize layer cache hits113 - Use BuildKit features (cache mounts for package managers)114 - Split slow-changing layers (OS packages) from fast-changing layers (app code)115 - Example for Node.js:116117 ```dockerfile118 # Good: dependency layer cached separately119 COPY package.json package-lock.json ./120 RUN npm ci --production121 COPY src/ ./src/122 ```1231243. **Recommend Kubernetes improvements.**125 - Add missing resource limits with reasonable defaults126 - Configure probes with appropriate initial delays and periods127 - Add pod anti-affinity for high-availability workloads128 - Recommend namespace isolation for multi-tenant clusters129 - Add network policies to restrict pod-to-pod communication1301314. **Recommend security hardening.**132 - Add non-root USER directive with specific UID133 - Add security context to Kubernetes pods134 - Pin base images to digest for supply chain security135 - Remove unnecessary capabilities (drop ALL, add only what is needed)1361375. **Generate optimization summary with estimated impact:**138139 ```140 Optimization Summary:141 Image size: 850MB -> ~180MB (switch to alpine + multi-stage)142 Build time: ~4m -> ~2m (layer reordering + cache mounts)143 Security: 3 findings (non-root, capabilities, image pinning)144 K8s: 5 resources missing resource limits145 ```146147---148149### Phase 4: VALIDATE -- Verify Configuration Correctness1501511. **Validate Dockerfile syntax.** Run `docker build --check` or parse for common errors:152 - Invalid instruction ordering (e.g., CMD before COPY)153 - Missing required arguments154 - Deprecated instructions (MAINTAINER)155 - Shell form vs. exec form for CMD/ENTRYPOINT1561572. **Validate Kubernetes manifests.** Check for:158 - Valid YAML structure159 - Required fields present (apiVersion, kind, metadata, spec)160 - Label selectors match between Deployment and Service161 - Port numbers are consistent across Service and container specs162 - ConfigMap and Secret references resolve to existing resources1631643. **Validate Helm charts.** If Helm is used:165 - `helm lint` passes166 - Template rendering with default values produces valid manifests167 - Values schema matches actual usage in templates168 - Dependencies are declared and version-locked1691704. **Validate docker-compose.** Check for:171 - Valid YAML and compose file version172 - All referenced images exist or have build contexts173 - Port mappings do not conflict174 - Named volumes are declared in the top-level volumes section175 - Networks are declared before use1761775. **Generate validation report:**178179 ```180 Container Validation: [PASS/WARN/FAIL]181182 Dockerfiles: PASS (2/2 valid)183 K8s manifests: WARN (label mismatch in worker-service.yaml)184 Helm chart: PASS (lint clean)185 Compose: PASS (valid structure)186187 Issues:188 1. k8s/worker-service.yaml: selector "app: worker" does not match189 deployment label "app: worker-v2" -- requests will not route190 ```191192---193194## Harness Integration195196- **`harness skill run harness-containerization`** -- Primary invocation for container review.197- **`harness validate`** -- Run after configuration changes to verify project health.198- **`harness check-deps`** -- Verify container tooling dependencies are available.199- **`emit_interaction`** -- Present optimization recommendations and gather decisions.200201## Success Criteria202203- All container configuration files in the project are discovered and cataloged204- Dockerfiles are analyzed for layer efficiency, security, and size205- Kubernetes manifests are validated for correctness and best practices206- Resource requests and limits are verified for all production workloads207- Image tag strategy is evaluated (no `latest` in production)208- Optimization recommendations include estimated impact209210## Examples211212### Example: Node.js Monorepo with Docker and Kubernetes213214```215Phase 1: SCAN216 Found: Dockerfile (app), Dockerfile.worker, docker-compose.dev.yml217 K8s: 12 manifests in k8s/ (2 Deployments, 2 Services, 2 ConfigMaps,218 2 HPA, 2 Ingress, 2 PDB)219 Base images: node:20 (not alpine), node:20 (worker)220221Phase 2: ANALYZE222 Dockerfile issues:223 - node:20 full image (940MB) -- use node:20-alpine (180MB)224 - No .dockerignore -- node_modules and .git copied into image225 - No USER directive -- running as root226 - No HEALTHCHECK227 K8s issues:228 - worker deployment missing memory limits229 - No network policies defined230 - Liveness probe on /healthz but no readiness probe231232Phase 3: OPTIMIZE233 1. Switch to node:20-alpine -- saves ~760MB per image234 2. Add .dockerignore with node_modules, .git, tests, docs235 3. Add multi-stage build: builder stage for npm ci, runtime for app236 4. Add USER node (UID 1000) after COPY237 5. Add readiness probe on /ready endpoint238 6. Add memory limit of 512Mi to worker deployment239240Phase 4: VALIDATE241 Dockerfiles: WARN (2 security findings, 1 size finding)242 K8s manifests: WARN (missing limits, missing readiness probe)243 Compose: PASS244 Result: WARN -- 6 actionable improvements identified245```246247### Example: Python FastAPI with Helm and Distroless248249```250Phase 1: SCAN251 Found: Dockerfile (multi-stage with distroless runtime)252 Helm chart: charts/api/ with values.yaml253 Base images: python:3.12-slim (builder), gcr.io/distroless/python3 (runtime)254255Phase 2: ANALYZE256 Dockerfile: Well-structured multi-stage build257 - Builder installs dependencies, runtime copies only venv258 - Distroless base (no shell, minimal attack surface)259 - Non-root user configured260 Helm:261 - Resource limits set in values.yaml262 - Probes configured with appropriate timeouts263 - HPA configured for 2-10 replicas264265Phase 3: OPTIMIZE266 Minor recommendations only:267 - Pin distroless image to digest for reproducibility268 - Add --mount=type=cache for pip downloads in builder stage269 - Add pod anti-affinity to spread replicas across nodes270271Phase 4: VALIDATE272 Dockerfile: PASS273 Helm lint: PASS274 Template render: PASS (all values resolve)275 Result: PASS -- well-configured container setup276```277278## Rationalizations to Reject279280| Rationalization | Reality |281| ------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |282| "We use `latest` in production because we always want the most recent image" | `latest` is a mutable pointer. A deployment at 9am and a rollback at 9pm may pull different image contents with the same tag. Immutable tags (semver or digest) are required for reproducible deployments and reliable rollbacks. |283| "The container runs as root because it needs to bind to port 80" | Binding to a privileged port requires elevated capability, not full root access. The correct solution is to run the container as a non-root user and use `--cap-add NET_BIND_SERVICE`, or to expose a high port and use a load balancer or ingress for port translation. |284| "We don't set resource limits because we want the container to use whatever it needs" | Containers without memory limits can exhaust node memory, triggering OOM kills of other containers on the same node. Kubernetes uses resource requests for scheduling and limits for safety; omitting limits transfers the risk of one container onto the entire node. |285| "Our image is 2GB but it only takes a few seconds to pull in our CI" | Image size multiplies across every developer pull, every CI run, and every Kubernetes pod startup. A 2GB image that takes 3 seconds to pull in CI with a warm cache takes 90 seconds on a cold node during an autoscaling event at peak traffic. |286| "We don't need liveness and readiness probes — the container exits if it crashes" | Process exit is a coarse health signal. A process that is running but deadlocked, stuck in an infinite retry loop, or unable to connect to its database will never exit. Kubernetes will continue routing traffic to it. Readiness probes prevent traffic routing to unhealthy containers that are still running. |287288## Gates289290- **No `latest` tag in production manifests.** Production Kubernetes manifests or compose files using `latest` image tags are blocking findings. Immutable tags or digests are required.291- **No containers running as root in production.** Missing USER directive in Dockerfiles or missing security context in K8s pods targeting production are blocking findings.292- **No missing resource limits in production.** Kubernetes Deployments without CPU and memory limits are blocking warnings for production namespaces.293- **No invalid manifest references.** Label selector mismatches between Services and Deployments, or ConfigMap/Secret references to nonexistent resources, are blocking errors.294295## Escalation296297- **When base images have known CVEs:** Flag the specific CVEs and recommend upgrading to a patched version. If no patched version exists, recommend an alternative base image and document the migration path.298- **When Kubernetes manifest complexity exceeds review scope:** For clusters with 50+ resources, recommend focusing on changed resources only (`--changed-only` flag) and scheduling a full audit separately.299- **When Helm chart dependencies are outdated:** Report the version gap and recommend updating. If the update includes breaking changes, flag it as a decision point and present the changelog.300- **When docker-compose is used for production:** Flag this as an architectural concern. Docker Compose is appropriate for development but production workloads should use an orchestrator (Kubernetes, ECS, Cloud Run). Present migration options.