How to install Docker on Ubuntu?
Launched in 2013, Docker has risen to becoming one of the quintessential tools for developers and DevOps engineers. It is an open-source containerization technology that makes it easier to build and deploy applications. Docker allows you to package applications in isolated containers. These containers contain the application, its libraries, dependencies and configuration files, all in one place. In this article, we will learn how to install Docker!
In the Serverspace you can create a server with already installed app "Docker".
Deploying applications in containers ensures they can be deployed in different computing environments consistently. Consistent application deployment eliminates worries about which platforms the app will run on and gives developers more time to focus on coding.
This guide will take you through the step-by-step process of installing Docker on Ubuntu in detail.
Prerequisites
Before we get started, ensure that you have the following in check:
- An instance of Ubuntu.
- A regular user with sudo or administrative privileges.
- A stable internet connection.
Without much further ado, let’s begin
Installing Docker on Ubuntu
Docker can be installed on Ubuntu in two ways. You can install Docker from the Ubuntu repositories, or you can install it from the Docker official repository. The latter, installing from the official docker repository, is the preferred option, as it guarantees you will get the latest version of Docker.
To get started with Docker, you need to update the package list on your Ubuntu system. This can be done by running the following command:
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:
DISTRO=$(. /etc/os-release && echo "$ID")
RELEASE=$(lsb_release -cs)
sudo add-apt-repository "deb [arch=${ARCH}] https://download.docker.com/linux/${DISTRO} ${RELEASE} stable"
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:
In this guide, you learned how to install Docker on Ubuntu. 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.