"Works on my machine" is a process failure: reproducible environments with Docker
Every engineer has said it, and every engineer has heard it: "works on my machine." It is usually delivered with a shrug, as if the machine were a force of nature. It is not. When code behaves differently on two machines
Every engineer has said it, and every engineer has heard it: "works on my machine." It is usually delivered with a shrug, as if the machine were a force of nature. It is not. When code behaves differently on two machines, the difference is never magic. It is an undocumented assumption — a version, a system library, an environment variable, a locale — that lives in one environment and not the other. "Works on my machine" is what a missing process sounds like.
I lead engineering teams that ship SaaS products used by real customers, and the single highest-leverage change we ever made was boring: we stopped treating environments as pets and started treating them as build artifacts. Docker is how we do it. Not because containers are fashionable, but because a Dockerfile is the first environment description that is both executable and reviewable in a pull request.
The anatomy of environment drift
Drift rarely announces itself. It accumulates:
- Runtime versions. Your laptop runs Node 22, the server runs Node 20, and a dependency quietly uses an API that only exists in one of them. Nothing fails loudly; something just behaves slightly differently.
- System libraries. The image-processing package works because your OS ships a library the production image does not. It fails at 2 a.m., on a customer upload, not in your tests.
- Environment variables. A feature flag, a timezone, a locale. The bug that only reproduces "in production" is very often a variable nobody wrote down.
- Native dependencies and build tools. The package that compiles from source on one machine and downloads a prebuilt binary on another is a classic source of "identical" installs that are not identical at all.
None of these are developer mistakes. They are what happens when the environment is described in a wiki page, a README from two years ago, or the memory of whoever set up the server. Human-maintained descriptions rot. Executable ones get tested every time they run.
One Dockerfile, from laptop to production
The rule that fixed this for us: the same Dockerfile builds the image that runs in development, in CI, and in production. Not three similar Dockerfiles. One.
FROM node:22.11.0-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:22.11.0-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
FROM node:22.11.0-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=deps /app/node_modules ./node_modules
CMD ["node", "dist/main.js"]
Three details in this file do most of the work. The base image is pinned to an exact version, not a floating tag. npm ci installs from the lockfile, so the dependency tree is the one that was committed, not the one the resolver felt like building today. And the multi-stage build means the production image carries the compiled output and production dependencies — not your dev tools, not your source history, not your mistakes.
Pinning is not paranoia
Floating tags like node:latest or even node:22 are drift on a schedule. The tag points at a different image next month than it does today, which means your "unchanged" application gets a new environment underneath it without a single commit. Pin exact versions for base images and dependencies, and upgrade them deliberately, in a pull request, where the change is visible and reversible. A deliberate upgrade that breaks something is a normal Tuesday. A silent one is an incident.
Compose for local parity
A Dockerfile describes one service. Real applications are a small society: an API, a database, a cache, maybe a queue. Docker Compose is how a new hire gets that whole society running with one command, configured the same way it is configured for everyone else:
services:
api:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:app@db:5432/app
depends_on:
- db
db:
image: postgres:16.4-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
The win is not convenience, although onboarding dropping from "follow the wiki and pray" to docker compose up is a genuine win. The win is parity: the database version your code is developed against is the database version in the file, for every developer, every time.
The CI gate: build the image you ship
Here is the step teams skip, and it is the step that makes the rest real. Your CI pipeline should build the Docker image and run your tests inside it — and then that exact image, not a rebuild, is what gets deployed. If CI builds one artifact and production builds another, you have tested a hypothesis and shipped a different one. Building once and promoting the same image through staging to production turns "it passed CI" into a statement about the thing your customers actually run.
This also changes code review for the better. A change to the Dockerfile is a change to production's environment, and it shows up in the diff like any other code change. Nobody edits the server by hand anymore, because there is nothing to edit: the server runs an image, and images come from the repository.
The failure stories are always the same shape
I will not dress these up as war stories; you have lived them yourself. The deploy that failed because the server's runtime was one major version behind the laptop's. The bug that existed only in production because a system locale changed how dates sorted. The new developer who lost two days to setup before writing a line of code, following instructions written for a stack the team had already left. Every one of those is the same story: the environment was knowledge in someone's head instead of code in the repository.
Start smaller than you think
You do not need Kubernetes, a platform team, or a migration project. Pick one service. Write the Dockerfile. Pin the versions. Add Compose for its database. Make CI build the image and deploy that image. The first service takes an afternoon; the argument for the second one makes itself the first time a deploy is boring.
Because that is the real goal. Deploys should be boring, onboarding should be boring, and "works on my machine" should be a sentence from the past — not because your machines got better, but because they stopped being different machines at all.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.