Introduction to Docker¶
🐳 Learning Objectives
- Understand Docker and containerization concepts
- Install Docker on your system
- Learn Docker architecture and components
- Run your first container
Welcome to the Docker course! Docker has revolutionized how we develop, ship, and run applications. In this course, you'll learn everything you need to master containerization.
What is Docker?¶
Docker is a platform that uses containerization to package applications and their dependencies into lightweight, portable containers. These containers can run consistently across different environments.
Why Docker Matters
Docker solves the "it works on my machine" problem by packaging applications with all their dependencies. Containers are isolated, portable, and consistent across development, staging, and production environments.
Getting Started
If you're new to containers, think of Docker as a lightweight alternative to virtual machines. Containers share the host OS kernel, making them much faster and more efficient than VMs.
Why Docker?¶
Benefits
- Consistency: Works the same on your machine, staging, and production
- Isolation: Applications don't interfere with each other
- Portability: Run anywhere Docker is installed
- Efficiency: Lightweight compared to virtual machines
- Scalability: Easy to scale applications horizontally
Key Concepts¶
Containers vs Virtual Machines¶
graph TB
A[Host OS] --> B[Hypervisor]
B --> C[VM 1]
B --> D[VM 2]
C --> E[Guest OS 1]
D --> F[Guest OS 2]
E --> G[App 1]
F --> H[App 2]
I[Host OS] --> J[Docker Engine]
J --> K[Container 1]
J --> L[Container 2]
K --> M[App 1]
L --> N[App 2]
Docker Components¶
- Docker Image: A read-only template for creating containers
- Docker Container: A running instance of an image
- Dockerfile: A text file with instructions to build an image
- Docker Hub: A registry for Docker images
Installation¶
macOS¶
Option 1: Using Homebrew (Recommended)
Homebrew is a package manager for macOS. If you don't have it, install it first from brew.sh.
What this does: This command downloads and installs Docker Desktop for Mac, which includes the Docker engine, CLI, and a GUI application.
Option 2: Direct Download
- Visit docker.com/products/docker-desktop
- Download Docker Desktop for Mac
- Open the
.dmgfile and drag Docker to Applications - Launch Docker from Applications
After installation: Docker Desktop will start automatically. You'll see a Docker icon in your menu bar.
Linux¶
For Ubuntu/Debian systems:
What these commands do:
- curl -fsSL https://get.docker.com -o get-docker.sh: Downloads the official Docker installation script and saves it as get-docker.sh
- -f: Fail silently on server errors
- -s: Silent mode (no progress bar)
- -S: Show errors even in silent mode
- -L: Follow redirects
- sh get-docker.sh: Executes the installation script, which automatically detects your Linux distribution and installs Docker
Note: You may need to run with sudo or add your user to the docker group:
Verify Installation¶
After installation, verify Docker is working correctly:
Expected output: Docker version 24.0.0, build abc123 (version numbers will vary)
What this does: This command:
1. Downloads the hello-world image from Docker Hub (if not already present)
2. Creates a new container from that image
3. Runs the container, which prints a welcome message
4. The container stops automatically after printing the message
Expected output:
Your First Container¶
Running a Simple Container¶
What happens:
1. Docker checks if the hello-world image exists locally
2. If not found, Docker automatically pulls it from Docker Hub
3. Docker creates a new container from the image
4. The container runs, prints a message, and exits
Understanding the output: You'll see a message confirming Docker is working. The container runs, completes its task, and stops automatically.
Running an Interactive Container¶
Breaking down this command:
- docker run: Creates and starts a new container
- -i: Interactive mode - keeps STDIN open so you can type commands
- -t: Allocates a pseudo-TTY (terminal) - gives you a terminal interface
- ubuntu: The base image to use (a minimal Ubuntu Linux system)
- bash: The command to run inside the container (starts a bash shell)
What happens: 1. Docker downloads the Ubuntu image (first time only, ~70MB) 2. Creates a new container from the image 3. Starts a bash shell inside the container 4. You're now inside the container! You can run Linux commands
Try these commands inside the container:
# Check the OS
cat /etc/os-release
# List files
ls -la
# Check current directory
pwd
# Exit the container
exit
Understanding Flags
-i(interactive): Allows you to interact with the container's STDIN-t(tty): Creates a terminal interface-it: Combined, these flags give you an interactive terminal session-d(detached): Runs container in background (we'll cover this later)
First Time Download
The first time you run docker run ubuntu, Docker will download the image. This may take a few minutes depending on your internet speed. Subsequent runs will be instant as the image is cached locally.
What You'll Learn¶
In this course, we'll cover:
- Containers: Creating and managing containers
- Images: Building custom images
- Dockerfiles: Writing efficient Dockerfiles
- Networking: Connecting containers
- Volumes: Managing persistent data
- Docker Compose: Orchestrating multi-container applications
Next Steps¶
Ready to dive in? Let's start by understanding containers in detail.
Next Lesson: Working with Containers