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


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.

BASH
# 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.

100%
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
💡 Tip: There is also a version of Docker Desktop available for Linux, but most Linux developers prefer to use Docker Engine directly—it’s lighter and doesn’t have an additional virtualization layer.


4. Installation Instructions for Each Platform

(1) Windows Installation

Prerequisites:

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)

BASH
# 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: ⭐)

BASH
# 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
⚠️ Note: Being a member of the 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.

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: ⭐⭐)

BASH
# 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"
💻 Output:

TEXT
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: ⭐⭐)

BASH
# List all Docker contexts
docker context ls
💻 Output:

TEXT
NAME                TYPE                DESCRIPTION
default *           moby                Current DOCKER_HOST based configuration
remote-server       moby                Remote Docker on 10.0.1.100
BASH
# 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: ⭐)

BASH
# 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

BASH
# ============================================
# 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
💻 Output (excerpt):

TEXT
# 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

Q Is Docker Desktop free?
A It’s free for personal use and small businesses (<250 employees, <10 million USD in annual revenue). Large enterprises need to purchase a Docker Business subscription (starting at $24 per user per month). Docker Engine (Linux version) is always free and open source.
Q Why is Engine recommended over Desktop on Linux?
A Docker Engine runs directly on the host kernel without an additional virtualization layer, resulting in lower resource consumption and better performance. Desktop requires running a virtual machine on Linux to provide a GUI, which adds an extra layer of overhead. Since server environments do not require a GUI, Engine is the most suitable choice.
Q Are mirror accelerators safe?
A Mainstream accelerators (Alibaba Cloud, USTC, NetEase) are all trusted CDN proxies that only cache official images without modifying their content. However, these accelerators only speed up pull operations; push operations still go through the original Docker Hub path. Internally, we recommend setting up a private registry.
Q How do I verify that Docker has been installed successfully?
A Three-step verification: ① 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.
Q How do I choose between WSL2 and Hyper-V?
A Choose WSL2 first—it starts up faster, uses memory more dynamically, and integrates better with Windows. Use Hyper-V only when WSL2 is unavailable (such as on older versions of Windows 10). Docker Desktop recommends WSL2 mode by default.
Q How much disk space does Docker take up after installation?
A Docker Engine takes up about 300 MB, and Docker Desktop (including the virtual machine) takes up about 2–4 GB. As you pull images and create containers, the space used will continue to grow. You can use docker system df to check the space used and docker system prune to clean it up.

📖 Summary


📝 Exercises

  1. Basic Problem (Difficulty: ⭐): Install Docker on your local machine, run docker run hello-world, and take a screenshot of the output to save it.
  2. 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.
  3. Challenge (Difficulty: ⭐⭐⭐): Run docker run -d -p 8080:80 nginx to start Nginx, use a browser to visit http://localhost:8080 to verify that the page displays correctly, and then use docker stop and docker rm to clean up the containers.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