How To Install and Use Docker Engine on Ubuntu 20.04 (Tutorial)

How to Install and use Docker Engine on Ubuntu 20.04. In this tutorial we will introduce Docker and how it works with it’s main advantages then move onto installation phase with how to install Docker from the regular Ubuntu repository. We shall also explain some basic Docker command (Docker to start, stop) and how to interact with Docker container.

Docker is one of the most popular containerization technology used by developers. It helps developers to create, deploy, and run applications with the help of containers.

What Is Docker?

Docker is one of the popular OS virtualized software platforms that have changed the way software development used to be. It provides tremendous economies of scale and even makes development scalable, and keeps the process user friendly. Docker is a  ‘platform as a service’ products and services  to apply in virtualization to provide software in packages called docker containers.

Docker is one of the best software for IT organizations that enables to create, deploy and run applications effortlessly. It contains all dependencies within them. It has a lightweight container that comprises all the instructions and dependencies, such as frameworks, libraries, and bins within it.

The best part of this container is that it can be moved from one environment to another quite effortlessly. In the DevOps cycle, deployment is where the Docker shines. It is because while deploying a solution, you acquire the guarantee that the tested code will work in the production environment.

Moreover, while building or testing the code, you need to have a container that runs the solution on the stages as it has the capability to validate the same environment used for production.

Advantages of Using Docker

Return On Investment and Cost Savings

ROI is the first advantage provided by Docker. The management decisions made while selecting a new product are considered the Return On Investment. The more the solution drives down costs, the more you tend to raise profits.

And Docker is considered the best in such situations. It facilitates this type of savings by dramatically reducing infrastructure resources. It requires fewer resources to run similar applications. This way, organizations tend to save on everything from server costs to the employees needed to maintain them. It allows the engineering team to be smaller and more effective.

Compatibility and Maintainability

Docker provides parity, which means that your images run the same, regardless of the server or laptop they are running on. Hence, developers will spend less time on setting up environments, debugging environment specific issues, and also attain a more portable and easy to set up codebase. It also makes the production infrastructure more reliable and easier to maintain.

Immediate Deployment

Docker can reduce deployment within seconds. It creates a container for every process and does not boost an OS. You can easily create or destroy your data without worrying about the cost incurred. It will be highly affordable.

Security

In the case of security, Docker ensures that applications running on containers are completely segregated and isolated from each other. It provides you with complete control over traffic flow and management.

Multi Cloud Platforms

Docker is highly portable. Over the past few years, all the crucial cloud computing providers, like Amazon Web Service (AWS) and Google Compute Platforms (GCP), have not only embraced Docker availability but also add individual support.

Straightforward and Immediate Configurations

Docker is highly straightforward. It means users can take their own configurations, put them into code, and deploy it without facing any issues. Since it is versatile, the infrastructure requirements are no longer linked with the environment of the applications. In short, developers can easily and immediately configure Docker tools into their system.

Follow this post, to explain how to install Docker Engine on Ubuntu 20.04.

Install Docker Engine Ubuntu 20.04

Prerequisites

  • A server running Ubuntu 20.04 operating system along with ssh access.
  • A root user or a user with sudo privileges.
  • Stable internet connection.

Add Docker Repository

By default, the latest version of Docker CE is not available in the Ubuntu default repository. So you will need to add the Docker CE repository to APT. First, install the required dependencies using the following command:

				
					apt install curl gnupg2 wget -y
				
			

Once all the dependencies are installed, add the Docker CE GPG key using the following command:

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
				
			

Next, add the Docker CE repository to APT using the following command:

				
					add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu\$(lsb_release -cs) stable"

				
			

Once the repository is added, update the repository cache using the following command:

				
					apt update -y
				
			

Once the repository is added, run the following command to install Docker Engine on your server.

				
					apt install docker-ce docker-ce-cli containerd.io -y
				
			

Once the Docker Engine is installed, start the Docker service and enable it to start at system reboot:

				
					systemctl start docker
systemctl enable docker
				
			

Verify the Docker Installation

After the Docker installation, you can also check the status of Docker service with the following command:

				
					systemctl status docker

				
			

You should see the following output:

				
					● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-06-04 16:09:50 UTC; 37min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 3502 (dockerd)
      Tasks: 16
     Memory: 190.6M
     CGroup: /system.slice/docker.service
             └─3502 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.208941392Z" level=info msg="No non-localhost DNS nameservers are left in >
Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.208980260Z" level=info msg="IPv6 enabled; Adding default IPv6 external se>
Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.268067457Z" level=info msg="No non-localhost DNS nameservers are left in >
Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.269667528Z" level=info msg="IPv6 enabled; Adding default IPv6 external se>
Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.334481515Z" level=info msg="No non-localhost DNS nameservers are left in >
Jun 04 16:40:56 ubuntu2004 dockerd[3502]: time="2022-06-04T16:40:56.334794893Z" level=info msg="IPv6 enabled; Adding default IPv6 external se>
Jun 04 16:45:25 ubuntu2004 dockerd[3502]: time="2022-06-04T16:45:25.442573727Z" level=info msg="ignoring event" container=3f4ccefd53953cac01d>
Jun 04 16:45:25 ubuntu2004 dockerd[3502]: time="2022-06-04T16:45:25.555962075Z" level=info msg="ignoring event" container=db01e784479fccba1fa>
Jun 04 16:45:25 ubuntu2004 dockerd[3502]: time="2022-06-04T16:45:25.562802861Z" level=info msg="ignoring event" container=8e210d7f15db03839d5>
Jun 04 16:45:25 ubuntu2004 dockerd[3502]: time="2022-06-04T16:45:25.565482536Z" level=info msg="ignoring event" container=330cf7a6e81e0944fc8>

				
			

