Day 1: Introduction to Containers and Docker

Learning Objectives: By the end of this lesson, you will be able to:

  • Understand what containers are and how they differ from virtual machines
  • Explain the benefits of containerization in modern software development
  • Identify the key components of Docker ecosystem
  • Recognize common use cases for Docker in development and deployment

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Think about the last time you tried to run someone else’s code on your machine and encountered the dreaded “but it works on my machine” problem. How much time did you spend trying to figure out dependency conflicts, different operating system behaviors, or missing libraries?

Lesson Overview

  • Duration: 45-60 minutes
  • Prerequisites: Basic understanding of software development concepts
  • Materials Needed: Computer with internet access (Docker installation comes tomorrow)

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. The Problem Docker Solves - Understanding the “dependency hell” and environment inconsistencies
  2. What Are Containers? - Lightweight, portable application packaging
  3. Docker vs. Virtual Machines - Key differences and advantages
  4. Docker Ecosystem Overview - Images, containers, registries, and more
  5. Real-world use cases - Where Docker shines in modern development

Success Criteria:

By the end of this session, you should be able to explain containerization concepts to a colleague and identify scenarios where Docker would be beneficial.

3. Foundation Knowledge (15-20 minutes)

Core Concepts

The Software Deployment Challenge

Imagine you’re a chef who creates amazing recipes. You want to share your recipes with other chefs around the world, but here’s the problem: every kitchen is different. Some have gas stoves, others electric. Some have metric measurements, others imperial. Some have fresh ingredients readily available, others rely on preserved foods.

Real-World Analogy: Docker is like providing each chef with a complete, standardized kitchen-in-a-box that includes all the right tools, ingredients, and cooking environment needed to perfectly recreate your recipe, regardless of the building they’re cooking in.

Why This Matters: In software development, applications need specific versions of programming languages, libraries, databases, and system configurations. These requirements often conflict between different applications or change over time, leading to the infamous “dependency hell.”

What Are Containers?

Containers are lightweight, standalone packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings. Unlike installing software directly on your operating system, containers provide an isolated environment that’s consistent across different machines.

Key Characteristics:

  • Isolated: Each container runs independently, with its own file system and network
  • Portable: Runs the same way on any machine that supports containers
  • Lightweight: Shares the host OS kernel, using fewer resources than virtual machines
  • Consistent: Same behavior in development, testing, and production environments

Key Terminology

  • Container: A running instance of an image with its own isolated environment
  • Image: A read-only template used to create containers (like a blueprint)
  • Docker: A platform that implements container technology with additional tooling
  • Host: The machine running the Docker engine and containers
  • Registry: A service for storing and distributing container images (like Docker Hub)

4. Detailed Explanation and Examples (25-30 minutes)

Understanding Containerization Technology

Containerization represents a fundamental shift in how we package and deploy applications. Traditional deployment methods require installing software directly on the host operating system, which creates several challenges:

  1. Dependency Conflicts: Different applications may require different versions of the same library
  2. Environment Drift: Development, testing, and production environments gradually become different
  3. Resource Waste: Applications may not fully utilize allocated resources
  4. Scaling Difficulties: Adding more instances requires complex configuration management

Containers solve these problems by packaging applications with their dependencies in isolated, portable units.

How Containers Work Under the Hood

Containers leverage several Linux kernel features:

  • Namespaces: Provide isolation for processes, networks, file systems, and other resources
  • Control Groups (cgroups): Limit and monitor resource usage (CPU, memory, disk I/O)
  • Union File Systems: Enable efficient layering and sharing of file system components

This technology allows multiple containers to run on the same host while maintaining complete isolation between them.

Docker vs. Virtual Machines: A Detailed Comparison

Virtual Machines Architecture:

[App A] [App B] [App C]
[Guest OS] [Guest OS] [Guest OS]
[Hypervisor]
[Host Operating System]
[Physical Hardware]

Docker Containers Architecture:

[App A] [App B] [App C]
[Docker Engine]
[Host Operating System]
[Physical Hardware]

Key Differences:

Aspect Virtual Machines Docker Containers
Resource Usage High (each VM needs full OS) Low (shares host OS kernel)
Startup Time Minutes Seconds
Isolation Complete OS isolation Process-level isolation
Portability Limited by hypervisor compatibility High across different platforms
Performance Some overhead from virtualization Near-native performance

Common Misconceptions

  1. Misconception: Containers are less secure than VMs because they share the kernel Reality: Containers provide strong isolation through kernel namespaces and cgroups. While VMs offer stronger isolation, containers can be equally secure with proper configuration.

  2. Misconception: Docker is only useful for large, complex applications Reality: Docker benefits applications of all sizes by ensuring consistency and simplifying deployment processes.

5. Live Coding Demonstration (20-25 minutes)

Example 1: Traditional Deployment Problems

Let’s imagine deploying a Python web application traditionally:

# On Developer's Machine (Python 3.9, pip 21.0)
pip install flask==2.0.1
python app.py  # Works perfectly

# On Production Server (Python 3.7, pip 19.0)
pip install flask==2.0.1  # Fails due to dependency conflicts
# Requires manual troubleshooting, version management, etc.

Problems Illustrated:

  • Different Python versions between environments
  • Dependency version conflicts
  • Missing system libraries
  • Configuration differences

Example 2: Container Solution Concept

