How to Install Docker on Ubuntu [3 Methods Explained & Best Practice]
Introduction: What is Docker?
Docker is an open-source platform that you can easily install on Ubuntu or other supported OS platforms, to package applications and their dependencies into lightweight, portable containers. These containers run consistently across different environments — your laptop, a cloud server, or a data center — eliminating the classic “it works on my machine” problem.
Think of Docker containers as mini-computers that share the same operating system but remain completely isolated. This makes them faster, more reliable, and more efficient compared to traditional virtual machines.
Why Use Docker Instead of Traditional Deployment?
The Traditional Way (Before Docker)
- Applications were installed directly on a server (bare metal or VMs).
- Dependency conflicts were common: one app needed Python 3.8, another needed Python 3.11.
- Moving apps from one environment to another (like dev → test → production) often led to “but it worked on my system!” issues.
- Scaling meant setting up new servers from scratch with all dependenciew, configuring everything manually.
The Docker Way (Modern Approach)
- Docker wraps your app and its dependencies into a container image.
- This image runs anywhere, regardless of the underlying OS.
- Developers build once, run anywhere — whether it’s Linux, Windows, cloud, or on-prem.
- Scaling is as easy as spinning up more containers, like cloning your app instantly.
In short, Docker solves the portability, consistency, and scalability problems that plagued traditional software deployment.
How to Install Docker on Ubuntu
There are three official methods to install Docker on Ubuntu. Each method has pros and cons, and I’ll help you choose the best one. If you prefer installation on a different platform, you can check the procedure documented in dockers official site linked here.
Method 1: Install via apt repository (Recommended)
This is the officially recommended way to install Docker on Ubuntu:
- Packages are signed and verified with Docker’s GPG key (authentic & secure).
- Docker is kept up-to-date via normal
apt upgrade. - Widely used in production environments.
Method 2: Install Manually (Download Packages)
Use this if you are in:
- Airgapped or restricted environments (no internet access).
- No auto-updates — you must fetch and install new versions manually.
- Higher maintenance burden and risk of missing security patches.
Method 3: Install via Docker Convenience Script
Best suited for:
- Quick testing or PoCs (proof of concept).
- Fast and simple, but less transparent (script fetched directly online).
- Not recommended for production since version control and security are harder to manage.
Step-by-Step: Install Docker on Ubuntu (apt Repository Method):
Follow these steps to install Docker on Ubuntu the safe and recommended way. However, if you are interested in using the manual method or the convenience script method, follow the steps from docker's official website linked here.
Step 1: Remove Old Docker Versions
Run the following one-liner to uninstall conflicting packages:
1for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
Step 2: Update System & Install Prerequisites
1sudo apt-get update
2sudo apt-get install ca-certificates curl -y
3sudo install -m 0755 -d /etc/apt/keyrings
4sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
5sudo chmod a+r /etc/apt/keyrings/docker.asc
Step 3: Add Docker’s Official apt Repository
1echo \
2 "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
3 $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
4 sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5sudo apt-get update
Step 4(a): Install a Specific Version of Docker
List available versions:
1apt-cache madison docker-ce | awk '{ print $3 }'
Then install your chosen version:
1VERSION_STRING=5:24.0.9-1~ubuntu.22.04~jammy
2sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin -y
Step 4(b): Install the Latest Version of Docker
1sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
Step 5: Add User to Docker Group
Run Docker without sudo:
1sudo groupadd docker
2sudo usermod -aG docker $USER
Step 6: Enable Docker on Startup
1sudo systemctl enable --now docker
Step 7: Run Your First Docker Container
1docker run hello-world
If you see “Hello from Docker!”, your installation was successful.
Conclusion: Best Way to Install Docker.
The apt repository method is the best way to install Docker because it ensures security, updates, and production readiness.
- Use the manual method only for airgapped systems.
- Use the script method only for testing/learning.
Now that Docker is up and running, you’re ready to start building and deploying containers.