Docker for Absolute Beginners
What is Docker?
Docker is a containerization platform that packages an application and all its dependencies (libraries, runtime, code) into a single unit called a container. This ensures applications run the same way everywhere-on a developer's machine, a colleague's computer, or a production server.
Think of it like a shipping container for software!
Key Definitions
Container
A lightweight, standalone package that includes:
- Application code
- All dependencies (Node.js, Python, databases, etc.)
- Operating system libraries
- Configuration files
Containers are isolated from each other and from the host system.
Image
A blueprint or template for creating containers. It's like a recipe that defines what should be inside a container.
Docker Engine
The runtime that executes and manages containers on the host machine.
Registry
A repository where Docker images are stored. The most popular is Docker Hub (like GitHub for Docker images).
Dockerfile
A text file with instructions to build a Docker image. It's like a step-by-step recipe.
How Docker Works (Visual Flow)
graph LR
A["Dockerfile
(Recipe)"] -->|docker build| B["Docker Image
(Blueprint)"]
B -->|docker run| C["Container
(Running App)"]
C -->|ports/volumes| D["Host Machine"]
B -->|docker push| E["Docker Hub
(Registry)"]
E -->|docker pull| F["Another Machine"]
Docker vs Virtual Machines
graph TD
subgraph VM["Virtual Machines"]
direction LR
Host1["Host OS"]
Hyper["Hypervisor"]
Guest1["Guest OS 1
App A"]
Guest2["Guest OS 2
App B"]
end
subgraph Docker["Docker Containers"]
direction LR
Host2["Host OS"]
Engine["Docker Engine"]
Con1["Container A
App A"]
Con2["Container B
App B"]
end
style VM fill:#ffcccc
style Docker fill:#ccffcc
Key Difference: Docker containers share the host OS kernel (lightweight), while VMs have their own OS (heavier).
Simple Example: Python Web App
Step 1: Create a Simple Python App
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Docker!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Step 2: Create a Dockerfile
# Use an official Python image as base
FROM python:3.9-slim
# Set working directory inside container
WORKDIR /app
# Copy app files into container
COPY app.py .
# Install dependencies
RUN pip install flask
# Expose port 5000
EXPOSE 5000
# Run the app
CMD ["python", "app.py"]
Step 3: Build the Image
docker build -t my-python-app .
Step 4: Run the Container
docker run -p 5000:5000 my-python-app
Navigate to http://localhost:5000 to verify the application is running.
Common Docker Commands
| Command | What it does |
|---|---|
docker build -t name . |
Build an image from Dockerfile |
docker run -p 8080:80 image |
Run a container from an image |
docker ps |
List running containers |
docker stop container_id |
Stop a running container |
docker images |
List all images |
docker pull image_name |
Download an image from registry |
docker push image_name |
Upload an image to registry |
Docker Workflow
graph LR
A["1. Write
Dockerfile"] -->B["2. Build
Image"]
B -->C["3. Test
Locally"]
C -->D{Works?}
D -->|No| A
D -->|Yes| E["4. Push to
Registry"]
E -->F["5. Deploy
to Server"]
F -->G["6. Run
Container"]
Benefits of Docker
- Consistency - App runs the same everywhere
- Isolation - Containers don't interfere with each other
- Lightweight - Containers are small and fast
- Easy Deployment - Share image, run anywhere
- Version Control - Track image versions like code
- Easy Scaling - Run multiple containers of same image
Real-World Example: Using Pre-built Images
Don't always build from scratch! Docker Hub has thousands of ready-made images.
Run Nginx (Web Server) in 10 seconds
docker run -p 8080:80 nginx
Navigate to http://localhost:8080 to verify Nginx is running successfully.
Run MongoDB (Database)
docker run -p 27017:27017 mongo
The database is now ready for connections.
Docker Compose (Multiple Containers)
For apps with multiple services (web app + database), use Docker Compose:
# docker-compose.yml
version: '3'
services:
web:
image: my-python-app
ports:
- "5000:5000"
depends_on:
- db
db:
image: mongo
ports:
- "27017:27017"
Run everything with one command:
docker-compose up
Architecture: How Containers Work
graph TD
A["Host Machine"] -->B["Host OS Kernel
Linux/Windows"]
B -->C["Docker Engine
(Daemon)"]
C -->D["Container 1
Node.js App"]
C -->E["Container 2
Python App"]
C -->F["Container 3
MongoDB"]
D -->G["Isolated
Filesystem"]
E -->H["Isolated
Filesystem"]
F -->I["Isolated
Filesystem"]
style A fill:#e1f5ff
style D fill:#fff3e0
style E fill:#fff3e0
style F fill:#fff3e0
Getting Started Checklist
Quick Reference: Dockerfile Instructions
| Instruction | Purpose | Example |
|---|---|---|
FROM |
Base image | FROM python:3.9 |
WORKDIR |
Set work directory | WORKDIR /app |
COPY |
Copy files in | COPY . . |
RUN |
Execute command | RUN pip install flask |
EXPOSE |
Declare port | EXPOSE 5000 |
ENV |
Set environment variable | ENV NODE_ENV=production |
CMD |
Default command | CMD ["python", "app.py"] |
Key Takeaways
- Docker solves the "works on my machine" problem through containerization
- Containers are lightweight, isolated packages containing applications and dependencies
- Images are blueprints for containers; registries store images
- Dockerfiles define step-by-step instructions for building images
- Multi-stage builds with Docker Compose manage complex, multi-service applications
- Start with pre-built images from Docker Hub, then progress to custom Dockerfiles
- Containerization ensures consistent application behavior across development, testing, and production
Next Steps: Install Docker, verify with docker run hello-world, and begin building containerized applications.