Engineering
Docker for developers
Docker containers explained plainly: images, layers, Compose, and the workflow that ends 'works on my machine' for good.
TL;DR
Docker packages your app and its dependencies into a portable unit that runs identically everywhere. Master images, layer caching, and Compose, and a whole category of environment bugs stops existing.

"Works on my machine" is the most expensive sentence in software. Docker exists to kill it. It packages your application together with its runtime, libraries, and system dependencies into a single portable unit that behaves identically on your laptop, a teammate's machine, and production. Once the core concepts click, it stops feeling like mysterious DevOps magic and becomes something you reach for daily.
Images and containers: the core idea
Two terms do most of the work in docker for developers conversations:
- An image is a blueprint, a read-only snapshot of your app and its environment, built from a
Dockerfile. - A container is a running instance of that image, a lightweight, isolated process.
Think of an image as a class and a container as an object you instantiate from it. You build the image once and run as many containers from it as you need. Unlike a virtual machine, containers share the host kernel, so they start in milliseconds and use a fraction of the resources.
Understand layers, and your builds get fast
A Dockerfile is a list of instructions, and each one creates a layer that Docker caches. This is the single most useful thing to understand for a good workflow: order instructions from least to most frequently changing.
Copy your dependency manifest and install dependencies before copying your application code:
``dockerfile COPY package.json package-lock.json ./ RUN npm ci COPY . . ``
Your source changes far more often than your dependencies, so Docker reuses the cached install layer on every rebuild and only re-runs the fast steps at the end. Get the order wrong and every one-line change reinstalls everything from scratch.
Dockerfile best practices worth keeping
Beyond layer order, a few habits pay consistent dividends:
- Use a specific base image tag (
node:20-slim) instead oflatestso builds are reproducible. - Add a
.dockerignorefile to excludenode_modules,.git, and local env files from the build context. Smaller context means faster builds. - Run your process as a non-root user inside the container.
Keep images small with multi-stage builds
Big images are slow to push, pull, and deploy. They also carry a larger attack surface. Multi-stage builds let you compile or bundle in a fat builder stage, then copy only the finished artifacts into a slim final image.
```dockerfile FROM node:20 AS builder WORKDIR /app COPY . . RUN npm ci && npm run build
FROM node:20-slim WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/main.js"] ```
The production image never sees your TypeScript compiler, test dependencies, or source maps. A lean image is a faster, safer image, and this pattern is worth adopting early.
Compose ties the whole stack together
Real apps are not one process. You have an API, a PostgreSQL database, maybe Redis for caching, perhaps a background worker. Docker Compose describes all of them in a single docker-compose.yml and starts them together with one command.
A new developer clones the repo, runs docker compose up, and has the full stack running locally in minutes. No multi-page setup wiki, no version mismatches, no "did you install PostgreSQL 16?" That single benefit alone justifies adopting Docker on most teams.
A minimal example for a Next.js app with Postgres:
```yaml services: app: build: . ports: - "3000:3000" environment: DATABASE_URL: postgres://user:pass@db:5432/mydb depends_on: - db db: image: postgres:16-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: mydb volumes: - pgdata:/var/lib/postgresql/data
volumes: pgdata: ```
The depends_on key ensures the database starts before the app tries to connect. The named volume keeps your data between container restarts.
The workflow that actually matters
This is where it ties together in practice:
- Define the environment once in a
Dockerfileanddocker-compose.yml, committed to the repo. - Every developer runs the identical stack locally, no configuration drift.
- CI builds and tests the same image you will deploy. See CI/CD pipeline setup for how to wire this up.
- That exact image ships to production.
The same bytes run everywhere. A whole category of environment bugs stops existing. You do not need to master every Docker flag to get this benefit; the fundamentals above cover the vast majority of real development work.
If your onboarding still involves a wiki page of setup steps, let's talk.
Frequently asked questions
what is the difference between a docker image and a container
An image is a read-only snapshot of your app and its environment, built from a Dockerfile. A container is a running instance of that image. You build an image once and spin up as many containers from it as you need.
how does docker layer caching work
Each instruction in a Dockerfile creates a cached layer. If nothing above that instruction has changed, Docker reuses the cache and skips re-running it. Order instructions from least to most frequently changing so expensive steps like dependency installs are cached on most rebuilds.
what is a docker multi-stage build
A multi-stage build uses multiple FROM instructions in one Dockerfile. You do heavy work like compiling or bundling in an early stage, then copy only the finished artifacts into a slim final image. This keeps production images small and free of build tooling.
do i need docker for local development
You don't need it, but it removes a whole class of setup problems. Docker Compose lets a new developer clone a repo, run one command, and have the full stack running locally with matching versions of every dependency.
How Pykero can help
Services related to this article.
Building something like this?
Pykero Agency designs and ships production web, mobile, SaaS, and AI products.
Talk to us


