0
0
Microservicessystem_design~7 mins

Docker basics review in Microservices - System Design Guide

Choose your learning style9 modes available
Problem Statement
Deploying applications directly on servers often leads to inconsistent environments, causing software to work on one machine but fail on another. This inconsistency slows down development and causes unexpected failures in production.
Solution
Docker packages applications and their dependencies into containers that run the same way everywhere. Containers isolate the app from the host system, ensuring consistent behavior across development, testing, and production environments.
Architecture
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Developer PC  │──────▶│ Docker Engine  │──────▶│ Containerized  │
│ (Build & Test)│       │ (Host System) │       │ Application    │
└───────────────┘       └───────────────┘       └───────────────┘

This diagram shows how a developer builds and tests an application on their PC, which runs inside a Docker Engine on the host system, creating a containerized application isolated from the host.

Trade-offs
✓ Pros
Ensures consistent environments across development, testing, and production.
Speeds up deployment by packaging all dependencies together.
Improves resource efficiency compared to full virtual machines.
Simplifies scaling and management of microservices.
✗ Cons
Containers share the host OS kernel, so they are less isolated than virtual machines.
Requires learning Docker commands and concepts, adding initial complexity.
Improper container configuration can lead to security risks.
Use Docker when you need consistent environments across multiple stages, want faster deployment cycles, or are building microservices that require isolated runtime environments.
Avoid Docker if your application requires full OS-level isolation or when running on systems where Docker is unsupported or adds unacceptable overhead.
Real World Examples
Netflix
Netflix uses Docker containers to package microservices, enabling rapid deployment and consistent environments across their cloud infrastructure.
Uber
Uber uses Docker to isolate services and manage dependencies, allowing teams to develop and deploy independently without environment conflicts.
Shopify
Shopify employs Docker containers to streamline development workflows and ensure that applications behave the same from developer laptops to production servers.
Code Example
Before Docker, the app runs directly on the host, which may have different Python versions or missing dependencies. After Docker, the Dockerfile defines a container with a specific Python version and app code, ensuring consistent runtime everywhere.
Microservices
### Before Docker: Running app directly
import time

def run_app():
    print("App is running")
    time.sleep(10)

run_app()

### After Docker: Dockerfile to containerize app
# Use official Python image
FROM python:3.11-slim

# Copy app code
COPY app.py /app/app.py

# Set working directory
WORKDIR /app

# Run the app
CMD ["python", "app.py"]
OutputSuccess
Alternatives
Virtual Machines
Virtual machines emulate entire operating systems, providing stronger isolation but with higher resource use and slower startup times.
Use when: Choose VMs when you need complete OS isolation or must run different operating systems on the same hardware.
Serverless Functions
Serverless abstracts infrastructure completely, running code in response to events without managing containers or servers.
Use when: Choose serverless for event-driven workloads with unpredictable scale and when you want to avoid managing infrastructure.
Summary
Docker solves environment inconsistency by packaging apps with their dependencies into containers.
Containers are lightweight and share the host OS kernel, enabling fast startup and efficient resource use.
Using Docker improves deployment speed and reliability, especially for microservices architectures.