Docker for Beginners: A Complete Getting Started Guide
Docker has revolutionized how we build, ship, and run applications. Instead of worrying about environment differences between development and production, Docker lets you package everything your app needs into a portable container.
What Is Docker?
Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. Containers are lightweight, standalone, and include everything needed to run a piece of software: code, runtime, libraries, and system tools.
Containers vs Virtual Machines
| Feature | Containers | Virtual Machines |
|---|---|---|
| Startup time | Seconds | Minutes |
| Size | Megabytes | Gigabytes |
| Isolation | Process-level | Full OS |
| Performance | Near-native | Overhead from hypervisor |
Installing Docker
On macOS or Windows, install Docker Desktop from docker.com. On Linux:
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
# Add your user to the docker group
sudo usermod -aG docker $USER
Your First Container
# Pull and run the official Nginx image
docker run -d -p 8080:80 --name my-nginx nginx
# Verify it's running
docker ps
# Check logs
docker logs my-nginx
# Stop and remove
docker stop my-nginx
docker rm my-nginx
Writing a Dockerfile
A Dockerfile defines how to build your image. Here's one for a Node.js app:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build and run it:
docker build -t my-node-app .
docker run -d -p 3000:3000 my-node-app
Essential Docker Commands
docker images— list local imagesdocker ps -a— list all containers (including stopped)docker exec -it <container> sh— shell into a running containerdocker volume ls— list volumes for persistent datadocker network ls— list networks
Docker Compose
For multi-container setups, use Docker Compose:
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:16-alpine
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Run with docker compose up -d.
Best Practices
- Use multi-stage builds to keep images small
- Never run containers as root in production
- Use
.dockerignoreto exclude unnecessary files - Pin image versions (e.g.,
node:20-alpine, notnode:latest) - One process per container
Docker is the foundation of modern DevOps. Master these basics and you're ready to tackle orchestration with Kubernetes.
Enjoyed this article?
Get more DevOps insights delivered to your inbox.
Get new posts by email
Subscribe to get an email when a new blog post is published. Skip anytime.
No spam, unsubscribe anytime.
Related Posts
Discussion
1 comment
Sign in to join the conversation.
This is exactly what I needed to get started with Docker. The Compose example is really helpful!
Glad it helped! Multi-stage builds are a game-changer for keeping images small.