How to install Docker on Ubuntu

Docker solves the it works on my machine situation. While also providing a way to quickly deploy your apps with just some configs in the form of Docker files.
I was experimenting with various VPS configurations before purchasing a long-term plan and every time I had the same repetitive setup workflow - first the VPS, then the environment required to host my apps, and then finally start setting up the apps, DB, etc.
For quite some time I have been looking forward to an opportunity where I could leverage Docker. This seemed like the perfect opportunity to start exploring Docker and the benefits of containerizing my apps.
Prerequisites
-
A Ubuntu machine with SSH access or GUI where you will install Docker.
-
Docker hub account if you want to create and push your images. Alternatively, you can also use your GitHub account to publish container images.
Steps to install and get started with Docker:
Docker helps run containerized applications. Containers are similar to virtual machines but more portable and light-weight. Most commonly solves the problem of but it works on my machine when you run the app in your peers or deploy to server.
Remove any older package for a fresh start:
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
Setup the apt repository for Docker:
# Adds the docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Adds the repository to apt sources:
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
sudo apt-get update
Install the latest Docker packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Check the status of the Docker service:
sudo systemctl status docker
Verify the setup by running the official Hello World image:
sudo docker run hello-world
(Optional) If you have installed Docker under a non-root user
You will need to run the Docker commands by prefixing sudo, but we can avoid that by adding the required non-root user to the Docker group:
For the currently logged-in user:
sudo usermod -aG docker ${USER}
For any other user:
sudo usermod -aG docker <user>
Logout and then log back in for the new group permissions to come into effect or force it using:
su - ${USER}