Install Docker CE on Ubuntu
So how does someone install Docker on Ubuntu? Let me show you… We’re starting off with a freshly installed Ubuntu 20.04 virtual machine that has been updated but has not had anything additional added to it yet.
We will start with adding some packages that are prerequisites for using the ‘apt’ commands over HTTPS and thus for Docker.
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Next, we’ll add the GPG key for the Official Docker repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Then we’ll add the Docker repo to the sources for APT.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
We’ll rerun our ‘update’ command.
sudo apt update
And the step everyone was waiting for, installing Docker.
sudo apt install docker-ce
Now we need to allow our user to run Docker commands without always needing to ‘sudo’. We can add the user we are logged in as by using this command.
sudo usermod -aG docker ${USER}
Okay now as an optional step, you can install Docker-Compose. At the time of writing this, it is on v2.7.0. You will want to check their release page and update the command below to the current version number.
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.7.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
Then we can set the correct permissions on docker-compose so that it will be executable.
chmod +x ~/.docker/cli-plugins/docker-compose
Now we can verify our Docker and Docker-Compose are installed by checking their versions with these commands.
docker compose version
docker version
And that is how you install Docker on your Ubuntu machine