A comprehensive practical guide to Docker, from basic commands to advanced deployments, with concrete examples and detailed explanations.
- ๐ข Beginner Level
- ๐ก Intermediate Level
- ๐ด Advanced Level
- โก Quick Reference
- ๐ ๏ธ Practical Examples
docker --version - Check Installation
# Check installed Docker version
docker --version
# Check detailed info
docker info๐ก Tip: Make sure Docker Desktop (Windows/Mac) or Docker Engine (Linux) is installed and running.
docker run - Run a Container
# Run an Ubuntu container and open an interactive shell
docker run -it ubuntu bash
# Run an Nginx server on port 8080
docker run -d -p 8080:80 nginx๐ก Tip: Use -d for detached mode, -p to map ports.
docker ps - List Containers
# Running containers
docker ps
# All containers (including stopped)
docker ps -a๐ก Tip: Combine with --format to customize output.
docker stop / start / rm - Manage Containers
# Stop a container
docker stop my_container
# Start a container
docker start my_container
# Remove a container
docker rm my_containerdocker images - Manage Images
# List local images
docker images
# Remove an image
docker rmi nginx
# Pull an image from Docker Hub
docker pull postgres:15๐ก Tip: Always use tags (nginx:1.25) instead of latest for stability.
Dockerfile - Build Custom Images
# Example Node.js app
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]# Build image
docker build -t my-node-app .
# Run container
docker run -d -p 3000:3000 my-node-app๐ก Tip: Use .dockerignore to exclude unnecessary files.
Volumes - Persist Data
# Create and mount a volume
docker volume create db_data
docker run -d -v db_data:/var/lib/postgresql/data postgres:15
# Bind mount local folder
docker run -d -v $(pwd)/data:/app/data my-node-app๐ก Tip: Volumes survive container deletion, bind mounts sync with local files.
Networks - Connect Containers
# Create a custom network
docker network create my_network
# Run containers on same network
docker run -d --name db --network my_network postgres
docker run -d --name api --network my_network my-node-app๐ก Tip: Containers on the same custom network can reach each other by name.
docker-compose - Multi-Container Apps
version: '3.9'
services:
db:
image: postgres:15
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: example
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
volumes:
db_data:# Start app
docker-compose up -d
# Stop app
docker-compose down๐ก Tip: Use .env file to store secrets and environment variables.
Multi-stage Builds - Optimize Images
# Build stage
FROM golang:1.21 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Final image
FROM alpine:3.18
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]๐ก Tip: Multi-stage builds reduce image size and improve security.
docker login / push - Push to Registry
# Login to Docker Hub
docker login
# Tag and push image
docker tag my-node-app myuser/my-node-app:1.0
docker push myuser/my-node-app:1.0๐ก Tip: Use private registries for enterprise projects.
Docker Swarm - Orchestration Basics
# Init swarm
docker swarm init
# Deploy a service
docker service create --name web -p 80:80 nginx
# Scale service
docker service scale web=3๐ก Tip: Use Kubernetes for advanced orchestration at scale.
Security Best Practices
- Run containers as non-root (
USERin Dockerfile). - Use slim base images (
alpine,distroless). - Regularly scan images (
docker scan). - Keep secrets outside images (use env vars, secret managers).
- Limit container privileges with
--cap-drop.
# Container lifecycle
docker run -it image bash
docker ps -a
docker stop <id>
docker rm <id>
# Images
docker images
docker pull nginx:1.25
docker rmi <id>
# Volumes & Networks
docker volume ls
docker network ls
# Compose
docker-compose up -d
docker-compose downFROM # Base image
RUN # Execute commands
COPY # Copy files
WORKDIR # Set working directory
EXPOSE # Expose port
CMD # Default command
ENTRYPOINT # Main entrypointdocker run -d -p 8080:80 -v $(pwd)/site:/usr/share/nginx/html nginxversion: '3.9'
services:
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
volumes:
- db_data:/var/lib/postgresql/data
app:
build: ./app
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://postgres:secret@db:5432/postgres
depends_on:
- db
volumes:
db_data:docker-compose up -d# Build stage
FROM node:18-alpine AS build
WORKDIR /app
COPY . .
RUN npm install && npm run build
# Serve stage
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]- ๐ Docker Documentation
- ๐ Dockerfile Best Practices
- ๐ Compose File Reference
- ๐ฎ Play with Docker
- ๐ Docker Cheatsheet
"This guide accompanies you in your Docker journey. From beginner to expert, each command is a step towards mastering containerization!"
โจ Created with passion by DorianABDS