Last reviewed:
What is a Dockerfile?
A Dockerfile is a text file of instructions for building a container image. Each instruction runs in order: FROM picks a base image, WORKDIR sets the working directory, COPY adds files from the build context (the folder you build from), RUN executes commands such as installing dependencies, ENV sets environment variables, USER chooses the account the app runs as, EXPOSE documents the port, and CMD or ENTRYPOINT defines what starts when the container runs. Most instructions create a layer, and Docker reuses cached layers when their inputs have not changed.
Writing a Dockerfile that works is easy. Writing a good one takes experience. Ordering matters for caching: copy the dependency manifest and install packages before copying your source code, so that changing one source file does not reinstall every dependency. Multi-stage builds compile the app in one image full of build tools and copy only the result into a small runtime image. Running as a non-root user limits the damage if the app is compromised. A .dockerignore file keeps node_modules, .git and secrets such as .env out of the build context and therefore out of the image.
This generator writes Dockerfiles that follow those practices for six common stacks: Node.js, Python, Java with Maven, Java with Gradle, Go, and static sites served by nginx. It also produces a matching .dockerignore.
How to use this tool
- Choose a stack: Node.js, Python, Java (Maven), Java (Gradle), Go or Static site (nginx). Sensible defaults fill the form.
- Set the runtime Version (for example Node 22, Python 3.12, Java 21 or Go 1.23) and the Port your app listens on.
- For Node.js, choose npm, yarn or pnpm, and set the Start command. Python also takes a start command; Go takes the Main package path, such as
./cmd/server. - For Node.js and static sites, decide whether to Run a build step first and where the build output goes (
distby default). - Enter a Healthcheck path to add a
HEALTHCHECK, or clear it for none. Keep Run as a non-root user ticked unless you have a reason not to. - Copy or download the Dockerfile and .dockerignore, put them in your project root, and build with
docker build -t myapp ..
Examples
Node.js with a build step (npm)
# syntax=docker/dockerfile:1
# Build stage
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
# Runtime stage
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -qO- http://localhost:3000/health > /dev/null || exit 1
CMD ["node","dist/server.js"]
The build stage installs all dependencies, including dev tools such as TypeScript, and builds the app. The runtime stage installs production dependencies only and copies in the built dist folder, so compilers and dev packages never reach the final image. USER node uses the unprivileged account that the official Node images include.
Go with a static binary
With the main package set to ./cmd/server:
# syntax=docker/dockerfile:1
# Build stage
FROM golang:1.23-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /out/app ./cmd/server
# Runtime stage
FROM alpine:3.20
RUN apk add --no-cache ca-certificates
RUN addgroup -S app && adduser -S app -G app
COPY --from=build /out/app /usr/local/bin/app
USER app
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -qO- http://localhost:8080/healthz > /dev/null || exit 1
CMD ["app"]
CGO_ENABLED=0 produces a statically linked binary that runs on Alpine without the Go toolchain, -s -w strips debug symbols to make it smaller, and ca-certificates lets the app make HTTPS calls.
The Node.js .dockerignore
.git
.gitignore
.dockerignore
Dockerfile*
docker-compose*.yml
.env
.env.*
*.log
.DS_Store
.vscode
.idea
node_modules
npm-debug.log*
coverage
dist
.next
build
dist, .next and build are excluded because the image builds them itself. With the build step turned off, those lines are left out so your prebuilt output is copied.
Common errors and how to fix them
npm cifails because the lockfile is missing or out of sync.npm cionly installs from apackage-lock.jsonthat matchespackage.json. Runnpm installlocally, commitpackage-lock.json, and rebuild. The same applies toyarn.lockandpnpm-lock.yaml.- A
COPYstep fails with "not found". BuildKit reports this asfailed to compute cache key: … not found. The file is missing from the build context or excluded by.dockerignore. Check that you rundocker buildfrom the project root and that the path is not ignored. - The app is unreachable on the published port. Inside a container the app must listen on
0.0.0.0, not127.0.0.1orlocalhost. The Python defaultgunicorn -b 0.0.0.0:8000does this; check your framework's host setting. - "EACCES: permission denied" or "Permission denied" at runtime. The non-root user cannot write to a folder owned by root. Write to
/tmpor a mounted volume, orchownthe folder in the Dockerfile beforeUSER. - The health check always fails. The path must return a 2xx status code on the port inside the container. Check the path, and give slow-starting apps such as Spring Boot more time by increasing
--start-period. - Changing one source file reinstalls every dependency. Keep the order the generator uses: copy the manifest and lockfile, install, and only then
COPY . ..
When developers use this
- Containerising an existing service for the first time, starting from a known-good structure.
- Replacing a large single-stage image with a multi-stage build to cut image size and attack surface.
- Adding a non-root user and a health check to satisfy a security scan or platform requirement.
- Creating a
.dockerignorefor a project that was accidentally shipping.gitor.envin its image. - Serving a React, Vue or other front-end build output from nginx.
Privacy and how it works
The Dockerfile and .dockerignore are assembled from templates in your browser as you change the form. Nothing is uploaded, and the tool does not inspect your project. That means it cannot see your files, but it also cannot know your exact build commands, so review the output before using it.
Limitations
- The templates assume common project layouts:
requirements.txtfor Python, a Spring BootbootJartask for Gradle, abuildscript for Node.js, and a single runnable JAR intargetfor Maven. Adjust them for other setups. - The yarn commands use Yarn Classic (v1) flags. Yarn 2 and later use
yarn install --immutableandyarn workspaces focus --productioninstead. HEALTHCHECKis used by Docker and Docker Compose. Kubernetes ignores it and uses liveness and readiness probes instead.- Image tags such as
node:22-alpinemove as new patch versions are released. Pin a digest if you need fully reproducible builds.
FAQ
Why use a multi-stage build?
The final image only contains what the app needs to run. Compilers, build tools and source code stay in the build stage, which typically makes images several times smaller and removes tools an attacker could use.
Why Alpine for Node.js, Java and Go but slim for Python?
Alpine uses musl instead of glibc. Many Python packages ship prebuilt wheels for glibc, which install quickly on Debian-based slim images. On Alpine they can fall back to compiling from source. Node.js, Java and statically linked Go binaries work well on Alpine.
What is the difference between CMD and ENTRYPOINT?
CMD sets the default command, which docker run image other-command replaces. ENTRYPOINT sets a fixed executable, and arguments given to docker run are appended to it. The Java template uses ENTRYPOINT so extra JVM or app arguments can be added at run time.
Why is the exec form ["node","server.js"] used?
The JSON array form runs the program directly as process 1, without a shell. It then receives signals such as SIGTERM from docker stop and can shut down gracefully. With the shell form, the signal goes to /bin/sh instead.
Why does the static site use port 8080 with a non-root user?
Ports below 1024 need root privileges by default. The non-root option uses the nginxinc/nginx-unprivileged image, which listens on 8080. Untick the option to use nginx:alpine on port 80.
What does -XX:MaxRAMPercentage=75 do?
It lets the JVM use up to 75% of the container's memory limit for the heap. That leaves room for thread stacks, metaspace and native memory, so the container is not killed for exceeding its memory limit.
Do I still need the # syntax=docker/dockerfile:1 line?
It is optional but recommended. It tells BuildKit to use the latest stable Dockerfile syntax, so features such as cache mounts and heredocs work regardless of the Docker version installed.