Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Have your coding agent write a Dockerfile you can rebuild anywhere

Get your coding agent to containerize your app the right way, then check the Dockerfile yourself: pinned base, no secrets, fast rebuilds.

Published on November 10, 20266 min read

Your app runs on your laptop. Then you push it to a cheap server, or hand the repo to a friend, and it crashes on startup. A Dockerfile fixes that by packing your app with everything it needs into one image that behaves the same everywhere. Your coding agent can write one in minutes. The catch is that it can produce a plausible-looking file that is slow to rebuild, runs as root, or quietly bakes your API keys into the image. So ask for specific things, then check the result yourself.

Why "works on my machine" bites hardest at deploy

Your laptop is not a clean environment, and that's the problem. Over months it has collected a specific version of Node or Python, system libraries you installed once and forgot, and a node_modules folder that's been there since the project started. A fresh server, a teammate's machine, or a container platform has none of that.

This is why the failure shows up at the worst moment. The code is the same. Everything around the code is different. You end up debugging the environment instead of your app.

A Docker image is a recipe that lists your app plus its exact runtime, its exact dependencies, and the commands to start it. Anyone can rebuild that recipe and get the same result, including a server, a collaborator, or you later. Docker's own best practices page is built around making those builds repeatable.

And since your agent already lives in your repo, writing this file is squarely its job. You just need to ask for the right file, not just "dockerize it".

What to ask the agent for

Give it a list, not a vibe. Each of the five things below is in Docker's own guidance, and each one prevents a specific, annoying failure.

  1. A pinned base image tag. FROM node:latest means "whatever Node exists that day". Docker's docs point out that tags are mutable: even alpine:3.21 can point to a newer patch release when you rebuild months later (best practices, "Pin base image versions"). Pin an exact version, or a digest if you want the build fully frozen.
  2. A .dockerignore that keeps junk out of the build. At minimum: .env, node_modules, .git. Without it, COPY . . sends your secrets and your whole dependency folder into the build. The .dockerignore docs show the syntax.
  3. Lockfile first, source later. Copy package.json and the lockfile, run the install, then copy the rest of your code. Build cache works layer by layer: if the dependency files didn't change, the install layer is reused instead of reinstalling everything (build cache docs).
  4. A non-root user. Docker's guidance is plain: if a service can run without privileges, use USER to switch to a non-root user (best practices, "USER").
  5. No secrets inside the image. No hardcoded API keys, no copying the .env in, and no keys in ARG or ENV lines: Docker's docs say build arguments and environment variables persist in the final image (build secrets). Anyone who pulls the image can read them. Pass secrets when the container runs.

The prompt to paste

Paste this into your coding agent. If you don't have a setup yet, you can try the zcode agent harness free.

Write a Dockerfile and a .dockerignore for this app (it's a small Node app
that starts with "node server.js").

Requirements:
- Base image: a pinned slim Node tag, no :latest
- .dockerignore excluding: .env, node_modules, .git, README.md
- Copy package.json and the lockfile first, run npm ci, THEN copy the rest
  of the source (so the install layer caches)
- Run the app as a non-root user, not root
- Do NOT copy or bake in any secrets or .env values; the app reads its
  config from environment variables at run time
- End with a sensible CMD

Then explain each line in one sentence so I can check it.

The last line matters. An explanation per line forces the agent to show its reasoning, and gives you something to disagree with.

A small Node app ends up looking roughly like this (check the current Node tag on Docker Hub before you copy it):

FROM node:22.14.0-slim

WORKDIR /app

# Dependencies first: this layer is cached until the lockfile changes
COPY package.json package-lock.json ./
RUN npm ci

# Now the source, which changes often
COPY . .

# The official node image ships an unprivileged "node" user
USER node

EXPOSE 3000
CMD ["node", "server.js"]

And the .dockerignore:

.env
node_modules
.git
README.md

For a Python app the shape is the same: python:3.12-slim as the base, a requirements.txt copied and installed before the source, ENV PYTHONDONTWRITEBYTECODE=1, and a RUN useradd plus USER pair instead of USER node. The official Node image includes the node user (docker-node best practices); for Python, have the agent create a user the way Docker's best practices page shows.

Check the work yourself

The agent's file being plausible is not the same as it being right. Four checks.

Build from a clean clone. Clone your repo into a fresh directory and build there. If the Dockerfile only works next to files that happen to be on your laptop, the clean-clone build finds it immediately.

Run it and hit it.

docker build -t myapp .
docker run --rm -p 3000:3000 myapp

Then open http://localhost:3000 (or your health endpoint) in a browser. A successful build is half the story; the app starting inside it is the other half. If it needs an API key, pass it at run time and confirm it boots: docker run --rm -p 3000:3000 -e API_KEY=xxx myapp.

Look for leaked secrets.

docker history --no-trunc myapp

This lists every layer with the command that created it (docker image history), so a key passed as ARG or ENV shows up here. If you see one, rebuild without it and rotate the key. History won't show the contents of copied files, so also check that .env didn't make it in:

docker run --rm myapp ls -la

Rebuild after a one-line change. Edit one file in your source, rebuild, and watch the output. The npm ci step should show as CACHED. If it reinstalls every dependency every time, the agent put COPY . . before the install, and you pay for that on every rebuild.

The checklist

Screenshot this and keep it next to your terminal:

  • Base image pinned to an exact tag, no latest
  • .dockerignore covers .env, node_modules, .git
  • Lockfile + install before COPY . .
  • App runs as a non-root user
  • No secrets in the image (no keys in ARG/ENV, no .env copied), config passed at run time
  • Builds from a clean clone
  • App starts and responds on its port
  • docker history and ls -la inside the container show no secrets
  • A one-line edit rebuilds with the install step CACHED

That's the whole game. It isn't a full production setup, but it's an image you can rebuild anywhere and hand to anyone.

If your agent runs on a GLM coding plan, here's 10% off GLM coding plans. For more build notes like this one, there's more on our blog.

If this saved you an evening of deploy debugging, a tip in $WURK helps us keep writing these.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.