How to Install Docker Engine on Ubuntu Machine

How to Install Docker Engine on Ubuntu Machine

While learning to use Docker, like every other tool, I had to start with its installation. And in this article, I will walk you through the steps I took to set everything up. Most of the information you would need is available on the official documentation, the link is here.

For this installation, I used Docker's documentation to create a bash script that can run on an Ubuntu terminal to set Docker up in less than four minutes. An additional step I will take is to include a repo link where you can grab and install the script if you are already knowledgeable about writing scripts.

Without wasting any more time, let's dive right into it.

Pre-requisites:

For this setup, I created a droplet on Digital Ocean. I read up on the requirements for installing Docker on a VM and found these recommendations:

  1. OS requirements:

    For a successful installation of Docker Engine, I read that the 64-bit version of any one of these Ubuntu versions: Ubuntu Lunar 23.04, Ubuntu Kinetic 22.10, Ubuntu Jammy 22.04 (LTS), Ubuntu Focal 20.04 (LTS), Ubuntu Bionic 18.04 (LTS) will work for Docker. And I picked the Ubuntu Focal 20.04.

  2. From here (documentation for installing the Docker desktop), I learnt that a minimum of 4GB RAM would be needed. This ensures there is enough memory for Docker to run on the system. You can follow this documentation to learn how to create a droplet on Digital Ocean.

So, ensure you create an Ubuntu droplet or have an Ubuntu VM on another cloud provider with at least 4GB of memory for this installation.

The Script:

After connecting to the Ubuntu terminal (follow this link to check how to connect to the droplet created), create a user that will be used to run the docker commands without using sudo as explained here. For this step, use the following commands:

adduser NAME-OF-USER

NAME-OF-USER in the block of code above will be your preferred username.

Then, open a new file and name it docker-install.sh (or any name you like but with the .sh extension) with this command:

vi docker-install.sh

This command will create a file with the Vi Editor for you. Press the i key on your keyboard to activate the insert mode on the editor while enabling you to edit the file. Copy and paste these lines of code into the file:

#!/bin/bash

# This script will install docker on a newly spun up VM
# update the ubuntu server
sudo apt-get update
sudo apt-get upgrade -y

# install packages to  allow apt to use a repository over HTTPS
sudo apt-get install ca-certificates curl gnupg -y

# Add Docker's official GPG key
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

# Set up the repository
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

# Update the apt repository
sudo apt-get update

# Install latest version of Docker Engine, containerd, and Docker Compose.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

After you have pasted this into your file, click on the ESC key, and save your file with :wq. This command will take you back to the Ubuntu terminal.

Running the Script:

The next step is to modify the permission for the file you created. For a script to run, you need to change its permissions to allow its execution by the user. This is achieved by using the following command:

chmod +x docker-install.sh

The next thing is to run your script with the following command:

./docker-install.sh

This should help to install Docker in under four minutes without you having to type out commands on your terminal.

To test your installation, you can type out docker --help or docker -v on your terminal.

Grab-and-Install Method

In my introduction, I mentioned that I will include a repo link for someone who already knows how to create a script but just needs an already prepared script to work with.

  1. Clone this repository and cd into the docker-installation directory.

  2. Make the docker-install.sh file executable, and run the script.

Best Practices

For best practice, it is advised that commands should not be run with the Superuser account. Hence, after connecting with the root user, the next thing you should do is to create a sudo user that will be added to the sudo group. The user can clone the repository and also run the script. While the docker user will then run any other tasks related to docker.

Conclusion

Note that the commands included in the bash scripts are those you would have typed out on your terminal one after the other to install Docker on your terminal. However, it is time-consuming and not a reusable method. Also, imagine that you have to install Docker on hundreds of servers, typing out the commands on each server would mean a herculean task and almost impossible.

With this bash script method, you can achieve a simplified and automated way of setting up Docker on your machine or any number of Ubuntu machines you would be working with.