---
name: dockerfile-builder
description: "A small, secure, cache-friendly Dockerfile for your language and package manager, with the dependency and user choices explained in comments. Use when a developer needs a new Dockerfile for a Node, Python, Go, Rust, Java, or Ruby service that does not have one yet. Do not use for reviewing or shrinking an existing Dockerfile or Kubernetes manifests. Related topics: devops, coding, docker."
---

# Dockerfile Builder

A small, secure, cache-friendly Dockerfile for your language and package manager, with the dependency and user choices explained in comments.

## When to use

Use this skill when a developer needs a new Dockerfile for a Node, Python, Go, Rust, Java, or Ruby service that does not have one yet. Do not use it for reviewing or shrinking an existing Dockerfile or Kubernetes manifests.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Choose a pinned base image for the language and the OS preference, and note any musl risk.
3. Order the layers so the dependency manifest is copied and installed before the source.
4. Apply the multi-stage, non-root, and environment choices, done when the final stage holds only runtime files.
5. Install system dependencies in one cleaned layer and write the matching .dockerignore.
6. Give the build and run commands with the exposed port.

## Inputs

- **Language and framework** (choose one: Node.js, Python with Flask, Python with Django, Go, Rust, Java, Ruby on Rails)
- **Package manager** (choose one: npm, yarn, pip, go mod, cargo, maven)
- **Application port** (number from 1 to 65535)
- **Multi-stage build** (choose one: Yes, No)
- **Base OS preference** (choose one: Alpine, Debian slim, Ubuntu, Default for the language)
- **System dependencies** (longer free text)
- **Optimisations** (choose any: Run as non-root user, Cache dependencies, Set environment variables)

## Output

A commented Dockerfile, a .dockerignore, and the build and run commands.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Dockerfile From Scratch

Write a Dockerfile for the project below.

- Language and framework: {{Language and framework}}
- Package manager: {{Package manager}}
- Application port: {{Application port}}
- Multi-stage build: {{Multi-stage build}}
- Base OS preference: {{Base OS preference}}
- System dependencies: {{System dependencies}}
- Optimisations: {{Optimisations}}

Write a complete, commented Dockerfile. Copy the dependency manifest and install before copying source, so the install layer is cached. Pin the base image to a version. With multi-stage on, build in one stage and copy only the artifact into a minimal final stage. Install system packages in a single layer and clean the package cache. Add a .dockerignore list, expose the port, and set a non-root user if chosen. Warn when Alpine could break a native dependency, since musl differs from glibc. Finish with the build and run commands.

## Pitfalls

- Copying all source before installing dependencies, which rebuilds them on every code change.
- Choosing Alpine for an app with native libraries that expect glibc.
- Leaving the container running as root with build tools in the final image.
