How to Install Docker on Ubuntu

How to Install Docker on Ubuntu: A Step-by-Step Guide

Docker is a powerful platform for containerizing applications, enabling developers to package applications with their dependencies for consistent deployment across environments. This guide provides a concise, step-by-step process to install Docker on Ubuntu, focusing on the most reliable method: installing Docker Engine using the official Docker repository. This is ideal for both development and production environments on Ubuntu 20.04, 22.04, or 24.04.

Prerequisites

  • Operating System: Ubuntu 20.04, 22.04, or 24.04 (64-bit).
  • User Privileges: A user with sudo privileges.
  • System Requirements:
  • 64-bit kernel and CPU with virtualization support.
  • At least 2GB RAM and 10GB free disk space.
  • Internet connection for downloading packages.
  • Optional: SSH access if setting up on a remote server.

Step-by-Step Installation

Step 1: Update the System

Ensure your system is up-to-date to avoid package conflicts.

sudo apt-get update
sudo apt-get upgrade -y

Step 2: Uninstall Old Docker Versions (If Any)

Remove any older Docker installations to prevent conflicts.

sudo apt-get remove -y docker docker-engine docker.io containerd runc

This won’t delete existing images or containers, as they’re stored separately.

Step 3: Set Up Docker’s APT Repository

Docker’s official repository provides the latest stable version of Docker Engine.

3.1 Install Dependencies

Install required packages for secure repository access.

sudo apt-get install -y ca-certificates curl gnupg lsb-release

3.2 Add Docker’s GPG Key

Add Docker’s official GPG key to verify package authenticity.

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

3.3 Add Docker Repository

Add the Docker repository to your system’s APT sources.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: Install Docker Engine

Update the package index and install Docker Engine, CLI, and related components.

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 5: Verify Installation

Check that Docker is installed and running.

  • Verify Docker version:
docker --version
  • Check Docker service status:
sudo systemctl status docker
  • Ensure Docker starts on boot:
sudo systemctl enable docker
sudo systemctl enable containerd
  • Run a test container:
sudo docker run hello-world

This pulls the hello-world image from Docker Hub and runs it, confirming Docker is working. You should see a success message.

Step 6: Manage Docker as a Non-Root User (Optional)

To run Docker commands without sudo, add your user to the docker group.

sudo usermod -aG docker $USER

Log out and back in (or run newgrp docker) for the change to take effect. Verify by running:

docker run hello-world

If it runs without sudo, the configuration is successful.

Step 7: Post-Installation Configuration (Optional)

  • Adjust Docker Storage: By default, Docker uses /var/lib/docker for storage. Ensure sufficient disk space or configure a different storage driver if needed (e.g., overlay2).
  • Firewall Settings: If using UFW, allow Docker-related ports:
sudo ufw allow 2375/tcp
sudo ufw allow 2376/tcp
  • Test Docker Compose: Verify Docker Compose installation:
docker compose version

Troubleshooting Common Issues

  • Docker Service Not Starting: Check logs with journalctl -u docker and ensure containerd is running (sudo systemctl status containerd).
  • Permission Denied: Ensure your user is in the docker group or use sudo.
  • Repository Errors: Verify the correct Ubuntu codename in /etc/apt/sources.list.d/docker.list (e.g., jammy for 22.04).
  • Networking Issues: Check firewall settings or reset Docker networking with sudo systemctl restart docker.

Alternative Installation Method: Convenience Script

For testing or development environments, Docker provides a convenience script (not recommended for production):

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Follow steps 5 and 6 to verify and configure.

Conclusion

You’ve successfully installed Docker Engine on Ubuntu! You can now start pulling images, building containers, or exploring Docker Compose for multi-container applications. For further learning, check the official Docker documentation or Docker Hub for pre-built images.

If you run into issues, consult the Docker community forums or use docker info --format '{{.ServerErrors}}' for diagnostic information. Happy containerizing!

Note: This guide is based on Ubuntu 22.04/24.04 and Docker’s latest stable release as of August 15, 2025. Always verify the latest instructions on the official Docker website.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *