Back to devops
devops#docker#dockerfile#devops-basics

Writing Your First Dockerfile

A friendly walkthrough of the Dockerfile syntax — write a working image for a small web app in 10 minutes.

Jane Contributor August 2, 2026 2 views

Writing Your First Dockerfile

A Dockerfile is a text file that lists the steps to build your container image. Think of it as a cooking recipe.

A working example (Node.js)

# 1. Start from a base image that already has Node
FROM node:20-alpine

# 2. Choose a folder inside the container
WORKDIR /app

# 3. Copy dependency files first (for caching)
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# 4. Copy the rest of the source
COPY . .

# 5. Tell users which port the app listens on
EXPOSE 3000

# 6. Command that runs when the container starts
CMD ["node", "server.js"]

Line by line

  • FROM — pick a starting image. -alpine versions are smaller.
  • WORKDIR — like cd /app for the container.
  • COPY — bring files from your machine into the image.
  • RUN — execute a command during the build (installs packages).
  • CMD — the command that runs every time a container starts.

Build and run it

docker build -t my-app .
docker run -p 3000:3000 my-app

Beginner tips

  1. Order matters — put commands that change less at the top so Docker can cache them.
  2. .dockerignore — like .gitignore; keep node_modules and .git out of the build context.
  3. Small images matter — a 100MB image starts faster than a 900MB one. Use -alpine or -slim base images.

Real-world analogy

A Dockerfile is a recipe card. docker build follows the recipe and makes a cake (the image). docker run serves a slice (the container). If you change the recipe, you bake a new cake — you never edit an already-baked cake.

Keep reading

You may also like

Discussion (0)

No comments yet. Be the first to weigh in.

Leave a comment

Comments are reviewed before appearing.