Skip to content

Docker and Kubernetes Setup via CLI

You are containerizing your application for the first time. The Dockerfile tutorial gets you a working image in ten minutes — 1.2 GB, running as root, with no health check, no signal handling, and a build cache that invalidates on every code change. The Kubernetes tutorial gets you a pod running — with no resource limits, no readiness probe, no pod disruption budget, and secrets hardcoded in the manifest. Getting from “it works” to “it is production-ready” takes another two days of research. Claude Code collapses that into a single, well-structured conversation.

  • A Claude Code workflow for generating optimized, secure Dockerfiles and multi-service Docker Compose stacks from your existing project
  • Copy-paste prompts that produce Kubernetes manifests with proper health checks, resource limits, autoscaling, and security contexts
  • A Helm chart generation approach that creates parameterized, environment-specific deployments

Production Dockerfiles from Project Analysis

Section titled “Production Dockerfiles from Project Analysis”

Claude Code reads your project and generates a Dockerfile that fits — not a generic template.

Claude Code detects whether you are building a Node.js, Python, Go, or multi-language project and generates accordingly. For a Node.js app, it separates the npm ci layer from the COPY . . layer so that dependency installation is cached unless package-lock.json changes. For a Go app, it produces a single static binary in a FROM scratch final stage.

After generation, validate the image:

Terminal window
# Build and check the image size
docker build -t myapp:test . && docker images myapp:test
# Run security scan
docker scout cve myapp:test
# Verify the health check works
docker run -d --name test myapp:test && sleep 5 && docker inspect --format='{{.State.Health.Status}}' test

Then iterate:

The image is 280 MB because it includes the entire node_modules. Switch to a production prune step that removes devDependencies after building, and copy only node_modules and dist to the runtime stage.

Local development should mirror production as closely as possible. Claude Code generates a Docker Compose stack that includes all your dependencies.

The health check with condition: service_healthy on the depends_on is critical. Without it, your application container starts before PostgreSQL is ready to accept connections, causing startup crashes.

For teams with multiple services:

Extend the Docker Compose to include our microservices: api-gateway (port 3000), user-service (port 3001), order-service (port 3002), and notification-service (port 3003). Each service has its own Dockerfile. Add a shared network, service-to-service communication via container names, and a single command to start the full stack. Also add an Nginx reverse proxy that routes /api/users/* to user-service, /api/orders/* to order-service, etc.

Kubernetes YAML is verbose and error-prone. Claude Code generates complete, production-ready manifests.

Generate Kubernetes manifests for deploying our application. Include: 1) Deployment with 3 replicas, rolling update strategy (maxSurge 1, maxUnavailable 0), pod anti-affinity to spread across nodes, 2) Service (ClusterIP) pointing to port 3000, 3) Ingress with TLS via cert-manager and rate limiting annotations, 4) ConfigMap for non-secret configuration (LOG_LEVEL, CACHE_TTL), 5) Secret references for DATABASE_URL and API keys (reference external secrets, do not hardcode values), 6) HorizontalPodAutoscaler scaling 3-10 replicas based on CPU (70%) and memory (80%), with scale-down stabilization of 5 minutes, 7) PodDisruptionBudget (minAvailable: 2), 8) resource requests (256Mi memory, 250m CPU) and limits (512Mi memory, 500m CPU), 9) liveness probe on /health, readiness probe on /ready, startup probe with 30-second failure threshold. Set security context to non-root, read-only filesystem, no privilege escalation.

Helm charts turn your Kubernetes manifests into parameterized, reusable packages.

After generation, validate the chart:

Terminal window
# Lint the chart
helm lint ./chart
# Render templates without deploying (dry run)
helm template myapp ./chart -f chart/values-staging.yaml
# Deploy to staging
helm upgrade --install myapp ./chart -f chart/values-staging.yaml -n staging

Container security is not optional. Claude Code can harden your containers from the start.

Review our Dockerfile and Kubernetes manifests for security issues. Check: 1) running as root (should be non-root), 2) using latest tag (should pin a specific version), 3) missing security context (should set readOnlyRootFilesystem, allowPrivilegeEscalation: false), 4) secrets in environment variables (should use Kubernetes Secrets or an external secret manager), 5) missing network policies (should restrict pod-to-pod communication), 6) container image from untrusted registry, 7) capabilities that should be dropped. Fix every issue and generate a NetworkPolicy that allows only the required traffic.

If your team uses both Intel and ARM machines (Mac M-series), you need multi-architecture Docker images.

Update our Dockerfile and CI pipeline to build multi-architecture images (linux/amd64 and linux/arm64). Use docker buildx with GitHub Actions cache. The build step should produce a single manifest that Docker automatically resolves to the correct architecture. Also update our docker-compose.yml to use platform: linux/amd64 for services that do not support ARM (like some database images).

The Docker build is slow because the cache invalidates on every change. Layer ordering matters. The COPY package*.json . must come before COPY . . so that code changes do not invalidate the dependency cache. Ask Claude Code: “Reorder our Dockerfile layers to maximize cache reuse. The dependency installation layer should only rebuild when the lockfile changes.”

Kubernetes pods crash-loop with OOMKilled. The memory limit is too low for your application’s actual usage. Ask Claude Code: “Our pods are getting OOMKilled with a 512Mi limit. Analyze our application’s memory usage patterns and recommend appropriate resource requests and limits. Also add a memory monitoring sidecar that exports usage to Prometheus.”

The Helm chart works in staging but fails in production. Usually a missing value or a hardcoded reference. Ask Claude Code: “Compare our values-staging.yaml and values-production.yaml with the template references. Find any template variable that is used but not defined in one of the values files.”

Docker Compose volumes have permission issues. Files created inside the container are owned by root, but your host user cannot edit them. The non-root user inside the container and your host user have different UIDs. Ask Claude Code: “Fix the volume permission issue by making the container user UID match our host user UID. Add a build arg for USER_UID that defaults to 1000.”

The health check passes but the app is not actually ready. A /health endpoint that just returns 200 does not prove the app can serve traffic. Ask Claude Code: “Update our readiness probe endpoint to check: database connection pool has available connections, Redis is reachable, and the last successful background job ran within the last 5 minutes.”