Cloud, DevOps & Security 10 min read

Docker for Beginners: Containers Explained and Your First Dockerfile for a Node.js App

Learn what Docker and containers are, why teams use them, and how to write a production-ready Dockerfile and docker-compose file for a Node.js API with PostgreSQL — explained line by line.

Umer Hasan

Umer Hasan

Co-Founder & CTO at Zapprex

Docker for Beginners: Containers Explained and Your First Dockerfile for a Node.js App
Table of contents

Containers in one analogy

A shipping container holds goods in a standard box that fits any ship, train or truck. A software container packages your app with everything it needs — runtime, libraries, settings — so it runs the same on a laptop, a test server and production. Docker is the most popular tool for building and running containers.

At a glance: Where to run containers in production — A single VPS with Docker Compose, Managed container services, Kubernetes
At a glance: Where to run containers in production

Key terms

TermMeaning
ImageA read-only template of your app — built from a Dockerfile
ContainerA running instance of an image
DockerfileThe recipe that describes how to build the image
RegistryWhere images are stored and shared (for example Docker Hub or a cloud registry)
Docker ComposeA file that runs several containers together — app, database, cache

A production-ready Dockerfile

Dockerfile for a Node.js API (multi-stage)dockerfile
# 1) install dependencies and build
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build --if-present

# 2) small runtime image with production dependencies only
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app .
USER node                       # do not run as root
EXPOSE 4000
CMD ["node", "server.js"]

Tip: Add a .dockerignore file with node_modules, .git and .env so builds are faster and secrets never end up inside the image.

Run the app and database together with Compose

Run the app and database together with Compose: 3 steps
  1. 1

    Build and start: run docker compose up --build.

  2. 2

    Check logs: docker compose logs -f api.

  3. 3

    Stop: docker compose down (data stays in the pgdata volume).

docker-compose.ymlyaml
services:
  api:
    build: .
    ports: ["4000:4000"]
    env_file: .env
    depends_on: [db]
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes: ["pgdata:/var/lib/postgresql/data"]
volumes:
  pgdata:

Where to run containers in production

“Docker removes “works on my machine”. Kubernetes is optional; consistency is not.”

— Umer Hasan, CTO
  • A single VPS with Docker Compose: simple and affordable for small apps.
  • Managed container services: AWS App Runner or ECS, Google Cloud Run, Azure Container Apps.
  • Kubernetes: for many services and teams — powerful, but usually unnecessary early on.

Topics

DockerContainersDocker ComposeDevOps
Umer Hasan
Written by

Umer Hasan

Co-Founder & CTO at Zapprex

Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.

FAQ

Questions readers ask

No. Virtual machines include a full operating system each, while containers share the host’s kernel and are much lighter and faster to start.

Have a project in mind?

Let's build something that moves your business forward.

Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.

  • Founder reply within 1 business day
  • NDA on request · 100% code ownership
  • Talk directly to the engineers