Developer and Text Tools

How to Write a Good Dockerfile (and Turn docker run into Compose)

What makes a Dockerfile fast, small and safe: layer caching, multi-stage builds, non-root users, health checks, and converting docker run to compose.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
How to Write a Good Dockerfile (and Turn docker run into Compose)

How to Write a Good Dockerfile (and Turn docker run into Compose)

Almost any Dockerfile will produce an image that runs. A good one builds in seconds after a code change, produces an image a fraction of the size, runs without root privileges and tells the orchestrator when the app is healthy. The free Dockerfile and Docker Compose Generator writes that kind of file for ten common stacks. Here is what it does and why.

1. Order layers for caching

Docker caches each instruction's result and reuses it until something it depends on changes. Copy the dependency manifests first, install, and only then copy the source:

COPY package.json package-lock.json ./ RUN npm ci COPY . .

Now editing a source file only invalidates the last layer, and the slow dependency install is reused. Copy everything first and every code change reinstalls all dependencies.

2. Use a multi-stage build

Compilers, dev dependencies and build caches are needed to build, not to run. A multi-stage build compiles in one stage and copies only the result into a clean, small runtime image. For Go, the final image can be a distroless base of a few megabytes holding just the binary; for Java, a JRE rather than a full JDK; for a React site, Nginx serving the built files. Smaller images pull faster and contain fewer packages to patch.

3. Do not run as root

By default a container's process runs as root inside the container. If the app is compromised, root inside a container is a much better starting point for an attacker. Create an unprivileged user and switch to it with USER, or use the non-root user that distroless images provide.

4. Add a health check

A HEALTHCHECK lets Docker and Compose know whether the app actually works, not just whether the process is alive. Compose can then wait for a database to be healthy before starting the app. Distroless images have no shell or curl, so use your orchestrator's probes there instead.

5. Use exec-form commands

CMD ["node", "server.js"] runs your process directly as process 1, so it receives the stop signal and can shut down cleanly. The shell form, CMD node server.js, wraps it in a shell that may not forward signals, making stops slow and abrupt.

6. Keep secrets out

Anything set with ENV or copied into the image is visible to anyone who can pull it, including in the layer history. Pass secrets at run time, through an environment file kept out of Git, or with Docker secrets. A .dockerignore stops local .env files and the .git folder being sent to the build at all.

Using the generator

Pick your stack, set the port, start command, health path and environment variables, and choose whether you want a multi-stage build and a non-root user. You get the Dockerfile, a matching .dockerignore and, if you like, a compose.yaml with an optional PostgreSQL service that the app waits for.

From docker run to compose.yaml

README files are full of long docker run commands. Compose files are easier to read, keep in Git and run together. Switch to the compose mode and paste one or more commands. Each becomes a service: -p becomes ports, -v volumes, -e environment, --restart restart, --memory mem_limit, the health options a healthcheck block, and anything after the image becomes the command. Named volumes are declared at the end, and any option the converter does not recognise is listed so nothing is silently dropped. Start everything with docker compose up -d.

Checking your image

After building, docker image ls shows the size and docker image history shows which layer is large. Run the container and check docker ps for the health status. Rebuild after changing one source file and confirm the dependency layer comes from the cache.

Everything is generated in your browser. Adjust paths and commands to your project's layout, and keep base images up to date for security fixes.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages