CFG-02 · Containers

Dockerfile generator

Base image, working directory, copy and run steps, exposed ports — with an optional multi-stage build for a smaller final image.

Build stage

Copied into the final stage at the same path under WORKDIR.

Dockerfile


  

Validate locally with docker build . before relying on this — a generator can't know your project's actual file layout.

Why layer order matters for build speed, not just correctness

Docker caches each instruction as a layer and reuses it unless that instruction or anything above it changed — copying dependency manifests (package.json, requirements.txt) and installing them before copying the rest of your source means a source-only change skips the slow install step entirely on the next build. Copying everything in one step first is the single most common reason a Dockerfile rebuilds slower than it needs to.

What multi-stage builds actually buy you

A multi-stage build compiles or bundles your app in one image (with the full toolchain: compilers, dev dependencies, build cache) and then copies only the finished output into a clean, minimal final image — the build tools never ship in the image that actually runs in production. That's smaller attack surface and a smaller image, at the cost of one extra FROM line.

Why alpine base images instead of the full image?

Alpine-based images are dramatically smaller (tens of MB instead of hundreds) because they use musl libc and a minimal package set instead of a full Debian/Ubuntu userland. The tradeoff is occasional compatibility issues with native dependencies compiled against glibc — if a build fails mysteriously only on the alpine tag, that's usually why.

Do I need a .dockerignore file too?

Yes, in almost every real project — without one, COPY . . sends your entire working directory (including node_modules, .git, local env files) into the build context, which slows builds and can leak secrets into the image. It isn't generated here since its contents are project-specific, but it belongs next to this Dockerfile.

What's the difference between CMD and ENTRYPOINT?

CMD sets the default command and is easy to override at docker run time; ENTRYPOINT is fixed and harder to override, typically used when the container should always run as a specific executable. This generator uses CMD, the right default for most application containers.