How to Do Dockerfile Creation on Claude Code (Step-by-Step)
In this article
- What Claude Code actually does when you ask it to create a Dockerfile
- How to create a Dockerfile with Claude Code: step by step
- What kinds of Dockerfiles can Claude Code generate?
- Useful prompts for Dockerfile creation on Claude Code
- How to use slash commands during Dockerfile work
- Why usage limits matter when creating Dockerfiles
- Key takeaways
Claude Code can generate production-ready Dockerfiles from a single natural-language prompt in your terminal. Tell it what language, runtime, and environment you need, and it writes a Dockerfile — including multi-stage builds, non-root user setup, and layer caching optimizations — without you leaving your editor context. This guide is for developers who want to containerize apps faster without hand-writing boilerplate. The main trade-off: complex multi-service setups may need a few clarifying rounds, and larger Dockerfile generation tasks consume meaningful Claude Code usage tokens.
- Claude Code understands Docker best practices out of the box (COPY vs ADD, .dockerignore, layer ordering).
- Multi-stage builds for compiled languages (Go, Rust, Java) are generated correctly in most cases on the first attempt.
- Dockerfile creation is a token-moderate task — enough to matter if you're near your Claude Code usage limit.
What Claude Code actually does when you ask it to create a Dockerfile
Claude Code operates as an agentic coding assistant directly in your terminal. When you ask it to create a Dockerfile, it reads your project structure first — checking for package manifests (package.json, requirements.txt, go.mod, etc.) to infer the correct base image, dependency installation steps, and entrypoint. It then writes the file directly to your project root.
This means you don't need to specify every detail. Claude Code infers context from your codebase, similar to how a senior engineer would approach containerization. According to Claude Code's official documentation, the tool can read, write, and edit files as part of multi-step agentic workflows — Dockerfile creation fits naturally into that capability.
How to create a Dockerfile with Claude Code: step by step
Step 1: Open Claude Code in your project directory
Navigate to your project root and start Claude Code:
cd your-project
claude
Step 2: Prompt Claude Code to generate the Dockerfile
Use a direct, specific prompt. The more context you give, the better the first-pass result:
Create a Dockerfile for this Node.js app. Use a multi-stage build,
a non-root user, and expose port 3000. Optimize for layer caching.
Or for a Python application:
Write a Dockerfile for this FastAPI app using Python 3.12-slim.
Include a .dockerignore file and a health check endpoint.
Claude Code will read your project files, then write the Dockerfile and any supporting files directly to disk.
Step 3: Review and iterate
Claude Code explains its choices inline. If you want changes — a different base image, Alpine instead of Debian slim, or a specific build argument — just follow up in the same session:
Switch the base image to node:20-alpine and add ARG support
for BUILD_ENV so we can pass environment variables at build time.
Step 4: Build and test
Once the Dockerfile is written, ask Claude Code to run a build to validate it:
Run docker build -t my-app . and show me any errors.
Claude Code will execute the command in your shell, read the output, and propose fixes if the build fails — all without you switching context.
What kinds of Dockerfiles can Claude Code generate?
| Project Type | What Claude Code generates | Notes |
|---|---|---|
| Node.js / Next.js | Multi-stage build, npm ci, non-root user | Handles monorepos if you specify the workspace |
| Python / FastAPI / Django | python-slim base, pip install, gunicorn entrypoint | Suggests virtualenv inside container for isolation |
| Go | Two-stage build (builder + scratch), minimal final image | Final image can be under 15MB |
| Java / Spring Boot | Maven/Gradle build stage + JRE runtime stage | Specify JDK version explicitly in prompt |
| Rust | Multi-stage with cargo build --release | Use rust:slim as builder to reduce build time |
For more complex infrastructure — multi-container setups with databases, caches, and reverse proxies — Claude Code can also generate a docker-compose.yml. See the guide on how to do Docker container setup on Claude Code for that workflow.
Useful prompts for Dockerfile creation on Claude Code
Here are battle-tested prompts you can adapt directly:
- Security-hardened Dockerfile: "Create a Dockerfile for this Express app. Run as a non-root user, drop all Linux capabilities except NET_BIND_SERVICE, and scan for obvious vulnerabilities."
- CI/CD-optimized: "Write a Dockerfile that layers dependencies separately from source code to maximize Docker layer cache hits in GitHub Actions."
- Environment-specific builds: "Create a Dockerfile with separate dev and prod stages. Dev should include hot reload with nodemon; prod should be minimal."
- Distroless final image: "Use a Google Distroless base image for the final stage to reduce attack surface. The app is a compiled Go binary."
How to use slash commands during Dockerfile work
Claude Code's slash commands are useful when working on container configuration:
- /clear — Reset context between unrelated containerization tasks to avoid context bleed.
- /usage — Check how much of your Claude Code usage you've consumed mid-session. Helpful before starting a large multi-file Docker setup.
- /review — Ask Claude Code to review the Dockerfile it just wrote against Docker best practices before you commit.
A full list of available commands is documented in the Claude Code slash commands list.
Why usage limits matter when creating Dockerfiles
Dockerfile creation on its own is a moderate-token task. The problem surfaces when you're mid-workflow: Claude Code is iterating on a multi-stage build, debugging a failed docker build, writing a .dockerignore, and adjusting a docker-compose.yml — and then you hit your usage cap. A five-hour lockout mid-PR is the worst time to find out you're out of tokens.
This is the exact scenario Usagebar is built to prevent. It sits in your macOS menu bar and shows your Claude Code usage in real time, with smart alerts at 50%, 75%, and 90% of your limit so you can pace heavier agentic tasks accordingly. Credentials are stored securely in macOS Keychain — no account required, and there's a free option for students.
You can also check current usage with the /usage command inside Claude Code, or at claude.ai/settings/usage — but neither gives you proactive alerts before you hit a wall. Knowing exactly when your usage window resets means you can plan a Dockerfile refactor session without getting cut off at a critical moment.
Get Usagebar — instant download, pay what you want (including free for students).
Key takeaways
- Start Claude Code in your project root so it can read existing config files and infer the right base image automatically.
- Be specific in your prompt: mention multi-stage builds, target runtime, exposed ports, and security requirements upfront to minimize back-and-forth.
- Use
/usageor the usage statistics command before starting a large containerization session to confirm you have headroom. - For iterative Docker work (build, fix, rebuild), ask Claude Code to run
docker builddirectly so it can self-correct from error output. - Monitor usage proactively with Usagebar to avoid a mid-task lockout — especially when containerizing a complex microservices project.
Never Get Locked Out Mid-Task Again
Never hit your usage limits unexpectedly. Usagebar lives in your menu bar and shows your 5-hour and weekly limits at a glance.
Get Usagebar$9 — one-time, lifetime updates