Installing Docker and Configuring the Environment
Installing Docker is easier than you might think—whether you're using Windows, Mac, or Linux, you can have a fully functional container environment up and running in 10 minutes.
1. What You'll Learn
- How to Install Docker on Various Platforms
- The Difference Between Docker Desktop and Docker Engine
- Mirror Accelerator Configuration
- Running Docker as a non-root user
- Docker Context Management
2. A True Story of a Team’s Technical Lead
(1) Challenge: The 15 individuals have different circumstances
Charlie's team had just decided to fully adopt Docker, but Lisa, a new team member, asked, "Should I install Docker Desktop or Docker Engine? Is the installation process the same on Mac and Linux?" The 15-person team used four different operating systems (Windows, Mac, Ubuntu, and CentOS), and the installation process varied for each one; just setting up the environment took two days.
(2) Implementing the Decision Tree Algorithm
Charlie drew an installation decision tree—3 OS types × 2 modes—and got the team up and running in 15 minutes.
# Verify Docker installation after setup
docker run hello-world
(3) Benefits: A unified, frictionless environment
After the team completed the unified installation, everyone’s docker version output became consistent, and the number of environment-related issues dropped from 5 per week to 0.
3. Docker Desktop vs Docker Engine
Before installing Docker, make sure you understand the differences between the two distributions.
graph TB
START["Select Docker Version"] --> OS{"Your operating system?"}
OS -->|Windows / Mac| DT["Docker Desktop<br/>with GUI + VM + CLI"]
OS -->|Linux Server| DE["Docker Engine<br/>Pure CLI Services"]
DT --> DEV["✅ Recommended Development Environments"]
DE --> PROD["✅ Recommended Production Environments"]
(1) Comparison of the Two Distributions
| Dimension | Docker Desktop | Docker Engine |
|---|---|---|
| Included Components | CLI + Daemon + GUI + VM | CLI + Daemon (pure background service) |
| Supported Platforms | Windows / Mac / Linux | Linux only |
| Resource Usage | High (2–4 GB, including virtual machines) | Low (runs directly in the host kernel) |
| GUI Management | ✅ Has a dashboard | ❌ Command-line only |
| Use Cases | Developer's personal computer | Server/CI environment |
| Price | Free for individuals, paid for businesses | Completely free (CE version) |
(2) Installation Decision-Making Process
| Requirement | Recommended Version |
|---|---|
| Windows / Mac Development | Docker Desktop |
| Linux Desktop Development | Docker Desktop (requires a GUI) or Engine |
| Linux Server Production | Docker Engine |
| CI/CD Build Nodes | Docker Engine |
4. Installation Instructions for Each Platform
(1) Windows Installation
Prerequisites:
- Windows 10 64-bit (version 1903 or later) or Windows 11
- Enable WSL2 (Windows Subsystem for Linux 2)
- Enable Hyper-V (optional; WSL2 mode is recommended)
Installation Steps:
| Step | Action |
|---|---|
| 1 | Download Docker Desktop for Windows (docker.com/products/docker-desktop) |
| 2 | Double-click the installer and make sure "Use WSL 2 instead of Hyper-V" is checked |
| 3 | Restart your computer after installation |
| 4 | Start Docker Desktop and wait for the status to change to "Running" |
| 5 | Open the terminal to verify: docker version |
(2) Mac Installation
| Step | Action |
|---|---|
| 1 | Download Docker Desktop for Mac (select "amd64" for Intel chips, and "arm64" for Apple Silicon) |
| 2 | Drag Docker.app to the Applications folder |
| 3 | Launch Docker.app and wait for the whale icon in the menu bar to become steady |
| 4 | Terminal Verification: docker version |
(3) Linux Installation (Ubuntu/Debian)
# 1. Update package index
sudo apt-get update
# 2. Install prerequisites
sudo apt-get install -y ca-certificates curl gnupg
# 3. Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# 4. Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 5. Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 6. Start Docker
sudo systemctl start docker
sudo systemctl enable docker
# 7. Verify
sudo docker run hello-world
5. Configuration for Non-Root Users
By default, only the root user and members of the docker group can run Docker on Linux.
▶ Example: Add the current user to the docker group (Difficulty: ⭐)
# Add current user to docker group
sudo usermod -aG docker $USER
# Apply group change (or log out and back in)
newgrp docker
# Verify: run docker without sudo
docker run hello-world
docker group is equivalent to having root privileges (because containers can mount any directory on the host). In a production environment, membership in the docker group should be strictly controlled.
6. Mirror Accelerator Configuration
Accessing Docker Hub from within China can be slow, but configuring an image accelerator can significantly improve pull speeds.
(1) Popular Mirror Accelerators
| Accelerator | Address | Description |
|---|---|---|
| Alibaba Cloud | https://<your-id>.mirror.aliyuncs.com |
Log in to get your unique link |
| USTC | https://docker.mirrors.ustc.edu.cn |
Free, no registration required |
| NetEase | https://hub-mirror.c.163.com |
Free |
| Official Docker | https://registry.docker.io |
Default, fast speeds from overseas |
▶ Example: Configuring a Mirror Accelerator on Linux (Difficulty: ⭐⭐)
# Create or edit Docker daemon configuration
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
]
}
EOF
# Restart Docker to apply changes
sudo systemctl daemon-reload
sudo systemctl restart docker
# Verify the configuration
docker info | grep -A 5 "Registry Mirrors"
Registry Mirrors:
https://docker.mirrors.ustc.edu.cn/
https://hub-mirror.c.163.com/
▶ Example: Configuring the Docker Desktop Image Accelerator (Difficulty: ⭐)
To configure in the Docker Desktop GUI: Settings → Docker Engine → Add the registry-mirrors array to the JSON → Apply & Restart.
Or edit the configuration file directly (Mac: ~/.docker/daemon.json, Windows: C:\Users\<user>\.docker\daemon.json).
7. Docker Context Management
Docker contexts allow you to quickly switch between multiple Docker hosts—such as your local Docker instance and a remote server.
▶ Example: Viewing and Switching Docker Contexts (Difficulty: ⭐⭐)
# List all Docker contexts
docker context ls
NAME TYPE DESCRIPTION
default * moby Current DOCKER_HOST based configuration
remote-server moby Remote Docker on 10.0.1.100
# Create a new context for a remote Docker host
docker context create remote-server --docker "host=ssh://user@10.0.1.100"
# Switch to the remote context
docker context use remote-server
# Verify: now all docker commands run on the remote server
docker ps
# Switch back to local
docker context use default
▶ Example: Verifying Successful Installation (Difficulty: ⭐)
# Full verification checklist
docker version # Client + Server versions
docker info # System-wide info
docker run hello-world # End-to-end test
8. Troubleshooting Common Installation Issues
| Problem | Cause | Solution |
|---|---|---|
Cannot connect to the Docker daemon |
Docker service not running | sudo systemctl start docker |
permission denied while trying to connect |
The user is not in the docker group | sudo usermod -aG docker $USER |
WSL2 installation is incomplete (Windows) |
WSL2 not installed correctly | wsl --install Restart afterward |
docker: command not found |
Docker is not included in the PATH | Reinstall or manually add it to the PATH |
| Mirror Retrieval Timeout | Network Issues | Configure Mirror Accelerator |
9. Complete Example: Setting Up a Docker Environment on an Ubuntu Server from Scratch
# ============================================
# Complete walkthrough: Install Docker on Ubuntu
# Covers: install, sudo-free, mirror, verify
# ============================================
# 1. Install Docker Engine
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 2. Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
# 3. Add current user to docker group (no more sudo)
sudo usermod -aG docker $USER
newgrp docker
# 4. Configure registry mirror for faster pulls
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 5. Verify: run Nginx and access it
docker run -d -p 8080:80 --name test-nginx nginx:latest
curl http://localhost:8080
# 6. Clean up
docker stop test-nginx
docker rm test-nginx
# docker run -d -p 8080:80 --name test-nginx nginx:latest
a1b2c3d4e5f6...
# curl http://localhost:8080
<!DOCTYPE html>
<html><head><title>Welcome to nginx!</title>...
❓ FAQ
pull operations; push operations still go through the original Docker Hub path. Internally, we recommend setting up a private registry.docker version You should see information for both the Client and Server sections; ② docker info Output should display normally; ③ docker run hello-world "Hello from Docker!" should be displayed. If all three steps pass, the installation is complete.docker system df to check the space used and docker system prune to clean it up.📖 Summary
- Docker Desktop is suitable for development on Windows and Mac (with a GUI), while Docker Engine is suitable for Linux servers (CLI only)
- For Windows, use WSL2 mode; for Mac, select the appropriate version based on whether your system uses Intel or Apple Silicon
- Use the official APT/YUM repositories for Linux installation; do not use the outdated versions that come with the system.
- Adding a user to the docker group allows them to run Docker without using sudo
- Configure a mirror accelerator to resolve slow access to Docker Hub within China
- Docker Context allows you to quickly switch between local and remote Docker hosts
📝 Exercises
- Basic Problem (Difficulty: ⭐): Install Docker on your local machine, run
docker run hello-world, and take a screenshot of the output to save it. - Advanced Exercise (Difficulty: ⭐⭐): Set up a domestic mirror accelerator and run
docker info | grep -A 5 "Registry Mirrors"to verify that the configuration is working. - Challenge (Difficulty: ⭐⭐⭐): Run
docker run -d -p 8080:80 nginxto start Nginx, use a browser to visithttp://localhost:8080to verify that the page displays correctly, and then usedocker stopanddocker rmto clean up the containers.



