Adding the Official Docker Repository
Follow these steps to configure the Apt package manager with Docker's official package source.
First, update the package index and install prerequisite utilities:
sudo apt update
sudo apt install ca-certificates curl
The ca-certificates package contains root certificaets for verifying secure HTTPS connections. The curl tool is used to transfer data from servers.
Create a directory to hold repository keyrings:
sudo install -m 0755 -d /etc/apt/keyrings
The install -m 0755 -d command creates a directory with read and execute permissions for all users.
Download Docker's public GPG key and save it to the keyrings directory:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
The -fsSL flags make curl fail silently on errors, follow redirects, and suppress progress output.
Set appropriate read permissions on the key file:
sudo chmod a+r /etc/apt/keyrings/docker.asc
This grants read access to the key file for all user categories.
Add the Docker Apt repository to your system's sources list:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
This command constructs a repository entry specifying the system architecture, GPG key location, and Ubuntu codename, then writes it to a dedicated sources file.
Update the package index again to include the new Docker repository:
sudo apt update
Installing Docker Packages
Install the Docker Engine and associated components:
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Configuring Non-Root User Access
By default, Docker commands require sudo privileges. To allow a regular user to run Docker commands, add them to the docker group.
First, verify if the docker group exists:
grep docker /etc/group
If the group doesn't exist (which is rare with a standard installation), create it:
sudo groupadd docker
Add you're user to the docker group:
sudo usermod -aG docker $USER
Replace $USER with your actual username.
To activate the new group membership for your current session, log out and log back in, or run:
newgrp docker
Verifying the Installation
Check the installed Docker version to confirm successful setup:
docker version
This command displays both client and server version information if Docker is running correctly.