How to Install Docker on Ubuntu 22.04
In this manual, I will explain, step by step, how to install Docker on Ubuntu 22.04.
You can install Docker using the apt repository or install it from a package.
The second option might be necessary for Ubuntu servers that have strict security policies and are not connected to the apt repository or are limited to some specific resources.
In this manual, I will use the installation from the apt repository method.
1 Set up the Docker apt
repository.
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
2 Install the Docker packages.
To install the latest version, run:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Lets run hello-world
image to verify the installation:
sudo docker run hello-world
If you see Hello from Docker!
message, it’s a sign that you have now successfully installed and started Docker Engine.
As you noticed, you have to run docker with sudo
and you will get permissions denied if you will try to run docker run
without sudo
.
That because the docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
- Add the
docker
group (it might be already exist):
sudo groupadd docker
- Add the connected user “$USER” to the
docker
group:
sudo gpasswd -a $USER docker
This adds the current user to the docker
group. If you want to add another user, change the username to match it.
- Either do a
newgrp docker
or log out/in to activate the changes to groups:
newgrp docker
- Run
docker run hello-world
command to test it:
docker run hello-world
If you liked my articles, join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.
As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.
Subscribe here: https://junior-devops-hub.ck.page