🐳 Docker Training Guide
Setup · Images · Containers · Compose
A Beginner-to-Intermediate Student Training Session
Docker Training Guide
1. What is Docker?
Docker is an open-source platform that lets you package, ship, and run applications inside lightweight,
isolated environments called containers. Containers bundle your app with all its dependencies so it runs
the same everywhere — development, testing, or production.
💡 Key Concept
Think of a Docker container like a shipping container — it holds everything the app needs, and works
the same no matter where it's deployed.
Core Concepts
• Image: A read-only blueprint/template for creating containers (like a class in OOP).
• Container: A running instance of an image (like an object from a class).
• Dockerfile: A text file with instructions to build a Docker image.
• Docker Hub: A public registry to share and pull Docker images.
• Docker Compose: A tool to define and run multi-container applications.
2. Installing Docker on Windows
Docker Desktop is the recommended way to run Docker on Windows. It includes the Docker Engine,
CLI, and Docker Compose.
System Requirements
• Windows 10 64-bit (Pro, Enterprise, or Education) — Build 19041 or later
• Windows 11 (any edition)
• WSL 2 (Windows Subsystem for Linux 2) enabled
• Hyper-V and Containers Windows features enabled
• At least 4 GB of RAM
Step-by-Step Installation
Step 1 — Enable WSL 2
Open PowerShell as Administrator and run:
# Enable WSL
wsl --install
# Set WSL 2 as default version
wsl --set-default-version 2
Docker Training Guide
Restart your computer after running these commands.
Step 2 — Download Docker Desktop
Go to [Link] and download the Docker Desktop installer for
Windows. Run the installer and follow the on-screen prompts.
💡 Installation Tip
During installation, ensure 'Use WSL 2 instead of Hyper-V' is checked (recommended). Also check
'Add shortcut to desktop' for convenience.
Step 3 — Start Docker Desktop
Launch Docker Desktop from the Start Menu. Wait for the Docker Engine to start — you will see a
green status indicator in the system tray when it is ready.
Step 4 — Verify Installation
Open Command Prompt or PowerShell and run:
docker --version
# Expected output: Docker version 24.x.x, build xxxxxxx
docker info
# Shows detailed system information about the Docker installation
Step 5 — Run the Hello World Test
Confirm Docker is working correctly:
docker run hello-world
# Expected output:
# Hello from Docker!
# This message shows that your installation appears to be working correctly.
Docker Training Guide
3. Creating Your First Docker Image
A Dockerfile is a set of instructions that Docker uses to build an image. Let's create a simple Python
web application and containerize it.
Project Structure
my-docker-app/
├── [Link]
├── [Link]
└── Dockerfile
Step 1 — Create the Application Files
[Link] — Simple Flask Web App
from flask import Flask
app = Flask(__name__)
@[Link]('/')
def home():
return '<h1>Hello from Docker! 🐳</h1>'
@[Link]('/health')
def health():
return {'status': 'healthy'}
if __name__ == '__main__':
[Link](host='[Link]', port=5000, debug=False)
[Link] — Python Dependencies
flask==3.0.0
gunicorn==21.2.0
Step 2 — Write the Dockerfile
# Use official Python base image
FROM python:3.11-slim
# Set working directory inside the container
WORKDIR /app
# Copy dependency file first (for layer caching)
COPY [Link] .
# Install Python dependencies
RUN pip install --no-cache-dir -r [Link]
Docker Training Guide
# Copy the rest of the application code
COPY . .
# Expose the port the app listens on
EXPOSE 5000
# Command to run the application
CMD ["python", "[Link]"]
💡 Best Practice — Layer Caching
Each instruction in a Dockerfile creates a new layer in the image. Docker caches these layers, so
copying [Link] before the source code means the pip install step is cached until
[Link] changes.
Step 3 — Build the Docker Image
Navigate to your project folder and run:
# Navigate to project directory
cd my-docker-app
# Build the image
# -t assigns a name (tag) to the image
# The dot (.) means use the current directory as build context
docker build -t my-flask-app:v1 .
# You will see output like:
# [1/4] FROM python:3.11-slim
# [2/4] RUN pip install ...
# Successfully built abc123def456
# Successfully tagged my-flask-app:v1
Step 4 — List Your Images
docker images
# Output:
# REPOSITORY TAG IMAGE ID CREATED SIZE
# my-flask-app v1 abc123def456 2 minutes ago 145MB
Docker Training Guide
4. Running Docker Containers
Run a Container
# Basic run command
docker run my-flask-app:v1
# Run in detached mode (background) with port mapping
# -d : detached/background mode
# -p : map host port 8080 to container port 5000
# --name : give the container a friendly name
docker run -d -p 8080:5000 --name my-app my-flask-app:v1
# Open browser at: [Link]
Common Run Options
Command Description
-d Detached mode — run container in the background
-p host:container Map a host port to a container port
--name <name> Assign a custom name to the container
-e KEY=VALUE Set an environment variable inside the container
-v host:container Mount a volume (share files between host and container)
--rm Automatically remove container when it exits
-it Interactive mode with terminal (for debugging)
--restart always Automatically restart the container if it crashes
Managing Running Containers
List Containers
# List only running containers
docker ps
# List ALL containers (including stopped ones)
docker ps -a
# Output columns:
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Docker Training Guide
Stop, Start, and Restart
# Stop a running container
docker stop my-app
# Start a stopped container
docker start my-app
# Restart a container
docker restart my-app
# Forcefully kill a container (immediate)
docker kill my-app
Remove Containers and Images
# Remove a stopped container
docker rm my-app
# Remove a running container (force)
docker rm -f my-app
# Remove an image
docker rmi my-flask-app:v1
# Remove all stopped containers, dangling images, unused networks
docker system prune
Docker Training Guide
5. Viewing Container Logs
Logs are the primary way to debug and monitor what is happening inside a running container.
# View all logs from a container
docker logs my-app
# Follow/stream logs in real-time (like tail -f)
docker logs -f my-app
# Show last 50 log lines
docker logs --tail 50 my-app
# Show logs with timestamps
docker logs -t my-app
# Show logs since a specific time
docker logs --since 30m my-app
# Combine options: last 100 lines with timestamps
docker logs --tail 100 -t my-app
💡 Tip
Use 'docker logs -f' during development to stream real-time output. Press Ctrl+C to stop following.
6. Inspecting Container Details
Docker provides several commands to get detailed information about running containers, images, and
networks.
docker inspect
Returns detailed JSON with all configuration details of a container or image.
# Full inspect output (JSON format)
docker inspect my-app
# Get only the container's IP address
docker inspect -f '{{[Link]}}{{.IPAddress}}{{end}}'
my-app
# Get environment variables
docker inspect -f '{{.[Link]}}' my-app
# Get mounted volumes
docker inspect -f '{{.Mounts}}' my-app
Docker Training Guide
docker stats
Live CPU, memory, and network usage of running containers.
# Real-time stats for ALL running containers
docker stats
# Stats for a specific container
docker stats my-app
# One-time snapshot (no streaming)
docker stats --no-stream my-app
docker exec — Run Commands Inside a Container
# Open an interactive bash shell inside the container
docker exec -it my-app bash
# Or if bash is not available (Alpine-based images)
docker exec -it my-app sh
# Run a single command without interactive shell
docker exec my-app env
docker exec my-app ls -la /app
docker top — View Processes in Container
# List running processes inside a container
docker top my-app
Docker Training Guide
7. Docker Compose
Docker Compose is a tool for defining and running multi-container applications. You describe your
entire application stack in a single [Link] file.
💡 Note
Docker Compose is included with Docker Desktop. You can verify by running: docker compose
version
When to Use Docker Compose
• Your app has multiple services (e.g., web server + database + cache)
• You need to manage service dependencies and startup order
• You want to define all configuration in one place
• You need consistent environments across dev, test, and CI
[Link] Structure
Below is a complete example — a Flask web app connected to a PostgreSQL database and Redis
cache:
version: '3.8'
services:
# ── Web Application Service ──────────────────────────────
web:
build: . # Build image from local Dockerfile
container_name: flask-app
ports:
- '8080:5000' # host:container port mapping
environment:
- FLASK_ENV=development
- DATABASE_URL=postgresql://user:password@db:5432/mydb
- REDIS_URL=redis://redis:6379/0
volumes:
- .:/app # Mount local code for hot reload
depends_on:
- db
- redis
networks:
- app-network
restart: unless-stopped
# ── PostgreSQL Database Service ───────────────────────────
db:
image: postgres:15 # Use official Postgres image
container_name: postgres-db
environment:
- POSTGRES_USER=user
Docker Training Guide
- POSTGRES_PASSWORD=password
- POSTGRES_DB=mydb
volumes:
- postgres_data:/var/lib/postgresql/data # Persist DB data
ports:
- '5432:5432' # Expose DB port (optional, for local tools)
networks:
- app-network
restart: unless-stopped
# ── Redis Cache Service ───────────────────────────────────
redis:
image: redis:7-alpine
container_name: redis-cache
ports:
- '6379:6379'
networks:
- app-network
restart: unless-stopped
# ── Named Volumes ─────────────────────────────────────────
volumes:
postgres_data:
# ── Networks ──────────────────────────────────────────────
networks:
app-network:
driver: bridge
Docker Compose Commands
Starting and Stopping
# Start all services defined in [Link]
docker compose up
# Start in detached (background) mode
docker compose up -d
# Rebuild images before starting
docker compose up --build
# Start only specific services
docker compose up -d web redis
# Stop all services (containers remain)
docker compose stop
# Stop and remove containers, networks
docker compose down
# Stop and remove containers, networks, AND volumes (deletes DB data!)
docker compose down -v
Docker Training Guide
Monitoring and Debugging
# View status of all services
docker compose ps
# View logs from all services
docker compose logs
# Follow logs from all services
docker compose logs -f
# View logs from a specific service
docker compose logs -f web
# Open shell inside a running service
docker compose exec web bash
# Run a one-off command in a service container
docker compose run web python [Link] migrate
Scaling and Management
# Scale a service to multiple instances
docker compose up -d --scale web=3
# Restart a specific service
docker compose restart web
# Pull latest images for all services
docker compose pull
# Show resource usage for compose services
docker compose top
# Validate and view the compose file
docker compose config
# Build or rebuild services
docker compose build
docker compose build --no-cache web
Docker Training Guide
8. Quick Reference Cheat Sheet
Docker Core Commands
Command Description
docker build -t name . Build image from Dockerfile in current directory
docker images List all local images
docker pull <image> Download an image from Docker Hub
docker push <image> Upload an image to a registry
docker rmi <image> Remove a local image
docker run -d -p 8080:80 <img> Run container detached with port mapping
docker ps List running containers
docker ps -a List all containers (incl. stopped)
docker stop <name> Gracefully stop a container
docker rm <name> Remove a stopped container
docker logs -f <name> Stream container logs
docker exec -it <name> bash Open shell inside running container
docker inspect <name> Show detailed container/image info (JSON)
docker stats Live resource usage of containers
docker system prune Remove all unused containers, images, networks
Docker Compose Commands
Command Description
docker compose up -d Start all services in background
docker compose up --build Rebuild and start services
docker compose down Stop and remove containers and networks
docker compose down -v Also remove named volumes
docker compose ps List status of services
docker compose logs -f Stream logs from all services
docker compose logs -f web Stream logs from specific service
docker compose exec web bash Shell into a running service
docker compose run web <cmd> Run one-off command in service
Docker Training Guide
docker compose restart web Restart a specific service
docker compose build --no-cache Full rebuild without cache
docker compose config Validate and print resolved compose file
9. Best Practices & Tips
✅ Best Practices
1. Use .dockerignore to exclude unnecessary files (like node_modules or .git) from the build context.
2. Use specific image tags (e.g., python:3.11-slim) instead of 'latest' for reproducibility. 3. Keep
images small — use slim/alpine base images and clean up package caches in the same RUN layer.
4. Never store secrets in Dockerfiles or [Link] — use environment files or Docker
secrets. 5. Use named volumes for database data persistence so data survives container recreation.
6. Use health checks in production to allow orchestrators to detect unhealthy containers. 7. Set --
restart unless-stopped on production containers so they recover from crashes.
Sample .dockerignore File
__pycache__/
*.pyc
*.pyo
.env
.git
.gitignore
node_modules/
*.log
*.md
tests/
Happy Dockerizing! 🐳
Docker Training Guide