Chapter 1: Getting Started with Docker
9/1/25About 5 min
Chapter 1: Getting Started with Docker
Learning Objectives
- Understand the concept and advantages of containerization technology
- Master Docker's core architecture and basic components
- Complete the installation and configuration of the Docker environment
- Learn to use basic Docker commands
Knowledge Points
What is Containerization Technology
Containerization is a lightweight virtualization technology that packages an application and its dependencies into a portable container, ensuring that the application runs consistently in any environment.
Core Features of Containerization Technology:
- Lightweight: Compared to traditional virtual machines, containers share the host operating system kernel, consuming fewer resources.
- Portability: Build once, run anywhere, solving the "it works on my machine" problem.
- Isolation: Containers are isolated from each other, providing a secure running environment.
- Fast Startup: Starts in seconds, much faster than traditional virtual machines.
Docker Architecture Overview
Docker uses a client-server (C/S) architecture model, mainly composed of the following components:
Component | Role | Description |
---|---|---|
Docker Client | Client | The primary way users interact with Docker, sending commands to the Docker Daemon |
Docker Daemon | Daemon | The core service of Docker, handling Docker API requests and managing images, containers, etc. |
Docker Images | Image | A read-only template used to create Docker containers |
Docker Container | Container | A running instance of an image, which can be started, stopped, moved, or deleted |
Docker Registry | Image Registry | A place to store Docker images, such as Docker Hub |
Containers vs. Virtual Machines
Traditional Virtual Machine Architecture:
┌─────────────────────────────────────────┐
│ App A │ App B │
│ ┌─────────────┐│┌─────────────┐ │
│ │ Guest OS │││ Guest OS │ │
│ └─────────────┘││└─────────────┘ │
│ │ ││ │ │
├─────────────────────────────────────────┤
│ Hypervisor │
├─────────────────────────────────────────┤
│ Host OS │
├─────────────────────────────────────────┤
│ Hardware │
└─────────────────────────────────────────┘
Docker Container Architecture:
┌─────────────────────────────────────────┐
│ App A │ App B │
│ ┌──────────────┼──────────────┐ │
│ │ Container A │ Container B │ │
│ └──────────────┼──────────────┘ │
├─────────────────────────────────────────┤
│ Docker Engine │
├─────────────────────────────────────────┤
│ Host OS │
├─────────────────────────────────────────┤
│ Hardware │
└─────────────────────────────────────────┘
Core Differences
- Resource Overhead: Containers run directly on the host kernel, requiring no extra operating system.
- Startup Time: Container startup time is usually in seconds, while virtual machines take minutes.
- Density: The same hardware resources can run more containers.
- Isolation Level: Virtual machines provide stronger isolation, while container isolation is relatively weaker but sufficiently secure.
Docker Installation and Configuration
Linux System Installation
# Install Docker on Ubuntu/Debian
# Update package index
sudo apt-get update
# Install necessary packages
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add Docker's official repository
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
# Verify successful installation
sudo docker run hello-world
CentOS/RHEL System Installation
# Install necessary tools
sudo yum install -y yum-utils
# Add Docker repository
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# Install Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io
# Start Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Verify installation
sudo docker run hello-world
Windows and macOS Installation
# For Windows users:
# Download Docker Desktop for Windows
# URL: https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe
# For macOS users:
# Download Docker Desktop for Mac
# URL: https://desktop.docker.com/mac/main/amd64/Docker.dmg
# After installation, verify
docker --version
docker-compose --version
Configure Docker User Permissions
# Add the current user to the docker group (to avoid using sudo every time)
sudo usermod -aG docker $USER
# Re-login or use the following command to take effect
newgrp docker
# Test if you can run without sudo
docker run hello-world
Basic Command Operations
Image-related Commands
# View Docker version information
docker --version
docker version # Detailed version information
docker info # System information
# Search for images
docker search nginx
docker search --filter stars=100 nginx # Filter images with 100+ stars
# Pull an image
docker pull nginx # Pull the latest version of nginx
docker pull nginx:1.20 # Pull a specific version
docker pull ubuntu:20.04 # Pull Ubuntu 20.04
# View local images
docker images # List all images
docker images nginx # List nginx-related images
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" # Formatted output
# Remove an image
docker rmi nginx:latest # Remove a specific image
docker rmi $(docker images -q) # Remove all images
docker image prune # Clean up unused images
Container-related Commands
# Run a container
docker run hello-world # Run and exit
docker run -it ubuntu:20.04 /bin/bash # Run Ubuntu interactively
docker run -d nginx # Run nginx in the background
docker run -d -p 8080:80 nginx # Run nginx with port mapping
docker run -d --name my-nginx nginx # Specify a container name
# View containers
docker ps # View running containers
docker ps -a # View all containers (including stopped ones)
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # Formatted output
# Container operations
docker start my-nginx # Start a container
docker stop my-nginx # Stop a container
docker restart my-nginx # Restart a container
docker pause my-nginx # Pause a container
docker unpause my-nginx # Unpause a container
# Enter a container
docker exec -it my-nginx /bin/bash # Enter a running container
docker attach my-nginx # Attach to a container (not recommended)
# View container logs
docker logs my-nginx # View container logs
docker logs -f my-nginx # Follow logs in real-time
docker logs --tail 100 my-nginx # View the last 100 lines of logs
# Remove a container
docker rm my-nginx # Remove a stopped container
docker rm -f my-nginx # Force remove a running container
docker rm $(docker ps -aq) # Remove all containers
docker container prune # Clean up stopped containers
Useful Command Combinations
# One-click cleanup of Docker environment
docker system prune -a # Clean up all unused images, containers, networks
docker system df # View Docker disk usage
# Batch operations
docker stop $(docker ps -q) # Stop all running containers
docker rm $(docker ps -aq) # Remove all containers
docker rmi $(docker images -q) # Remove all images
# Container resource monitoring
docker stats # Monitor container resource usage in real-time
docker stats my-nginx # View resource usage of a specific container
# Detailed container information
docker inspect my-nginx # View detailed container configuration
docker port my-nginx # View container port mappings
docker top my-nginx # View container processes
Practical Exercises
Hello World Example
# Run your first Docker container
docker run hello-world
# Explanation of the output:
# 1. The Docker client contacts the Docker daemon
# 2. The daemon pulls the hello-world image from Docker Hub
# 3. A new container is created and the program in the image is run
# 4. The container stops after the program outputs its message
Web Server Example
# Run an Nginx web server
docker run -d --name web-server -p 8080:80 nginx
# Verify the service is running
curl http://localhost:8080
# Or visit http://localhost:8080 in your browser
# View container status
docker ps
# View container logs
docker logs web-server
# Enter the container
docker exec -it web-server /bin/bash
# Execute commands inside the container
ls /usr/share/nginx/html/ # View web files
cat /etc/nginx/nginx.conf # View nginx configuration
# Exit the container
exit
# Stop and remove the container
docker stop web-server
docker rm web-server
Data Persistence Example
# Create a volume and run a container
docker run -d --name nginx-with-volume \
-p 8080:80 \
-v /tmp/nginx-data:/usr/share/nginx/html \
nginx
# Create a custom web page on the host
echo "<h1>Hello from Docker!</h1>" > /tmp/nginx-data/index.html
# Visit the web page to see the effect
curl http://localhost:8080
# The data remains on the host even after stopping the container
docker stop nginx-with-volume
ls /tmp/nginx-data/ # The file still exists
Common Problem Solving
Permission Issues
# Problem: Got permission denied while trying to connect to the Docker daemon socket
# Solution: Add the user to the docker group
sudo usermod -aG docker $USER
newgrp docker
# Or temporarily use sudo
sudo docker run hello-world
Network Connection Issues
# Problem: Unable to pull images or connection timed out
# Solution: Configure a domestic mirror source
# Create or edit the daemon configuration file
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://registry.docker.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
]
}
EOF
# Restart the Docker service
sudo systemctl daemon-reload
sudo systemctl restart docker
# Verify the configuration
docker info | grep "Registry Mirrors" -A 3
Storage Space Issues
# Problem: Insufficient disk space
# Solution: Clean up Docker resources
# View disk usage
docker system df
# Clean up unused resources
docker system prune -a # Clean up all unused images, containers, networks, build cache
# Clean up separately
docker container prune # Clean up stopped containers
docker image prune # Clean up unused images
docker network prune # Clean up unused networks
docker volume prune # Clean up unused volumes
::: warning Important Notes
- Docker requires a 64-bit operating system.
- In a production environment, it is recommended to use specific version tags instead of `latest`.
- Regularly clean up unused Docker resources to free up disk space.
- Containers run as the root user by default, so be mindful of security.
:::
## Summary
By completing this chapter, you should have mastered:
- **Containerization Concepts**: Understood the advantages and use cases of containerization technology.
- **Docker Architecture**: Learned about Docker's core components and working principles.
- **Environment Setup**: Completed the installation and basic configuration of Docker.
- **Basic Operations**: Mastered the basic management commands for images and containers.
- **Practical Experience**: Deepened your understanding of Docker through practical examples.
In the next chapter, we will delve into Docker image management, including the layered structure of images, the build process, and registry management.