Overview
Docker is a powerful tool for containerizing applications, and one of its key strengths is its ability to run on multiple platforms, including Linux, macOS, and Windows. In this detailed guide, we’ll cover step-by-step instructions for installing Docker on each of these operating systems. Whether you’re working on a Linux server, a macOS development machine, or a Windows laptop, this guide will help you get Docker up and running.
This post will cover:
- Installing Docker on Linux (Ubuntu, CentOS, and other distributions)
- Installing Docker on macOS
- Installing Docker on Windows
- Post-installation steps for Docker
- Troubleshooting common Docker installation issues
1. Installing Docker on Linux
Linux is the original home of Docker, and it remains one of the best platforms for running Docker containers due to its lightweight nature and ease of integration with the Docker engine. Below are the steps for installing Docker on popular Linux distributions, such as Ubuntu and CentOS.
1.1. Installing Docker on Ubuntu
Step 1: Update Your System
Before installing Docker, ensure your package manager is up to date by running the following commands:
sudo apt update
sudo apt upgrade
Step 2: Install Required Packages
You need to install a few prerequisite packages that allow apt to use repositories over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Step 3: Add Docker’s Official GPG Key
Run the following command to add Docker’s official GPG key to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Add Docker’s Repository
Add Docker's official repository to your system:
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
Step 5: Install Docker Engine
Update the package list and install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Step 6: Start and Enable Docker
Once Docker is installed, start the Docker service and enable it to run at boot:
sudo systemctl start docker
sudo systemctl enable docker
Step 7: Verify the Installation
To verify that Docker is working, run the following command:
sudo docker run hello-world
This will download a test image and run it in a container. If you see a message saying "Hello from Docker!", the installation was successful.
1.2. Installing Docker on CentOS
Step 1: Uninstall Older Versions of Docker
If you have an older version of Docker installed (called docker
or docker-engine
), remove it:
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
Step 2: Set Up the Docker Repository
Install the required packages:
sudo yum install -y yum-utils
Then, add the Docker repository to your system:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Step 3: Install Docker Engine
Now, install Docker Engine and containerd:
sudo yum install docker-ce docker-ce-cli containerd.io
Step 4: Start and Enable Docker
Once Docker is installed, start the Docker service and enable it to start on boot:
sudo systemctl start docker
sudo systemctl enable docker
Step 5: Verify the Installation
Run the following command to check if Docker is installed correctly:
sudo docker run hello-world
1.3. Installing Docker on Other Linux Distributions
For other Linux distributions such as Debian, Fedora, or Arch Linux, the Docker installation process is generally similar. You need to set up the Docker repository, install Docker using the package manager, and then start and enable the Docker service. You can follow distribution-specific instructions in the official Docker documentation.
2. Installing Docker on macOS
Docker Desktop is the official Docker application for macOS, providing a simple and user-friendly way to install Docker on a Mac. Docker Desktop for macOS includes Docker Engine, Docker CLI, and Docker Compose.
Step 1: Download Docker Desktop
Go to the Docker Desktop for Mac download page and download the .dmg
file for macOS.
Step 2: Install Docker Desktop
Once the download is complete, open the .dmg
file and drag the Docker icon to the Applications folder to install it.
Step 3: Launch Docker Desktop
Open the Applications folder and double-click the Docker icon to start Docker Desktop. You will see the Docker whale icon appear in the macOS menu bar, indicating that Docker is running.
Step 4: Verify the Installation
To verify that Docker is installed correctly, open a terminal and run the following command:
docker --version
You should see output indicating the version of Docker that is installed. Additionally, you can run the following test:
docker run hello-world
If Docker is working correctly, you will see the "Hello from Docker!" message.
3. Installing Docker on Windows
Docker Desktop is also available for Windows, making it easy to install Docker on a Windows machine. However, Docker Desktop for Windows requires Windows 10 (version 1903 or higher) or Windows 11 with the WSL 2 (Windows Subsystem for Linux) backend.
Step 1: Install WSL 2 (Optional but Recommended)
If you don’t have WSL 2 installed, you should install it before installing Docker Desktop. Follow these steps:
Set WSL 2 as the default version:
wsl --set-default-version 2
Enable the WSL feature:
wsl --install
Step 2: Download Docker Desktop for Windows
Go to the Docker Desktop for Windows download page and download the installer.
Step 3: Install Docker Desktop
Run the downloaded installer and follow the installation prompts. Make sure to select the option to install Docker with WSL 2 support.
Step 4: Launch Docker Desktop
Once the installation is complete, open Docker Desktop by clicking the Docker icon in the Start menu. Docker will launch and start running in the background.
Step 5: Verify the Installation
Open a PowerShell window or Command Prompt and run the following command to verify that Docker is installed:
docker --version
You can also run the hello-world
container to test Docker:
docker run hello-world
If you see the "Hello from Docker!" message, Docker is installed and working correctly.
4. Post-Installation Steps for Docker
After installing Docker, there are a few additional configuration steps that you may want to perform:
4.1. Manage Docker as a Non-Root User (Linux)
By default, Docker requires sudo
privileges to run commands. If you want to run Docker without needing sudo
, you can add your user to the docker
group:
sudo usermod -aG docker $USER
After running this command, log out and log back in for the changes to take effect.
4.2. Configure Docker to Start on Boot (Linux)
You can enable Docker to start automatically when the system boots by running the following command:
sudo systemctl enable docker
4.3. Install Docker Compose (Optional)
If you want to use Docker Compose, which allows you to define and run multi-container applications, you can install it by following the instructions in the official Docker Compose documentation.
5. Troubleshooting Common Docker Installation Issues
5.1. Docker Daemon Not Starting
If the Docker daemon fails to start, check the logs to diagnose the issue:
sudo journalctl -xeu docker
Common issues include missing dependencies or conflicting services.
5.2. WSL 2 Installation Problems (Windows)
If Docker fails to start on Windows due to WSL 2 issues, make sure that the correct WSL version is installed and that WSL 2 is enabled. You can check the WSL version with:
wsl -l -v
If WSL 2 is not the default, you can set it by running:
wsl --set-default-version 2
Conclusion
Installing Docker on Linux, macOS, and Windows is a straightforward process, and once installed, Docker provides a powerful environment for developing, testing, and deploying applications in containers. Whether you're running Docker on a Linux server or using Docker Desktop on macOS or Windows, Docker offers a consistent experience across all platforms.