What is Docker?
Docker is a containerization platform that packages an application along with its runtime, libraries, dependencies, and configuration into a container.Build once, run anywhere.
Why Docker?
- Eliminates “Works on my machine” issues
- Provides consistent environments
- Isolates application dependencies
- Simplifies deployment
- Makes applications portable across cloud platforms
Container
A container is a lightweight, isolated runtime environment that contains:- Application code
- Runtime (Python)
- Dependencies
- Configuration
Virtual Machine vs Docker
Virtual Machine
Each Virtual Machine runs its own operating system.Docker Container
Containers share the host operating system kernel.Comparison
Virtual Machines virtualize an entire operating system, whereas Docker virtualizes only the application.
Docker Architecture
Image
A Docker Image is a read-only template used to create containers.Dockerfile
A Dockerfile contains instructions to build a Docker image. Common instructions:
Example:
Container Lifecycle
Registry
A Docker Registry stores Docker images. Popular registries:- Docker Hub
- GitHub Container Registry
- Amazon ECR
- Google Artifact Registry
Port Mapping
Maps a host port to a container port.Environment Variables
Store configuration outside the application. Examples:- Database URL
- API Keys
- JWT Secret
Volumes
Volumes store data outside containers, allowing data to persist even after a container is removed. Typical use cases:- PostgreSQL data
- Uploaded files
- Logs
Bind Mount
A bind mount shares a local directory with a container, making code changes immediately available inside the container.Networks
Docker Networks allow containers to communicate securely.Docker Compose
What is Docker Compose?
Docker Compose manages multiple containers using a single configuration file (docker-compose.yml).
Instead of starting containers one by one, Compose starts the entire application stack with a single command.
Why Docker Compose?
Ideal for applications with multiple services such as:- FastAPI
- PostgreSQL
- Redis
- Nginx
Compose File
Thedocker-compose.yml file defines:
- Services
- Images
- Ports
- Environment variables
- Volumes
- Networks
- Dependencies
Common Compose Options
Multi-Container Architecture
Docker Workflow
Docker Compose Workflow
Deployment Workflow
Best Practices
- Use lightweight base images (
python:3.x-slim) - Keep secrets in
.env - Use volumes for persistent data
- Use Docker Compose for multi-container applications
- Keep images small with
.dockerignore - Pin dependency versions
- Avoid storing persistent data inside containers
Summary
- Docker packages applications into portable containers.
- Containers are lightweight and share the host operating system.
- Docker Images are blueprints used to create containers.
- Dockerfile defines how images are built.
- Volumes preserve data beyond a container’s lifetime.
- Networks enable communication between containers.
- Docker Compose orchestrates multiple containers from a single configuration file.
- Docker and Docker Compose provide a consistent, scalable, and production-ready environment for developing and deploying FastAPI applications.