August 2, 2024•7 min read••
Tags ▼
- Docker
- Mcoding
A comprehensive Docker overview covering images, containers, layers, mounts, multi-stage builds, Compose orchestration, and deployment patterns for real apps today.
This tutorial provides a complete, hands-on introduction to Docker, focusing on how to build, run, debug, and orchestrate containerized applications. The core problem addressed is environment inconsistency and dependency conflicts across systems, which Docker solves using isolated containers.
Primary technologies covered:
The tutorial walks through building a multi-container application (frontend + backend + database), managing persistence, optimizing images, and deploying to registries.
Option A (Recommended for beginners):
Option B (Advanced):
Linux (Ubuntu example):
sudo apt update # Add Docker repo (copy from official docs) sudo apt install docker-ce docker-ce-cli containerd.io
Start Docker daemon:
sudo systemctl start docker
docker run hello-world
Check images:
docker image ls
Check containers:
docker ps -a
docker run -p 80:80 nginx
Key flags:
-p host_port:container_port → port mappingRun in background:
docker run -d -p 80:80 nginx
View logs:
docker logs <container_id>
Stop container:
docker stop <container_id>
One image → multiple containers
Run specific version:
docker run nginx:1.27
Pin exact image (recommended for production):
docker run nginx@sha256:<digest>
docker run -e KEY=value python:3.12-slim \ python -c "import os; print(os.environ)"
-e → environment variablesInteractive shell:
docker exec -it <container_id> /bin/bash
Flags:
-i → interactive-t → terminaldocker run -v my_volume:/data nginx
docker run -v $(pwd)/data:/data nginx
Differences:
Dockerfile:
FROM nginx:1.27 COPY static/ /usr/share/nginx/html RUN rm /usr/share/nginx/html/50x.html
Build:
docker build -t my-nginx .
Run:
docker run -p 80:80 my-nginx
Dockerfile:
FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY src/ . EXPOSE 8000 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Key rules:
COPY requirements.txt . RUN pip install -r requirements.txt COPY src/ .
FROM python:3.12-slim AS builder COPY . . RUN pip wheel . FROM python:3.12-slim AS runner COPY --from=builder /wheels /wheels RUN pip install /wheels/*
Benefits:
docker-compose.yml:
services: backend: build: context: ./backend dockerfile: Dockerfile ports: - "8000:8000" frontend: build: context: ./frontend ports: - "80:80" mongodb: image: mongo:7.0.12 volumes: - mongodb_data:/data/db volumes: mongodb_data:
Run everything:
docker compose up
Stop & remove:
docker compose down
.env.env file:
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=example
In compose:
env_file: - ./mongodb/.env
Connection string:
mongodb://root:example@mongodb:27017
mongodb → service name (internal DNS)Tag image:
docker tag my-image username/my-image:1.0
Login:
docker login
Push:
docker push username/my-image:1.0
Port mapping: -p host:container
Detach mode: -d
Auto-remove container: --rm
Interactive shell: -it
Volumes vs Bind mounts:
Layer caching rules:
Multi-stage builds: reduce size + improve security
Compose networking:
Security note: Docker runs with root privileges by default
Use digest pinning (sha256) for production stability
Prefer slim or alpine images for smaller size
Always split:
Use --no-cache-dir with pip to reduce size
Use Docker Compose instead of manual container management
Add database UI tools (e.g., Mongo Express) for debugging
Use retry logic instead of depends_on for startup ordering
Security risks:
Alpine Linux differences:
musl instead of glibcbashapk)Secrets handling:
Layer history:
Networking issues:
Official Docker Docs (installation & CLI reference)
Docker Hub (image registry exploration)
FastAPI documentation (API building)
MongoDB docs (data persistence & queries)
Cloud container platforms:
“Docker Deep Dive” — Nigel Poulton A highly practical guide focused on real-world Docker usage. It explains containers, networking, volumes, and orchestration with clarity, making it ideal for reinforcing the implementation patterns shown in this tutorial.
“The Docker Book” — James Turnbull Covers Docker fundamentals through advanced usage including image building, registries, and deployment pipelines. Especially useful for understanding production workflows and Docker internals.
“Container Security” — Liz Rice Focuses on security aspects of containers, including namespaces, isolation, and vulnerabilities. This directly supports the tutorial’s warnings about root access, layers, and secrets management.
“Kubernetes Up & Running” — Kelsey Hightower et al. While Docker Compose is used here, this book helps you transition to Kubernetes for production orchestration, scaling, and deployment of containerized apps.
“Python Web Development with FastAPI” — Bill Lubanovic A practical guide to building APIs with FastAPI, complementing the backend portion of the tutorial with deeper implementation details and production-ready patterns.
Follow on your preferred channel for new articles, notes, and experiments.