Topic 4 of 5 · 5 min read
Dockerfiles and container configuration
Goal: Review an AI-generated Dockerfile and fix the lines that run as root, leak secrets or break reproducible builds.
A Dockerfile is a recipe whose result is shipped to production: read it for who the process runs as, what is baked into the image and whether it builds the same way twice.
Check yourself · 1 of 3
A Dockerfile has no USER instruction. What does that mean at runtime?
Why it matters
Agents write Dockerfiles that run on the first try, using habits from thousands of tutorials: FROM node:latest, COPY . ., run as root. These work on a laptop. In production they bring security and repeatability problems that no test catches.
What to check
- Floating tags.
latestor a bare major tag changes without your commit. Pin a specific version, and for strict builds pin the image digest. Docker's build best-practices page covers pinning. - User. If there is no
USERline, the process runs as root. A bug in the app then becomes a root bug in the container. Create and switch to a non-root user. - Secrets in layers.
COPY .env .,ARG NPM_TOKENorENV API_KEY=...can leave the secret in the image history, even if a later line deletes it. Use build secrets or pass values at runtime. - Build context.
COPY . .without a.dockerignorepulls in.git,.envandnode_modules. - Image size and layers. Use a multi-stage build so compilers and dev dependencies do not ship. Smaller images start faster and have less to attack.
- Cache order. Copy dependency files and install before copying source, so edits do not reinstall everything.
- Health and signals. A health check or probe, and a start command that handles shutdown signals.
Before and after
FROM node:latest
WORKDIR /app
COPY . .
ENV DATABASE_URL=postgres://admin:hunter2@db/prod
RUN npm install
CMD ["node", "server.js"]
Four problems: a moving tag, a password stored in the image, a whole-directory copy and root. The fix pins node:22-slim, moves the URL to runtime config, adds .dockerignore, installs with a lockfile in a first stage and ends with USER node.
What goes wrong
- The agent adds
RUN rm .envafter copying it. The earlier layer still holds the file. apt-get installwithout pinning or cleanup makes builds differ day to day.- The agent copies a "working" Dockerfile from another project with different ports and users.
Examples
The deleted secret that is not deleted
The agent writes COPY .env ., then RUN npm ci, then RUN rm .env and calls it safe. Anyone who pulls the image can inspect the earlier layer and read the file. Use runtime environment variables or a build secret mount.
latest breaks Tuesday's build
A Dockerfile begins FROM python:latest. A new major release lands, a dependency fails to build, and a hotfix can no longer be shipped. Pinning the version turns a surprise into a deliberate upgrade.
Key points
- Pin base images to a specific version, and to a digest when builds must be exactly repeatable.
- A Dockerfile with no USER line runs the process as root.
- A secret copied or set in any layer stays in the image history, even if a later step deletes it.
- Add a .dockerignore so .git and .env files are not copied into the build.
- Use multi-stage builds and ordered layers to keep images small and builds quick.
Common mistakes
- Approving a Dockerfile because the build and the app start worked.
- Believing that a later RUN step removes a file from earlier layers.
- Leaving out the USER line and the .dockerignore file.
- Using the same image tag for different content and deploying by tag name alone.
Check yourself · 2 of 3
Why is FROM node:latest a problem?
Check yourself · 3 of 3
An agent copies .env into an image and later deletes it with RUN rm. Is the secret safe?
Practice items, not counted towards your mastery.
Start here
- strong_migrations: catch unsafe migrations in development (opens in a new tab)ToolFree
A catalogue of unsafe migration operations with the safe alternative for each.
- Parallel Change (martinfowler.com) (opens in a new tab)ArticleFree
The expand, migrate, contract pattern behind safe schema changes.
- terraform plan command (HashiCorp docs) (opens in a new tab)DocsFree
What plan output means and how to save and review a plan before apply.
More for this topic
- Building best practices (Docker docs) (opens in a new tab)DocsFree
Base images, pinning, multi-stage builds and non-root users.