#Docker

Docker is the industry standard for containerization.


#🎯 Learning Objectives

  • Install and configure Docker
  • Master essential Docker commands
  • Manage images, containers, volumes, and networks
  • Follow Docker best practices

#Installation

#Linux

bash
1# Quick install
2curl -fsSL https://get.docker.com | sh
3sudo usermod -aG docker $USER
4newgrp docker
5
6# Verify installation
7docker --version
8docker run hello-world

#Windows & macOS

Download Docker Desktop from docker.com

#Post-Installation Test

bash
1# Run test container
2docker run hello-world
3
4# Check Docker info
5docker info
6
7# Check Docker version details
8docker version

#Essential Commands

#Running Containers

bash
1# Basic run
2docker run nginx                          # Foreground
3docker run -d nginx                       # Detached (background)
4docker run -d --name web nginx            # Named container
5docker run -it ubuntu bash                # Interactive terminal
6
7# Port mapping
8docker run -d -p 8080:80 nginx            # Host:Container
9docker run -d -p 80:80 -p 443:443 nginx   # Multiple ports
10docker run -d -P nginx                    # Random host ports
11
12# Environment variables
13docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
14docker run -d --env-file .env myapp
15
16# Resource limits
17docker run -d --memory="256m" --cpus="0.5" nginx
18
19# Auto-restart
20docker run -d --restart=unless-stopped nginx
21docker run -d --restart=always nginx
22
23# Remove after exit
24docker run --rm -it ubuntu bash

#Container Management

bash
1# List containers
2docker ps                    # Running only
3docker ps -a                 # All (including stopped)
4docker ps -q                 # Only IDs
5docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
6
7# Start/Stop/Restart
8docker stop container_name
9docker start container_name
10docker restart container_name
11docker kill container_name    # Force stop
12
13# Remove containers
14docker rm container_name
15docker rm -f container_name   # Force remove running
16docker rm $(docker ps -aq)    # Remove all stopped
17
18# Inspect container
19docker inspect container_name
20docker inspect -f '{{.NetworkSettings.IPAddress}}' container_name

#Logs and Debugging

bash
1# View logs
2docker logs container_name
3docker logs -f container_name              # Follow (tail)
4docker logs --tail 100 container_name      # Last 100 lines
5docker logs --since 1h container_name      # Last hour
6docker logs -t container_name              # With timestamps
7
8# Execute commands in container
9docker exec -it container_name bash
10docker exec -it container_name sh
11docker exec container_name ls /app
12
13# Copy files
14docker cp container_name:/app/file.txt ./
15docker cp ./file.txt container_name:/app/
16
17# View processes
18docker top container_name
19
20# Resource stats
21docker stats
22docker stats container_name

#Image Management

bash
1# List images
2docker images
3docker images -a                           # Include intermediates
4docker images --filter "dangling=true"     # Unused images
5
6# Pull images
7docker pull nginx
8docker pull nginx:1.25
9docker pull nginx:1.25-alpine
10
11# Build images
12docker build -t myapp:1.0 .
13docker build -t myapp:1.0 -f Dockerfile.prod .
14docker build --no-cache -t myapp:1.0 .
15
16# Tag images
17docker tag myapp:1.0 myregistry.com/myapp:1.0
18docker tag myapp:1.0 myapp:latest
19
20# Push images
21docker push myregistry.com/myapp:1.0
22
23# Remove images
24docker rmi image_name
25docker rmi $(docker images -q)             # Remove all
26
27# Image history
28docker history nginx
29
30# Save/Load images
31docker save -o myapp.tar myapp:1.0
32docker load -i myapp.tar

#Volumes (Persistent Storage)

bash
1# Create named volume
2docker volume create mydata
3
4# List volumes
5docker volume ls
6
7# Inspect volume
8docker volume inspect mydata
9
10# Use named volume
11docker run -d -v mydata:/app/data nginx
12
13# Bind mount (host directory)
14docker run -d -v $(pwd)/data:/app/data nginx
15docker run -d -v /host/path:/container/path nginx
16
17# Read-only volume
18docker run -d -v mydata:/app/data:ro nginx
19
20# Anonymous volume
21docker run -d -v /app/data nginx
22
23# Remove volume
24docker volume rm mydata
25docker volume prune          # Remove unused

#Volume Use Cases

TypeSyntaxUse Case
Named Volume-v mydata:/dataDatabase storage, shared data
Bind Mount-v /host:/containerDevelopment, config files
tmpfs--tmpfs /tmpTemporary data, secrets

#Networks

#Network Types

DriverDescriptionUse Case
bridgeDefault isolated networkContainer-to-container
hostShare host networkPerformance, no isolation
noneNo networkingSecurity, isolated tasks
overlayMulti-host networkingDocker Swarm

#Network Commands

bash
1# Create network
2docker network create mynet
3docker network create --driver bridge mynet
4
5# List networks
6docker network ls
7
8# Inspect network
9docker network inspect mynet
10
11# Run container on network
12docker run -d --network mynet --name web nginx
13docker run -d --network mynet --name api myapi
14
15# Containers on same network can communicate by name
16docker run --network mynet alpine ping web
17
18# Connect/disconnect running container
19docker network connect mynet container_name
20docker network disconnect mynet container_name
21
22# Remove network
23docker network rm mynet
24docker network prune         # Remove unused

#Container Communication Example

bash
1# Create network
2docker network create app-network
3
4# Start database
5docker run -d \
6  --network app-network \
7  --name db \
8  -e POSTGRES_PASSWORD=secret \
9  postgres
10
11# Start app (connects to db via hostname 'db')
12docker run -d \
13  --network app-network \
14  --name app \
15  -e DATABASE_URL=postgres://postgres:secret@db:5432/postgres \
16  myapp

#Cleanup Commands

bash
1# Remove stopped containers
2docker container prune
3
4# Remove unused images
5docker image prune
6docker image prune -a         # Remove all unused
7
8# Remove unused volumes
9docker volume prune
10
11# Remove unused networks
12docker network prune
13
14# Remove everything unused
15docker system prune
16docker system prune -a        # Include unused images
17docker system prune --volumes # Include volumes
18
19# Check disk usage
20docker system df

#Quick Reference

ActionCommand
Rundocker run -d -p 80:80 --name web nginx
Listdocker ps -a
Stopdocker stop container_name
Logsdocker logs -f container_name
Execdocker exec -it container_name bash
Builddocker build -t myapp:1.0 .
Pushdocker push registry/myapp:1.0
Cleandocker system prune -a

[!TIP] Best Practice: Always use specific image tags (e.g., nginx:1.25) instead of latest in production!

[!IMPORTANT] Security: Never run containers as root in production. Use USER directive in Dockerfile.