How to Install Docker on Ubuntu 22.04
Let's walk through the steps to install Docker on Ubuntu 22.04. This process is straightforward, but there are a few things to keep in mind before we get started.
First, make sure your Ubuntu system is up to date. You can do this by opening a terminal and running:
sudo apt update
sudo apt upgrade -y
Now, let's dive into the installation process.
Step 1: Uninstall Old Versions
Before installing Docker, it's a good idea to remove any old or conflicting Docker packages. This helps avoid potential issues during the installation. Run the following command to remove any existing Docker packages:
sudo apt-get remove docker docker-engine docker.io containerd runc
This command might tell you that some of these packages aren't installed, which is fine. It just means you're starting with a clean slate.
Step 2: Set Up the Docker Repository
To install Docker, we'll use the official Docker repository. This ensures you get the latest stable version of Docker. Here's how to set it up:
- Install the necessary packages to allow
apt
to use a repository over HTTPS:
sudo apt-get install ca-certificates curl gnupg lsb-release
- Add the Docker GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Add the Docker repository to your
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 3: Install Docker Engine
Now that the repository is set up, you can install Docker. First, update your package index:
sudo apt-get update
Then, install the latest version of Docker Engine, containerd, and Docker Compose:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Step 4: Verify the Installation
To make sure Docker is installed correctly, run the following command:
sudo docker run hello-world
This command downloads a test image and runs it in a container. If everything is set up correctly, you should see a message confirming that Docker is working.
Step 5: Manage Docker as a Non-Root User (Optional)
By default, Docker requires sudo
to run commands. If you want to run Docker commands without 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.
Additional Notes
- Firewall Considerations: If you're using a firewall like
ufw
orfirewalld
, be aware that Docker can bypass these rules when exposing container ports. Make sure to configure your firewall accordingly. - Docker and iptables: Docker is only compatible with
iptables-nft
andiptables-legacy
. If you're usingnft
for firewall rules, you'll need to switch toiptables
. - Upgrading Docker: To upgrade Docker, simply run
sudo apt-get update
andsudo apt-get upgrade
to get the latest version from the repository.
That's it! You've now installed Docker on Ubuntu 22.04. If you run into any issues or need further configuration, refer to the Docker documentation for more detailed instructions.