docker-init
Generate a Dockerfile and .dockerignore tailored to the current project.
When to use
- When the user asks to create or initialize a Dockerfile
- When the user wants to containerize their project
- When the user runs
/docker-init
Instructions
-
Detect the project type by scanning the repository for key files:
package.json/package-lock.json/yarn.lock/pnpm-lock.yaml/bun.lockb→ Node.jsgo.mod→ GoCargo.toml→ Rustpyproject.toml/requirements.txt/Pipfile/setup.py→ PythonGemfile→ Ruby*.csproj/*.sln→ .NETbuild.gradle/pom.xml→ Java/Kotlinmix.exs→ Elixirdeno.json/deno.jsonc→ Deno
-
Detect the package manager and framework from lock files and config:
- Node.js: npm / yarn / pnpm / bun; Next.js / Remix / Astro / Express, etc.
- Python: pip / poetry / uv / pipenv
- Others: detect from project config files
-
Determine the entrypoint:
- Check
mainorscripts.startinpackage.json - Check
[package]inCargo.toml - Check
moduleingo.mod - Check
CMDhints from framework conventions
- Check
-
Generate the Dockerfile with these best practices:
- Use a multi-stage build (build stage + runtime stage)
- Build stage: use Debian-based official SDK/build images (e.g.,
node:22-bookworm,golang:1.23-bookworm,rust:1.83-bookworm) - When using
apt-get installin the build stage, always clean up the apt cache. Use this pattern:RUN apt-get update && apt-get install -y --no-install-recommends \ <packages> \ && rm -rf /var/lib/apt/lists/* - Runtime stage: use
gcr.io/distroless/*-debian12:nonrootimages as the base. Choose the appropriate variant:- Go:
gcr.io/distroless/static-debian12:nonroot - Rust (statically linked):
gcr.io/distroless/static-debian12:nonroot - Rust (dynamically linked):
gcr.io/distroless/cc-debian12:nonroot - Node.js:
gcr.io/distroless/nodejs22-debian12:nonroot - Python:
gcr.io/distroless/python3-debian12:nonroot - Java:
gcr.io/distroless/java21-debian12:nonroot - .NET:
gcr.io/distroless/dotnet8-debian12:nonroot(match the .NET version)
- Go:
- Always use the latest stable version of the language/runtime for base images. Check the project config (e.g.,
go.mod,Cargo.toml,package.json engines,.python-version) to determine the version, and default to the latest stable if unspecified. - When downloading binaries via
curlorwget, always verify the checksum (e.g.,sha256sum --check) before using them. - Pin image tags to full semver patch versions (e.g.,
node:22.12.0-bookworm,golang:1.23.4-bookworm) instead of usinglatest, major-only, or minor-only tags. This ensures reproducible builds. - Prefer copying CLI tools from official Docker images using
COPY --from=rather than downloading them withcurl. For example:COPY --from=aquasec/trivy:0.58.2 /usr/local/bin/trivy /usr/local/bin/trivy - Copy dependency files first and install dependencies before copying source code to leverage Docker layer caching
- Set appropriate
EXPOSEport if detectable - Do NOT add
HEALTHCHECK(distroless images do not have a shell or curl)
-
Generate
.dockerignoreappropriate for the detected project type. Common exclusions:.git,.github,.gitignorenode_modules,__pycache__,target,dist(build artifacts)*.md,LICENSE,docs/.env,.env.*Dockerfile,docker-compose*.yml- IDE/editor files (
.vscode,.idea)
-
If the project type cannot be determined, ask the user what language/framework they are using before generating files.
-
Do not overwrite existing
Dockerfileor.dockerignorewithout confirming with the user first.