Docker makes it easy to create, deploy, and run applications in containers, ensuring consistency across environments.
Basic Dockerfile
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Essential Docker Commands
# Build image
docker build -t myapp:latest .
# Run container
docker run -d -p 8000:8000 myapp:latest
# List containers
docker ps
# Stop container
docker stop container_id
# Remove container
docker rm container_id
Docker Compose
version: '3.8'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- DEBUG=1
db:
image: postgres:13
environment:
- POSTGRES_PASSWORD=secret
Best Practices
# Multi-stage build for smaller images
FROM python:3.9 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.9-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
CMD ["python", "app.py"]
Docker ensures your app runs the same everywhere!