2 Hours

2. Installing Docker on Your Machine

Setup

Getting Docker Running on Your Operating System

Docker runs natively on Linux, but requires a lightweight virtual machine on macOS and Windows (though WSL 2 has made Windows experience almost native). This module walks through installation on all three platforms and verifies everything works correctly.

Installing on macOS (Intel and Apple Silicon)

Docker Desktop for Mac is the recommended installation method. It includes Docker Engine, Docker CLI, Docker Compose, and a graphical dashboard for managing containers. For Apple Silicon (M1, M2, M3) Macs, Docker Desktop runs natively without emulation, which means containers run at full speed. The installation process is straightforward: download the DMG from Docker's website, drag the Docker.app icon to your Applications folder, and launch Docker. The first launch requires you to accept the license agreement and provide your system password to install networking components. You will see a whale icon in your menu bar when Docker is running — click it to access preferences and troubleshoot. Docker Desktop for Mac automatically starts when you log in, so you only need to install it once.

Bash
# After installing Docker Desktop on Mac, verify it works:
docker --version
# Expected output: Docker version 24.0.7, build afdd53b

docker compose version
# Expected output: Docker Compose version v2.23.0

docker run hello-world
# Should download the test image and print a success message

# If you get permission errors, you may need to reset Docker Desktop
# Go to Troubleshoot → Reset to factory defaults

Installing on Windows with WSL 2

Windows users should install Docker Desktop with WSL 2 (Windows Subsystem for Linux) backend. This is the only officially supported configuration and provides near-native performance. Before installing Docker, you must enable WSL 2 on your Windows machine. Open PowerShell as Administrator and run `wsl --install` — this installs WSL 2 with Ubuntu as the default distribution. After restarting, go to the Windows Features control panel and ensure Virtual Machine Platform and Windows Subsystem for Linux are both enabled. Then download Docker Desktop for Windows from Docker's website. During installation, ensure Use WSL 2 instead of Hyper-V' is selected. After installation completes, launch Docker Desktop. Go to Settings → Resources → WSL Integration and enable integration with your WSL distro. This allows you to run Docker commands directly from your Linux terminal inside Windows.

Text
# Run these commands in PowerShell as Administrator:
# Enable WSL 2
wsl --install

# After restart, verify WSL version
wsl --status
# Should show: Default Version: 2

# After Docker Desktop installation, test in WSL terminal
docker run hello-world

# If Docker commands don't work in WSL, check WSL integration
# Docker Desktop → Settings → Resources → WSL Integration → Enable for your distro

Installing Docker Engine on Linux (Ubuntu/Debian)

Linux users can install Docker Engine directly without Docker Desktop. Docker Engine is the core daemon and CLI, and it runs natively because Linux already has all the kernel features containers need. The installation process involves adding Docker's official repository to your package manager, then installing the docker-ce package. Ubuntu and Debian-based distributions use apt. After installation, Docker commands require sudo by default because the Docker daemon runs as root. To run Docker without sudo, add your user to the docker group. This is convenient for development but be aware that membership in the docker group is effectively equivalent to root access — anyone in the group can run any container, and containers can mount any directory. On production servers, always use sudo or restrict docker group membership carefully.

Bash
# Update package index and install prerequisites
sudo apt update
sudo apt install -y ca-certificates curl

# Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Add your user to docker group (to run without sudo)
sudo usermod -aG docker $USER
# Log out and back in, or run: newgrp docker

# Verify installation
docker run hello-world

Post-Installation Verification and Configuration

After installation, you should verify that Docker is working correctly and optionally configure the daemon for your needs. The `docker info` command shows extensive details about your Docker installation: number of containers (running and stopped), number of images, storage driver (overlay2 is the modern default), logging driver, and cgroup version. This is the first command to run when troubleshooting Docker issues. The Docker daemon can be configured via `/etc/docker/daemon.json` on Linux or the Docker Desktop settings GUI on macOS/Windows. Common configurations include setting the default logging driver to avoid filling your disk with logs, configuring a private registry mirror, setting the default address pools for custom networks, and enabling experimental features. After changing daemon.json, restart the Docker daemon with `sudo systemctl restart docker` on Linux, or restart Docker Desktop on other platforms.

Bash
# Check Docker version and system info
docker version
docker info

# Run a test container to verify everything works
docker run --rm hello-world
# --rm automatically removes the container after it exits

# Optional: Configure Docker daemon (Linux)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "default-address-pools": [
    {
      "base": "172.17.0.0/16",
      "size": 24
    }
  ]
}
EOF
sudo systemctl restart docker
Security Note: The docker group grants root-equivalent privileges. On production servers, require sudo for Docker commands instead of adding users to the docker group. For development workstations, adding your user to the docker group is acceptable but be aware of the security implications — any container you run can access any file on your system through bind mounts.

Knowledge Check

Ready to test your understanding of 2. Installing Docker on Your Machine?

Back