DEV Community

Cover image for Docker for Developers, From Zero to a 234 MB Image
Muiduzzaman Lipu for Refactorly

Posted on

Docker for Developers, From Zero to a 234 MB Image

"It works on my machine" is the oldest excuse in software. Docker fixes it by packaging your app together with everything it needs to run: the OS libraries, the runtime, and your code. If it runs in the container on your laptop, it runs the same way on the server.

This is the practical minimum you need as a developer. We'll go from zero to a running container, then cut the image from 1.69 GB to 234 MB. Every command below was run for real, and the image sizes are measured.

Prefer video? Here's the walkthrough:

Images vs containers

Two words to get straight first:

  • Image: a read-only package with a minimal OS, your runtime (Node, Python…) and your code. The recipe.
  • Container: a running instance of an image.

You can start many containers from one image, like creating many objects from one class.

A Dockerfile is the recipe

You describe an image in a Dockerfile:

FROM node:22
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/server.js"]
Enter fullscreen mode Exit fullscreen mode
  • FROM: the base image to start from (here, the official Node 22 image)
  • WORKDIR: the folder inside the image where the next commands run
  • COPY: copies your project into the image
  • RUN: runs a command while building (installing dependencies, compiling)
  • CMD: the command that runs when a container starts

EXPOSE only documents the port. It doesn't publish anything. That's what -p does below.

Build it, run it

$ docker build -t myapp .
$ docker run -d --name web -p 8080:3000 myapp
$ curl localhost:8080
Hello from Docker!
Enter fullscreen mode Exit fullscreen mode
  • -t myapp names (tags) the image
  • -d runs the container in the background
  • -p 8080:3000 maps port 8080 on your machine to port 3000 inside the container. The order is host:container, and it's the flag people mix up most often.

Managing containers

$ docker ps
CONTAINER ID   IMAGE   PORTS                    NAMES
502a0c6c53a3   myapp   0.0.0.0:8080->3000/tcp   web

$ docker logs web
Listening on 3000

$ docker stop web && docker rm web
Enter fullscreen mode Exit fullscreen mode

docker ps lists running containers (add -a to include stopped ones), docker logs shows your app's output (-f follows it), and stop plus rm clean up.

Volumes: the gotcha that loses your data

When a container is removed, everything written inside it is gone. That's fine for your app code, and a disaster for a database.

Use a volume (-v name:/path) for anything you want to keep:

$ docker run --rm -v pgdata:/data alpine sh -c 'echo saved > /data/note.txt'
$ docker run --rm -v pgdata:/data alpine cat /data/note.txt
saved ✅

$ docker run --rm alpine cat /data/note.txt
cat: can't open '/data/note.txt': No such file or directory
Enter fullscreen mode Exit fullscreen mode

The first container writes a file into the pgdata volume and is deleted (--rm). A brand-new container with the same volume still sees the file. Without the volume, it doesn't exist.

Multi-stage builds: 1.69 GB → 234 MB

Our first Dockerfile ships everything: the full Debian-based node:22 image, the TypeScript compiler, every dev dependency and the source files. The result was 1.69 GB.

A multi-stage build compiles in one stage, then copies only the built output into a small final image:

FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:22-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
CMD ["node", "dist/server.js"]
Enter fullscreen mode Exit fullscreen mode

Same app, 234 MB. About 7× smaller, which means faster pushes, faster deploys and less attack surface.

Two details in that file matter:

  1. COPY package*.json ./ before COPY . . lets Docker cache the npm install layer. Change your code and the rebuild skips reinstalling dependencies.
  2. Only the final stage ends up in the image. The compiler and dev dependencies from the build stage are thrown away.

(If your app needs runtime dependencies, copy package*.json into the final stage and run npm ci --omit=dev there too.)

Don't forget .dockerignore

COPY . . copies everything, including node_modules and your .env file with real secrets. Add a .dockerignore:

node_modules
.env
.git
dist
Enter fullscreen mode Exit fullscreen mode

Cheat sheet

  • docker build -t name .: build an image
  • docker run -d -p 8080:3000 name: start a container (host:container)
  • docker ps / docker logs / docker stop / docker rm
  • -v volume:/path: keep data after the container is gone
  • Multi-stage build + alpine base: much smaller images
  • Always add a .dockerignore

Want a follow-up on Docker Compose (app + Postgres + Redis with one command)? Let me know in the comments.


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)