Introduction
Docker packages applications and dependencies into standardized units called containers.
Images and Containers
# Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]
# Build image
docker build -t myapp:latest .
# Run container
docker run -d -p 8000:8000 myapp:latest
# List containers
docker ps -a
# Stop container
docker stop container_id
Docker Compose
version: "3.8"
services:
app:
build: .
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgres://db:5432/app
depends_on:
- db
db:
image: postgres:14
environment:
POSTGRES_DB: app
POSTGRES_USER: user
POSTGRES_PASSWORD: password
Practice Problems
- Write Dockerfile for Python app
- Use Docker Compose for multi-container
- Build and tag images
- Manage container lifecycle
- Mount volumes for development