Docker for Beginners 2026: Complete Getting Started Guide
Learn Docker from scratch - containers, images, Docker Compose, and deployment explained step by step for beginners.
Docker solves the "it works on my machine" problem. It packages your application with all its dependencies into a container that runs the same everywhere - your laptop, your teammate's laptop, and the production server.
This guide teaches you Docker from zero, step by step.
Why Learn Docker in 2026?
- Industry standard - 83% of organizations use containers in production
- Required in job postings - most backend and DevOps roles require Docker knowledge
- Consistent environments - no more "works on my machine" issues
- Easy deployment - deploy to any cloud with one command
- Microservices - Docker is the foundation of modern architecture
- Learning DevOps - Docker is the first step toward Kubernetes
What Is Docker?
Docker creates containers - lightweight, isolated environments that package your app with everything it needs to run.
Without Docker:
- "Install Node.js 20, PostgreSQL 15, Redis 7, set these 20 environment variables, run these 5 commands..."
- Works on your machine but crashes on your teammate's
- Production server has different versions of everything
With Docker:
- One Dockerfile describes everything your app needs
docker compose upstarts everything with one command- Same environment everywhere - development, testing, production
Key Concepts
Images vs Containers
| Concept | Analogy | What It Is |
|---|---|---|
| Image | Recipe | Blueprint with instructions to build your environment |
| Container | The meal | A running instance of an image |
- An image is a read-only template (like a class in programming)
- A container is a running instance of an image (like an object)
- You can run multiple containers from the same image
Dockerfile
A Dockerfile is a text file with instructions to build an image:
# Start from Node.js base image
FROM node:20-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application code
COPY . .
# Build the application
RUN npm run build
# Expose port 3000
EXPOSE 3000
# Start command
CMD ["npm", "start"]
What each instruction does:
FROM- base image to start from (like an OS with tools pre-installed)WORKDIR- set the working directory inside the containerCOPY- copy files from your machine into the containerRUN- execute a command during image buildEXPOSE- document which port the app listens onCMD- the command to run when the container starts
Step 1: Install Docker
Windows/Mac: Download Docker Desktop from docker.com
Linux:
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
Verify installation:
docker --version
# Docker version 27.x.x
docker run hello-world
# Should print "Hello from Docker!"
Step 2: Your First Container
Run an Existing Image
# Run an Nginx web server
docker run -d -p 8080:80 nginx
# -d = run in background (detached)
# -p 8080:80 = map your port 8080 to container's port 80
Open http://localhost:8080 - you will see the Nginx welcome page!
Manage Containers
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container
docker stop <container_id>
# Remove a container
docker rm <container_id>
# View container logs
docker logs <container_id>
# Execute a command inside a container
docker exec -it <container_id> /bin/sh
Step 3: Build Your Own Image
Example: Dockerize a Node.js App
- Create a simple app:
// server.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Docker!');
});
server.listen(3000, () => console.log('Server running on port 3000'));
- Create a Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY server.js .
EXPOSE 3000
CMD ["node", "server.js"]
- Build and run:
# Build the image
docker build -t my-app .
# Run a container from the image
docker run -d -p 3000:3000 my-app
# Test it
curl http://localhost:3000
# Hello from Docker!
.dockerignore
Like .gitignore, this file tells Docker what NOT to copy:
node_modules
.git
.env
.next
dist
*.log
Always include node_modules - dependencies should be installed fresh inside the container.
Step 4: Docker Compose
Docker Compose lets you run multiple containers together. Most real apps need a web server + database + cache.
docker-compose.yml Example
version: '3.8'
services:
# Your web application
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:password@db:5432/myapp
- REDIS_URL=redis://cache:6379
depends_on:
- db
- cache
# PostgreSQL database
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
# Redis cache
cache:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
pgdata:
Docker Compose Commands
# Start all services
docker compose up
# Start in background
docker compose up -d
# Stop all services
docker compose down
# Rebuild after code changes
docker compose up --build
# View logs
docker compose logs -f
# Run a command in a service
docker compose exec web sh
One command to start your entire app - web server, database, and cache. Every team member runs the same setup.
Step 5: Volumes (Persistent Data)
Containers are ephemeral - when you remove a container, its data is gone. Volumes persist data outside the container.
Types of volumes:
- Named volumes - Docker manages the storage (best for databases)
- Bind mounts - map a host directory to a container directory (best for development)
# Named volume (database data persists)
volumes:
- pgdata:/var/lib/postgresql/data
# Bind mount (live code reloading for development)
volumes:
- ./src:/app/src
Step 6: Docker for Development
Hot Reloading Setup
For development, mount your source code so changes appear instantly:
# docker-compose.dev.yml
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
volumes:
- .:/app # Mount entire project
- /app/node_modules # Prevent overwriting node_modules
environment:
- NODE_ENV=development
# Dockerfile.dev
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
Step 7: Deploying with Docker
Deploy to Any Cloud
Docker containers run on every cloud platform:
- Railway - git push deploys with auto-detection
- Fly.io -
fly deployfrom Dockerfile - AWS ECS - managed container service
- Google Cloud Run - serverless containers
- DigitalOcean App Platform - simple container hosting
Production Dockerfile (Multi-Stage Build)
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
# Stage 2: Production (smaller image)
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/package.json ./
EXPOSE 3000
CMD ["npm", "start"]
Multi-stage builds create smaller production images by only including what is needed to run (not build tools, dev dependencies, etc.).
Essential Docker Commands Cheat Sheet
| Command | What It Does |
|---|---|
docker build -t name . | Build image from Dockerfile |
docker run -d -p 3000:3000 name | Run container |
docker ps | List running containers |
docker stop id | Stop container |
docker rm id | Remove container |
docker images | List images |
docker rmi name | Remove image |
docker logs id | View logs |
docker exec -it id sh | Shell into container |
docker compose up -d | Start all services |
docker compose down | Stop all services |
docker compose logs -f | Follow logs |
docker system prune | Clean unused resources |
Common Mistakes Beginners Make
- Not using .dockerignore - copies node_modules into the image (huge, slow)
- Running as root - add
USER nodein Dockerfile for security - Not using multi-stage builds - production images are unnecessarily large
- Storing secrets in Dockerfile - use environment variables or secrets management
- Not using volumes for databases - data disappears when container is removed
- Using
latesttag - always pin specific versions (node:20-alpine, notnode:latest) - Ignoring layer caching - put COPY package.json before COPY . for faster builds
- Not cleaning up - run
docker system pruneregularly to free disk space
Related ByteVerse guides
Next, read Next.js 16 Deployment Guide 2026, JavaScript Roadmap 2026, How to Learn Programming 2026, and Website Speed Optimization 2026 to explore more.
Running Docker on Windows? Set up WSL 2 first - Docker Desktop uses it as the backend and performance is significantly better.
Frequently Asked Questions
Is Docker hard to learn?
The basics (build, run, docker-compose) take 2-3 hours to learn. Understanding images, containers, volumes, and networking takes a few days of practice. Docker is easier than most people expect - the concepts are intuitive once you start using them.
Do I need Docker for web development?
Not strictly necessary, but highly recommended. Docker ensures every team member has the same development environment. It eliminates "works on my machine" problems and makes onboarding new developers instant.
Docker vs virtual machines - what is the difference?
Docker containers share the host OS kernel, making them lightweight (MBs) and fast to start (seconds). Virtual machines include a full OS, making them heavy (GBs) and slow to start (minutes). Use Docker for applications, VMs for running different operating systems.
How much RAM does Docker need?
Docker Desktop recommends 4GB RAM minimum. Each container uses only what the app inside it needs - a Node.js app might use 50-200MB. A typical development setup with web server + database + cache uses 500MB-1GB total.
Should I learn Docker or Kubernetes first?
Docker first, always. Kubernetes orchestrates Docker containers at scale. You need to understand Docker containers before Kubernetes makes sense. Learn Docker → Docker Compose → then Kubernetes when you need to manage many containers.
Is Docker free?
Docker Engine is free and open source. Docker Desktop is free for personal use, education, and small businesses (under 250 employees). Large enterprises need a Docker Business subscription ($24/user/month).
Share this article