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.

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.

Key terms
| Term | Meaning |
|---|---|
| Image | A read-only template of your app — built from a Dockerfile |
| Container | A running instance of an image |
| Dockerfile | The recipe that describes how to build the image |
| Registry | Where images are stored and shared (for example Docker Hub or a cloud registry) |
| Docker Compose | A file that runs several containers together — app, database, cache |
A production-ready 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

- 1
Build and start: run docker compose up --build.
- 2
Check logs: docker compose logs -f api.
- 3
Stop: docker compose down (data stays in the pgdata volume).
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.”
- 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
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.
Keep reading
All articlesOTP Login Done Right: Security, Rate Limits, Autofill and SMS vs WhatsApp OTP
Build OTP login that is secure, cheap and pleasant: how OTPs should be generated and stored, rate limiting, SMS pumping fraud, autofill on Android, iOS and the web, and when to use WhatsApp or voice OTP.
Web Application Security Checklist: 25 Must-Do Steps Based on the OWASP Top 10
A practical, plain-English security checklist for websites and web apps: access control, injection, authentication, secrets, dependencies, headers, logging and backups — mapped to the OWASP Top 10.
API Security Best Practices: Authentication, Authorisation, Rate Limiting and More
How to secure REST and GraphQL APIs used by mobile apps and websites: tokens and keys, object-level authorisation, input validation, rate limits, CORS, webhooks and logging — with simple examples.
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.
Not necessarily. Docker helps most when you have several services, multiple environments or a team that needs identical setups.
It can, with volumes and backups, but many teams prefer a managed database service for production to simplify backups, upgrades and failover.
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
