news
Serverspace has became Free TON validator, launched DePool, and gives bonuses for TON Crystals holders

How to install Docker on Ubuntu 18.04 LTS

WB
William Bell
May 28, 2020

Launched in 2013 , Docker has risen to become one of the most quintessential tools among developers and DevOps engineers. Docker is an open source containerization technology that simplifies building and deploying applications. It enables the packaging of applications in isolated environments called containers. Inside these containers, applications are shipped alongside their requisite libraries, dependencies, and configuration files.

Deploying applications in containers ensures that they can be deployed across different computing environments with utmost consistency. Consistency in the application deployment eliminates worry over which platforms the application will be run in and gives  developers more time  to focus on writing code.

In this guide, we dive deep and take you through a step-by-step procedure of how to install Docker on Ubuntu 18.04 LTS.

Prerequisites

Before we get started, ensure that you have the following in check:

  1. An instance of Ubuntu 18.04 LTS.
  2. A regular user with sudo or administrative privileges.
  3. A stable internet connection.

Without much further ado, let’s begin

Installing Docker on Ubuntu 18.04

Docker can be installed on Ubuntu using two ways. You can either install Docker from Ubuntu repositories or install it from Docker’s official repository.  The latter,  — installing from the official Docker repository — is the more preferred option because it guarantees that you get the latest version of docker.

To get started with installing Docker from Docker repository, first update the Ubuntu package list as shown:

$ sudo apt update

Once you have updated your list of installed packages install the  prerequisites required for the installation of Docker:

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

In the example below, these prerequisites have already been installed, so the system will skip the installation. If a newer version is available, the existing packages will be upgraded.

If a newer version is available, the existing packages will be upgraded

Next,  proceed and import Docker’s GPG key as shown using the curl command-line tool:

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

You will get an ‘OK’ reply on the terminal as shown:

You will get an ‘OK’ reply on the terminal as shown

With the GPG key in place, append Docker’s repository to the sources.list file as shown:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

With the GPG key in place, append Docker’s repository to the sources.list file

For Ubuntu to synchronize  with the newly added repository, update the package list:

$ sudo apt update

Update the package list

At this point, you are now ready to install Docker using the APT package manager. However, before you do so, you might want to have a peek at the various versions of docker packages available for download. To print a list of docker packages  execute the command:

$ apt list -a docker-ce

The list of Docker packages is ordered from the latest to the earliest as displayed in the output below.

The list of Docker packages is ordered from the latest to the earliest

To install a specific version of Docker , for example  version 19.03.7 run the command:

$ sudo apt install docker-ce=5.19.03.7~3-0~ubuntu-bionic

However, If you want to  install the latest version , simply run the command:

$ sudo apt install docker-ce

Install the latest versio

When prompted to continue, simply press ‘Y’ and hit ENTER. Once installed, check the status of Docker by executing:

$ sudo systemctl status docker

Check the status of Docker

The output confirms that Docker is up and running. To check the version of Docker installed, run the command:

$ docker --version

To check the version of Docker installed, run the command

As expected, the latest version — at the time of writing this guide — has been installed.  To confirm that docker was installed correctly run the hello-world container:

$ sudo docker container run hello-world

The command initializes a few processes. First, it pulls a test image from  Docker hub which is a repository for Docker images. (We shall discuss images in the next section ). Once the image is successfully downloaded, a container is spawned from the image that runs the application which displays the message ‘Hello from Docker !’.

The command initializes a few processes

Docker Images

A docker image is a read-only binary file that ships with an application alongside its binaries, libraries, dependencies, and the requisite instructions needed for successfully running the application.

Docker images are hosted on Docker hub which is a cloud-based library or repository. Docker hub gives you access to thousands of images from open source projects and other software vendors.

Searching a Docker image

To search an image from Docker use the syntax:

$ sudo docker search image

For example to search for Nginx image run the command:

$ sudo docker search nginx

As shown in the table below, the output includes information about the image such as image name, a brief description of the image, and its popularity indicated as ‘Stars’ in the third column.

Searching a Docker image

Downloading a Docker image

To download a docker image from Docker hub to your local system, use the command shown:

$ sudo docker pull image

For example, to download the  Nginx image, execute:

$ sudo docker pull nginx

Downloading a Docker image

Usually, docker images are tagged and bear version numbers. To be more specific when downloading an image, you can specify the tag as follows:

$ sudo docker pull image:tag

Let’s assume you want to download an Ubuntu 20.04 image. The command for downloading the image will be:

$ sudo docker pull ubuntu:20.04

Download an Ubuntu 20.04 image

To list images downloaded on your system, execute the command:

$ sudo docker images

The output is arranged in a tabular format with 5 columns displaying information such as the images’ repository, image tag, image ID,  how long ago the image was created, and the size of the image.

The output is arranged in a tabular format with 5 columns

To remove a Docker image run the command:

$ sudo docker image rm ubuntu:20.04

To remove a Docker image run the command

Docker containers

A docker container is a runtime instance of a docker image. It’s in a Docker container that an application is packaged alongside its libraries and dependencies.

To launch a docker container based on the Ubuntu image we downloaded earlier, execute the command:

$ sudo docker container run ubuntu:20.04

Launch a docker container based on the Ubuntu image

At first glance, it may appear as if nothing really happened, but that is far from the truth. The Ubuntu container started and stopped immediately since we issued no commands to it.

To run an OS  container more effectively, use the -it arguments to access the bash shell and interact with it:

$ sudo docker container run -it ubuntu:20.04

Once you run the command, you should get a bash shell prompt that allows you to run commands as though you were executing them on an actual Ubuntu 20.04 instance.

Docker containers

Listing Docker containers

To display a list of running Docker containers, use the command:

$ sudo docker container ls

$ sudo docker ps

Listing Docker containers

The output gives you statistics such as the container ID, image name, when the container was last created, and status to mention a few. From the output, we can see that we only have Nginx container running.

To view all containers, both active and dormant, execute:

$ sudo docker container ls -a

or

$ sudo docker ps -a

The output prints out 4 more containers which are inactive.

The output prints out 4 more containers

Starting and stopping docker containers

To start a Docker container, use the docker start command using either the container name or container ID.

For example, to start the  Ubuntu container, run:

$ sudo docker start c28e836e7fbf

Starting and stopping docker containers

To stop a Docker container, again feel free to use either the container name or container ID using the docker stop command:

$ sudo docker stop c28e836e7fbf

Stop a Docker container

Removing a Docker container

To remove a docker container, use the docker rm command. Once again feel free to use either the container name or container ID:

$ sudo docker rm c28e836e7fbf

Removing a Docker container

Conclusion

In this guide, you learned how to install Docker on Ubuntu 18.04. Additionally, we gave you handy tips on how you can manage Docker images and containers on your system. It’s our hope that this tutorial was informative.

Start Your Cloud Journey Migration made simplified. Take the first step right now.
We use cookies to provide our services and for analytics and marketing. To find out more about our use of cookies, please see our Privacy Policy. By continuing to browse our website, you agree to our use of cookies.