← Back to Python

All Topics

Advertisement

Learn/Python/DevOps

Docker Basics

Topic: Containers

Advertisement

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

  1. Write Dockerfile for Python app
  2. Use Docker Compose for multi-container
  3. Build and tag images
  4. Manage container lifecycle
  5. Mount volumes for development

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →