Introduction to Docker
Docker is an open-source platform that enables developers to build, deploy, and run applications in containers. Containers package an application with all its dependencies, ensuring it runs consistently across different environments (development, testing, production).
Unlike virtual machines (VMs), Docker containers share the host OS kernel, making them lightweight, fast, and efficient.
Why Use Docker?
✔ Consistency – Works the same everywhere.
✔ Isolation – Apps run in separate containers.
✔ Portability – Run on any OS (Linux, Windows, macOS).
✔ Scalability – Easily deploy multiple instances.
✔ Efficiency – Uses fewer resources than VMs.
Docker Architecture: How It Works
Docker follows a client-server architecture:
1. Docker Daemon (dockerd
)
- Runs in the background and manages containers.
- Handles building, running, and distributing containers.
2. Docker Client (docker
CLI)
- The primary way users interact with Docker.
- Sends commands to the Docker daemon.
3. Docker Images
- Read-only templates used to create containers.
- Built from a
Dockerfile
(a script defining the environment).
4. Docker Containers
- Runnable instances of an image.
- Isolated processes with their own filesystem and networking.
5. Docker Registry (Docker Hub)
- A cloud repository for storing and sharing Docker images.
- Public (Docker Hub) or private (AWS ECR, Azure Container Registry).
Key Docker Concepts Explained
1. Dockerfile
A text file containing instructions to build a Docker image.
Example:
# Use an official Python image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY . .
# Run the app
CMD ["python", "app.py"]
2. Docker Image vs. Container
Docker Image | Docker Container |
---|---|
Blueprint (template) | Running instance of an image |
Immutable (read-only) | Writable (temporary changes) |
Stored in a registry | Lives in memory while running |
3. Volumes (Persistent Storage)
- Containers are ephemeral (data is lost when they stop).
- Volumes allow persistent storage for databases, logs, etc.
Example:
docker run -v /host/path:/container/path my-image
4. Docker Compose (Multi-Container Apps)
- Defines and runs multi-container apps using a YAML file.
- Useful for microservices (e.g., web app + database).
Example docker-compose.yml
:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: postgres:13
environment:
POSTGRES_PASSWORD: example
How to Install Docker
On Linux (Ubuntu)
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo docker --version
On macOS / Windows
- Download Docker Desktop from docker.com.
Basic Docker Commands Cheat Sheet
Command | Description |
---|---|
docker pull nginx | Download an image from Docker Hub |
docker run -d -p 80:80 nginx | Run a container in detached mode |
docker ps | List running containers |
docker stop <container_id> | Stop a container |
docker rm <container_id> | Remove a container |
docker images | List downloaded images |
docker rmi <image_id> | Delete an image |
docker build -t my-app . | Build an image from a Dockerfile |
docker logs <container_id> | View container logs |
docker exec -it <container_id> bash | Enter a running container |
Docker Use Cases
🚀 Microservices Architecture – Deploy scalable, independent services.
🚀 CI/CD Pipelines – Consistent testing & deployment environments.
🚀 Development Environments – No more “works on my machine” issues.
🚀 Cloud-Native Apps – Ideal for Kubernetes, AWS ECS, Azure ACI.
🚀 Legacy App Modernization – Containerize old apps for cloud migration.
Docker vs. Virtual Machines (VMs)
Feature | Docker Containers | Virtual Machines |
---|---|---|
Performance | Near-native (shares OS) | Slower (full OS emulation) |
Startup Time | Seconds | Minutes |
Resource Usage | Lightweight | Heavy (requires full OS) |
Isolation | Process-level | Full hardware-level |
Use Case | Cloud-native apps | Legacy apps needing full OS |
Best Practices for Docker
✔ Use .dockerignore
– Exclude unnecessary files.
✔ Keep images small – Use Alpine Linux variants.
✔ One process per container – Follow microservices principles.
✔ Use environment variables for configuration.
✔ Scan images for vulnerabilities (e.g., docker scan
).
Conclusion: Why Docker is a Game-Changer
Docker revolutionized software deployment by making apps portable, scalable, and consistent. Whether you’re a developer, DevOps engineer, or cloud architect, mastering Docker is essential for modern software development.
Ready to start? Install Docker and run your first container today!