How to install Docker on Ubuntu 18.04 LTS
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.
Before we get started, ensure that you have the following in check:
- An instance of Ubuntu 18.04 LTS.
- A regular user with sudo or administrative privileges.
- A stable internet connection.
Without much further ado, let’s begin
Step 1: 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.
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
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"
For Ubuntu to synchronize with the newly added repository, update the package list.
$ sudo apt update
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.
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
When prompted to continue, simply press ‘Y’ and hit ENTER. Once installed, check the status of Docker by executing:
$ sudo systemctl status docker
The output confirms that Docker is up and running. To check the version of Docker installed, run the command:
$ docker --version
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 !’.
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.
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
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
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.
To remove a Docker image run the command:
$ sudo docker image rm ubuntu:20.04
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
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.
Listing Docker containers
To display a list of running Docker containers, use the command:
$ sudo docker container ls
$ sudo docker ps
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
$ sudo docker ps -a
The output prints out 4 more containers which are inactive.
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
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
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
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.