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.
In the Serverspace you can create a server with already installed app "Docker".
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:
- 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
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:
Once you have updated your list of installed packages install the prerequisites required for the installation of Docker:
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:
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:
For Ubuntu to synchronize with the newly added repository, 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:
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:
However, If you want to install the latest version , simply run the command:
When prompted to continue, simply press ‘Y’ and hit ENTER. Once installed, check the status of Docker by executing:
The output confirms that Docker is up and running. 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:
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 !’.
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:
For example to search for Nginx image run the command:
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:
For example, to download the Nginx image, execute:
Usually, docker images are tagged and bear version numbers. To be more specific when downloading an image, you can specify the tag as follows:
Let’s assume you want to download an Ubuntu 20.04 image. The command for downloading the image will be:
To list images downloaded on your system, execute the command:
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:
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:
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:
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:
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:
or
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:
To stop a Docker container, again feel free to use either the container name or container ID using the docker stop command:
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:
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.