Skip to content

Docker Installation and Setup

Docker Engine vs. Docker Desktop

The installation process differs based on your operating system.

  • For Linux: You will install Docker Engine, a set of command-line tools and a background daemon (dockerd) that run natively on your host.

  • For Windows and macOS: You will install Docker Desktop. This is a comprehensive desktop application that includes the Docker Engine, the client, Docker Compose, and Kubernetes. Because Linux containers can only run on a Linux kernel, Docker Desktop for Windows and Mac runs these components inside a lightweight, managed Linux virtual machine.

    • On Windows, this is achieved via the Windows Subsystem for Linux (WSL) 2.

    • On macOS, this is achieved via the Hypervisor framework.

This guide covers the two most common development environments: a native Ubuntu (Linux) server and a Windows 11 machine using WSL2.

Installation on Ubuntu 22.04 / 24.04

This process installs Docker Engine from Docker's official apt repository, which ensures you always have the latest version.

Reference: Official Docker Documentation for Ubuntu

1. Remove Old Versions

First, remove any older, conflicting Docker packages that may have been installed from Ubuntu's default repositories or other sources.

bash
sudo apt-get remove -y docker docker-engine docker.io containerd runc docker-doc docker-compose docker-compose-v2 podman-docker

2. Set Up the Repository

Next, configure apt to use Docker's official GPG key and repository.

  1. Update apt and install prerequisite packages
bash
sudo apt-get update

sudo apt-get install -y \
	ca-certificates \
	curl
  1. Add Docker's official GPG key
bash
sudo install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL [https://download.docker.com/linux/ubuntu/gpg](https://download.docker.com/linux/ubuntu/gpg) -o /etc/apt/keyrings/docker.asc

sudo chmod a+r /etc/apt/keyrings/docker.asc
  1. Add the repository to Apt sources: This command dynamically finds your Ubuntu codename (e.g., "jammy" or "noble")
bash
echo \
  "deb [arch=$(dpkg --print-architecture) \
  signed-by=/etc/apt/keyrings/docker.asc] \
  [https://download.docker.com/linux/ubuntu](https://download.docker.com/linux/ubuntu) \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Update apt again to pull from the new Docker repo
bash
sudo apt-get update

3. Install Docker Engine

Now that the repository is set up, you can install the latest version of the Docker Engine, client, and plugins.

bash
# Install the latest stable version of Docker
sudo apt-get install -y docker-ce \
	docker-ce-cli \
	containerd.io \
	docker-buildx-plugin \
	docker-compose-plugin

4. Uninstall Docker Engine

If you need to remove Docker:

  1. Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:
bash
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
  1. Images, containers, volumes, or custom configuration files on your host aren't automatically removed. To delete all images, containers, and volumes:
bash
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
  1. Remove the Docker apt source and GPG key:
bash
sudo rm /etc/apt/sources.list.d/docker.list
sudo rm /etc/apt/keyrings/docker.asc

Installation on Windows 11

The recommended (and required) method for running Docker on Windows is by using Docker Desktop with the WSL 2 (Windows Subsystem for Linux) backend.

Reference : Official Docker Documentation for Windows

https://www.docker.com/products/docker-desktop/

1. Enable WSL 2 and Virtualization

Before installing Docker Desktop, you must enable the required Windows features.

  1. Open PowerShell or Command Prompt as Administrator.

  2. Enable the Windows Subsystem for Linux:

    powershell
    wsl --install

    This single command will enable the necessary features, download the latest Linux kernel, and set WSL 2 as the default. It will also install the default Ubuntu distribution.

    Note: If you have an older version of Windows, you may need to run these commands manually:

    powershell
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  3. Restart your computer as prompted to complete the setup.

2. (Optional) Install a Linux Distribution

If the wsl --install command did not install a distribution, or if you prefer a different one, you can get one from the Microsoft Store. Open the store and search for "Ubuntu" or "Debian" and click "Install".

3. Install Docker Desktop

  1. Go to the official Docker website and download the Docker Desktop for Windows installer: https://www.docker.com/products/docker-desktop/

  2. Run the downloaded .exe installer.

  3. Follow the on-screen prompts. Ensure that the "Use WSL 2 instead of Hyper-V (recommended)" option is CHECKED.

  4. Once the installation is complete, Docker Desktop will start. The whale icon in the system tray indicates its status. It may take a minute to start the Docker Engine.

Docker Desktop will automatically detect your WSL 2 distributions and integrate with them.

Post-Installation Steps

For Linux (Ubuntu)

By default, the Docker daemon socket is owned by root, which means you must use sudo for every Docker command. To avoid this, add your user to the docker group (which is created by the installer).

bash
# 1. Add your user to the 'docker' group
sudo usermod -aG docker $USER
bash
# 2. IMPORTANT: You must log out and log back in
# for this group change to take effect.
#
# Alternatively, run this command to activate the
# changes for your current terminal session:
newgrp docker

Security Warning: Adding a user to the docker group is equivalent to giving them root access to the host.

2. Configure Docker to Start on Boot

The Docker Engine service is automatically configured to start on boot. You can verify and manage this with systemd.

bash
# Check if the services are running and enabled
sudo systemctl status docker
sudo systemctl status containerd

# (If needed) Enable the services to start at boot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

# (If needed) Start the services manually
sudo systemctl start docker.service
sudo systemctl start containerd.service

3. Configure Container Logging

Docker provides logging drivers for collecting and viewing log data. The default driver, json-file, writes log data to files on the host filesystem. Over time, these log files can expand and exhaust disk resources. It is a best practice to configure log rotation in the Docker daemon's configuration file (/etc/docker/daemon.json).

For Windows (Docker Desktop)

Docker Desktop will automatically start when you log in to Windows. You can configure this behavior in Settings > General. It automatically manages the lifecycle of the Docker Engine within its WSL 2 virtual machine.

Verify the Installation

You can test that your installation is working correctly by running the hello-world container.

bash
# This command downloads a minimal test image and runs it.
# It will print a confirmation message if successful.

docker run hello-world

(Note: Omit 'sudo' if you added your user to the 'docker' group)

After you see the "Hello from Docker!" message, you can test with a more interactive container, like a shell in a lightweight Alpine Linux image:

bash
# This will pull the 'alpine' image, start a container,
# and drop you into an interactive shell ('/bin/sh').
# --rm = automatically remove the container when you exit
# -it = interactive TTY (a terminal)

docker container run --rm -it alpine /bin/sh

If you see a command prompt (e.g., / #), your installation is fully functional. Type exit to leave the container.

bash
docker container run --rm -ti docker.io/fedora:latest /bin/bash

docker container run --rm -ti docker.io/fedora:latest /bin/bash

docker.io/ubuntu:latest, docker.io/fedora:latest, and docker.io/alpine:latest all represent a Docker image repository, followed by an image name and an image tag.

References