# Dockerfile - Blueprint for our container
FROM python:3.9-slim
COPY app.py requirements.txt ./
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
# Build once, run anywhere
docker build -t my-app .
docker run my-app  # Identical behavior everywhere

Key Points:

  • Same Python version everywhere (3.9-slim)
  • Isolated dependency installation
  • Consistent runtime environment
  • Single artifact for all environments

Example 3: Real-World Workflow

# docker-compose.yml - Multi-service application
version: '3.8'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - database
  
  database:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

This configuration ensures that every developer gets the exact same web server and database setup with a single command: docker-compose up

6. Hands-On Practice (30-40 minutes)

Exercise 1: Identify Container Use Cases (10 minutes)

Objective: Recognize scenarios where containerization provides clear benefits

Instructions:

  1. Read through the following scenarios
  2. For each scenario, determine if containers would be beneficial and why
  3. Identify the specific problems containers would solve

Scenarios:

  1. A team of 10 developers working on a microservices application with 5 different services
  2. A data science team sharing Jupyter notebooks with complex Python dependencies
  3. A startup deploying a simple static website
  4. A company running legacy applications that require specific old library versions
  5. An e-commerce platform that needs to scale rapidly during Black Friday sales

Discussion Points:

  • Which scenarios benefit most from containerization?
  • What specific problems would containers solve in each case?
  • Are there scenarios where containers might be overkill?

Exercise 2: Docker Ecosystem Exploration (15 minutes)

Objective: Familiarize yourself with Docker’s ecosystem and terminology

Instructions:

  1. Visit Docker Hub in your browser
  2. Search for popular images: nginx, python, mysql, node
  3. For each image, note:
    • Number of downloads
    • Available tags/versions
    • Official vs. community maintained
    • Basic usage instructions

Questions to Consider:

  • What makes an image “official”?
  • How do you choose between different tags?
  • What information would you need before using an image in production?

Exercise 3: Planning Your First Container (15 minutes)

Objective: Think through containerizing a real application

Challenge: Choose a simple application you’ve worked on (or a hypothetical one) and plan how you would containerize it.

Planning Template:

Application: [Your app name]
Programming Language/Framework: [e.g., Python/Flask, Node.js/Express]
Dependencies: [List major dependencies]
Configuration: [Environment variables, config files]
Data Storage: [Files, databases]
External Services: [APIs, third-party services]

Containerization Plan:
- Base Image: [What would you start with?]
- Install Dependencies: [How would you handle this?]
- Configuration: [How would you manage config?]
- Data Persistence: [How would you handle data?]
- Networking: [What ports need to be exposed?]

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Confusing containers with virtual machines Cause: Similar isolation concepts but different implementations Solution: Remember that containers share the host OS kernel and are more lightweight

  2. Problem: Thinking Docker solves all deployment problems Cause: Overestimating container capabilities Solution: Understand that containers solve consistency and portability, but you still need orchestration, monitoring, and security

  3. Problem: Assuming containers are inherently more secure Cause: Misunderstanding isolation levels Solution: Recognize that container security requires proper configuration and best practices

Conceptual Clarity Tips

  • Containers are about consistency and portability, not just isolation
  • Docker is a tool that implements containerization, not the only container technology
  • Containers work best for stateless applications; stateful services require additional considerations

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Start Small: Begin with simple, stateless applications before tackling complex, stateful services
  • Image Strategy: Use official images as base layers when possible
  • Documentation: Always document your container requirements and build process

When to Use Containers

  • Microservices architectures
  • Development environment standardization
  • Continuous integration/deployment pipelines
  • Application migration and modernization
  • Testing with different environment configurations

When Containers Might Not Be the Best Choice

  • Simple static websites (traditional hosting might be simpler)
  • Legacy applications with tight OS integration
  • High-performance computing requiring maximum resource access
  • Applications requiring specific hardware drivers

9. Summary and Review (5 minutes)

Key Takeaways

  1. Containers solve the “works on my machine” problem by packaging applications with their dependencies
  2. Docker is lighter weight than VMs because containers share the host OS kernel
  3. Containerization enables consistency across development, testing, and production environments

Quick Review Questions

  1. What’s the main difference between a container and a virtual machine?
  2. Name three benefits of using containers in software development
  3. In what scenarios might traditional deployment be preferable to containers?

For Instructors

Teaching Tips

  • Use plenty of analogies to explain abstract concepts (kitchen, shipping containers, apartments vs. houses)
  • Encourage students to think about their own development pain points
  • Address the “complexity vs. benefits” concern that beginners often have

Assessment Ideas

  • Have students explain containerization to someone unfamiliar with the concept
  • Quiz on VM vs. container differences
  • Scenario analysis exercises for identifying appropriate use cases

Adaptation Notes

  • For complete beginners: Spend more time on the fundamental concepts and analogies
  • For experienced developers: Focus more on architectural patterns and decision-making criteria
  • Online delivery: Use breakout rooms for discussion exercises
  • Adjust timing based on group questions and engagement levels

Common Student Questions

Q: “Isn’t this just making things more complicated?” A: Start simple and grow into complexity. The initial learning curve pays dividends in consistency and deployment simplicity.

Q: “Do I need to containerize everything?” A: No, start with applications that benefit most (microservices, development environments, CI/CD pipelines).

Q: “How is this different from just using virtual environments?” A: Virtual environments only isolate language-specific dependencies, while containers isolate the entire runtime environment including system libraries and configuration.