How to Install Docker on Ubuntu 20.04: A Step-by-Step Guide
Docker is a containerized platform that allows individuals to bundle and execute applications within containers. These containers provide a level of isolation similar to virtual machines, yet they are characterized by enhanced portability and efficiency in resource usage. Furthermore, these distinctive isolation and security features enable the concurrent execution of numerous containers on a single host.
Containers are autonomous software entities or units that encompass their unique application code, configuration settings, and necessary dependencies. Docker operates on a client-server model, with the Docker daemon used for container management.
In this guide, we will demonstrate how to install Docker on Ubuntu 20.04 and explore the different Docker commands for managing containers. You can also visit how to install Docker Compose on Ubuntu if you want to learn further about Docker-related tasks.
You should have the below prerequisites to install Docker on the Ubuntu system:
- A running Ubuntu 20.04 or later server.
- The user should have root or sudo privileges to install Docker.
Now, let’s go through the Docker installation in detail!
Installing Docker on Ubuntu 20.04
The Docker package is available for installation in the default Ubuntu repository, but it may not be the latest version. However, we will use the official Docker repository to get the latest Docker version. To use the Docker repository on the Ubuntu 20.04 system, you will include a new package source and GPG key from Docker to proceed with the valid installation.
Steps to Install Docker on Ubuntu 20.04
To install Docker on Ubuntu 20.04, open the “Terminal” using the keyboard shortcut ‘Ctrl+Alt+t’ and then follow the below-listed steps:
Step 1: Remove the Existing Docker Installation
Before starting the Docker installation on Ubuntu 20.04, it is recommended that you remove the unnecessary existing Docker installation packages or files from your system. You can remove these packages by running the below-given command:
$ sudo apt-get remove docker docker-engine docker.io
After executing the above command, you need to enter the “sudo” password and hit “Enter.”
Step 2: Update the Apt Package Repositories
Update the system apt repositories by executing the following command:
$ sudo apt update
Step 3: Install the Required Docker Dependencies
Now, you need to install the important Docker dependencies that are necessary to run Docker on your Ubuntu system. To do that, install “apt-transport-https,” which allows you to SSL data transfer, “ca-certificates,” and “software-properties-common,” which will enable you to manage PPA. Also, install “curl” to download the GPG and data from the internet.
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
After executing the above command, all required Docker packages will be installed on your system that will allow “apt” to use packages over HTTPS.
Step 4: Download the Docker Repository GPG key
After installing the Docker prerequisites, download the GPG key for the official Docker repository and add this key to the apt-key trusted key manager using the following command:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add
Step 5: Add the Docker Repository to Ubuntu 20.04
After Downloading the GPG key, you can add the Docker repository to the system Apt sources by executing the following command:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Press “Enter” to add the Docker repository to your Ubuntu system. You should verify the installation source priority for the “docker-ce” package to ensure that it's being installed from the software repository you've added most recently. To verify it, run this command:
$ sudo apt-cache policy docker-ce
Step 6: Install Docker on Ubuntu 20.04
Before installing Docker on Ubuntu, it is good practice to update the system apt packages index. Make sure to obtain the most up-to-date installation package requirements, whether you're working locally or not.
$ sudo apt update
After updating all repositories, it’s time to install Docker on Ubuntu 20.04. You will install the Docker community edition here. This Docker platform offers complete core features but no commercial support. Furthermore, it typically omits enterprise features that are often unnecessary for small and medium-sized businesses. To install Docker on Ubuntu, execute this command:
$ sudo apt install docker-ce
The command above will automatically fetch and install all the necessary Docker packages on your Ubuntu system. After installing Docker, its service should be up and running on your system.
Step 7: Start the Docker Service
Once Docker is installed, its service will automatically enable and start on your system. You can verify the Docker service running status by using the following command:
$ sudo systemctl status docker
If you find that the Docker service is disabled or is not running, you can manually enable and start it by running these commands on the terminal:
$ sudo systemctl enable docker
$ sudo systemctl start docker
Now, again verify the Docker service status:
$ sudo systemctl status docker
As you can see, the Docker service is up and running on this system.
Step 8: Check the Docker Version
After completing the installation, check the Docker version to display information about Docker by using this command:
$ sudo docker version
The installed Docker version should be displayed on the terminal window.
An alternative is also available to view information about Docker currently running on your system:
$ sudo docker info
Run Docker Commands Without “sudo” on Ubuntu 20.04
By default, the docker command can only be run by the root user or by a user in the Docker group, which is automatically created during Docker’s installation process. If you attempt to run the Docker command without prefixing it with sudo or without being in the Docker group, you’ll get an output like this:
Docker commands, by default, only run under root or superuser privileges, or the user should add to the Docker group, which is assigned during the Docker’s installation. If you want to run Docker commands without using “sudo” or root privileges, you will see the below output on the terminal:
To avoid using “sudo” as a prefix while running the Docker command, you need to add this “user” to the Docker group. After that, you will be able to run the docker commands as a non-root user:
$ sudo usermod -aG docker username
To use the new Docker group membership, reboot your system or log out from the server from your current account. Again, login to your system and run this command:
$ su - username
Verify whether the user is added to the Docker group or not using the following command:
How to Run Docker Commands on Ubuntu 20.04?
In this section, we will explore and run different Docker commands on the Ubuntu system. Docker provides a sequence of options and commands, followed by arguments. To view all Docker subcommands, type “docker” on the terminal:
To view the system-wide information, run this command:
$ docker info
Run Docker Images
Docker images are like building blocks for Docker containers. Docker pulls these images from a repository called Docker Hub, which is like a big storage or registry managed by the Docker company. Many different programs and things you might want to use are stored there because it's open for anyone to put their Docker images.
Let’s run the test image for Docker “hello-world.” To run a Docker “hello-world” image, use this command:
$ docker run hello-world
The above command will find the hello-world image locally. If the image is not found, it will pull this image from the Docker Hub repository. Docker will automatically download this image and run it inside a Docker container. If this image successfully runs on your Ubuntu system, you will see the following output:
Search for a Docker Image
To find images on Docker Hub, you can use the “docker” command with the “search” option. For instance, if you want to look for the “MongoDB” image, you can type:
$ docker search MongoDB
Pull Docker Image
After searching Docker images, you can pull these images from the Docker Hub. For example, you searched for MongoDB Docker images. In the search results, you can see that the “mongo” Docker image is available for download. To download or pull this image from the Docker hub, use the following command:
$ docker pull mongo
View Docker Images
To get all local Docker images, use the following command:
$ docker images
As you can see, the “hello-world” image is now locally available in the Docker library.
List Docker Container Log
To list all Docker container logs or container state, run this Docker command:
$ docker container ls -a
Manage Docker Containers
After running Docker commands, you can also manage Docker containers. To view active docker containers, use the following command:
$ docker ps
To view all active and inactive Docker containers, execute “docker ps” with switch “-a:”
$ docker ps -a
As you can see from the screenshot above, the above command will display all Docker containers. Two containers are created, one being ”‘hello-world” and the other “mongo.”
You can start an existing Docker container using the container ”Names.” For example, to start a ”mongo” container, you can use this command:
$ docker container start charming_noyce
Now, run the “docker ps” command again:
As you can see, the status of the ”mongo” container is up and running on this system.
To open a command line shell on a running container, type the following command:
$ docker exec -it charming_noyce /bin/bash
Now, you can run Linux commands or install applications inside this container:
In this guide, we showed you how to install Docker on Ubuntu 20.04. In addition, we explored useful Docker commands that help us in managing containers.
By using Docker, users can run multiple containers on a single piece of hardware, isolate and manage applications, and increase productivity. Also, by using the instructions provided above, you can install Docker on Linux VPS servers, which offer more advanced features.
Should you have any more queries, don’t hesitate to share them in the comments. Thanks!