To verify the Docker package information, run the following command:

				
					docker version
				
			

You should get the following output:

				
					Client: Docker Engine - Community
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:17:23 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:15:28 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

				
			

Create Your First Container Using Docker

At this point of the post how to Install and use Docker Engine on Ubuntu 20.04 the Docker is installed and running. Now, its time to create and run your first container. We will use the hello world container to test the Docker.

Run the following command to launch the hello world container:

				
					docker run hello-world
				
			

This command will download the hello world Docker image and start it as shown below:

				
					Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

				
			

Once the container is started, you can verify the downloaded Docker image using the following command:

				
					docker images
				
			

You should see the hello world Docker image in the following output:

				
					REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   8 months ago   13.3kB

				
			

How to Use Docker

In this section of how how to Install and use Docker Engine on Ubuntu 20.04, we will show you how to interact and use Docker container.

Search Docker

To search for any Docker image, use the following syntax:

				
					docker search image-name
				
			

For example, to search for an Ubuntu image, run the following command:

				
					docker search ubuntu
				
			

You will get a list of all Ubuntu images in the following output:

				
					NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   14368     [OK]       
websphere-liberty                WebSphere Liberty multi-architecture images …   284       [OK]       
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   112       [OK]       
neurodebian                      NeuroDebian provides neuroscience research s…   91        [OK]       
open-liberty                     Open Liberty multi-architecture images based…   53        [OK]       
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   50                   
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                46        [OK]       
ubuntu/mysql                     MySQL open source fast, stable, multi-thread…   33                   
ubuntu/apache2                   Apache, a secure & extensible open-source HT…   32                   
ubuntu/prometheus                Prometheus is a systems and service monitori…   27                   
kasmweb/ubuntu-bionic-desktop    Ubuntu productivity desktop for Kasm Workspa…   26                   
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   24                   
ubuntu/bind9                     BIND 9 is a very flexible, full-featured DNS…   21                   
ubuntu/postgres                  PostgreSQL is an open source object-relation…   17                   
ubuntu/redis                     Redis, an open source key-value store. Long-…   10                   
ubuntu/grafana                   Grafana, a feature rich metrics dashboard & …   6                    
ubuntu/prometheus-alertmanager   Alertmanager handles client alerts from Prom…   6                    
ubuntu/memcached                 Memcached, in-memory keyvalue store for smal…   5                    
ubuntu/telegraf                  Telegraf collects, processes, aggregates & w…   4                    
ubuntu/kafka                     Apache Kafka, a distributed event streaming …   4                    
ubuntu/zookeeper                 ZooKeeper maintains configuration informatio…   3                    
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   3                    
ubuntu/cassandra                 Cassandra, an open source NoSQL distributed …   2                    
bitnami/ubuntu-base-buildpack    Ubuntu base compilation image                   1                    [OK]
ubuntu/loki                      Grafana Loki, a log aggregation system like …   0                    

				
			

From the above list, download the Ubuntu image using the following command:

				
					docker pull ubuntu
				
			

This will download the Ubuntu image from the Docker Hub registry:

				
					Using default tag: latest
latest: Pulling from library/ubuntu
125a6e411906: Pull complete 
Digest: sha256:26c68657ccce2cb0a31b330cb0be2b5e108d467f641c62e13ab40cbec258c68d
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

				
			

You can verify the downloaded image using the following command:

				
					docker images
				
			

You will get the following output:

				
					REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        latest    d2e4e1f51132   5 weeks ago    77.8MB
hello-world   latest    feb5d9fea6a5   8 months ago   13.3kB

				
			

To create container from the downloaded Ubuntu image, run the following command:

				
					docker run -it ubuntu
				
			

This will create a container from the Ubuntu image and connect to the container shell as shown below:

				
					root@4013ea7fb65e:/# 
				
			

Here, you can install any package inside the Ubuntu container. Now, run the following command to exit from the Ubuntu container:

				
					exit
				
			

To create a create a container from the Ubuntu image and start it in background, run the following command:

				
					docker run -dit ubuntu
				
			

You can now verify the running container using the following command:

				
					docker ps
				
			

You should see the running container in the following output:

				
					CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
b113e9d06154   ubuntu    "bash"    4 seconds ago   Up 3 seconds             trusting_villani
				
			

How to Manage Docker Container

In this section, we will show you how to manage Docker container.

To start, stop or pause the Docker container, use the following syntax:

				
					docker start container-id
docker stop container-id
docker pause container-id
				
			

For example, to start the Ubuntu container, run the following command:

				
					docker start b113e9d06154
				
			

To stop the Ubuntu container, run the following command:

				
					docker stop b113e9d06154
				
			

To remove the stopped container, run the following command:

				
					docker container rm b113e9d06154
				
			

If you want to stop all running containers, run the following command:

				
					docker container stop $(docker container ls -aq)
				
			

If you want to remove all stopped containers, run the following command:

				
					docker container rm $(docker container ls -aq)
				
			

To remove the unused Docker images and containers, run the following command:

				
					docker system prune -a
				
			

Great! We have learned about how to install and use Docker Engine on Ubuntu 20.04.

How to Install and use Docker Engine on Ubuntu 20.04 Conclusion

In this post we illustrated how to install Docker CE on Ubuntu 20.04 server and also how to interact with Docker and different commands. I hope this guide will help a lot to manage the Docker container.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x