How to Install Docker on Ubuntu

This guide will show you how easy it is to install the latest version of Docker to your Ubuntu system.

Ubuntu Install Docker

Docker is an OS-level virtualization platform that is designed so you can deliver software within a self-contained package.

These self-contained packers are called containers. A container is completely separated from the host system. This separation allows it to have its own operating system and configuration.

Docker enables you to define channels for the containers to talk to the operating system or another container.

One of the most significant advantages of installing Docker on your Ubuntu system is that you can quickly install software using it.

Instead of installing several different packages and adding additional clutter you can instead pull a containerized version with everything it needs.

Docker has installable versions of its engine for all current major versions of Ubuntu, including Ubuntu 18.04 and Ubuntu 20.04.

For the following steps, you will need to either use SSH to your Ubuntu system or be utilizing the Ubuntu terminal directly.

Installing Docker on Ubuntu

In this guide, we will be installing Docker from the official Docker package repository. This repository is always guaranteed to have the latest available version of the software, as it is from the official source.

The only downside of this method is that it requires a couple of additional steps as you need to add the repositories GPG key and the repository itself.

1. Before we get started, let us perform an update of our Ubuntu system.

To update the package list and the packages themselves, run the following two commands.

sudo apt update
sudo apt upgrade

2. To install Docker to Ubuntu from the official repository, we will need some additional packages.

These packages add some additional functionality to the apt package manager and Ubuntu.

Run the following command on your system to install the required packages.

sudo apt install curl apt-transport-https ca-certificates

You will notice that we are installing three additional packages. Each of them is quite useful for the following steps.

curl – We will be using curl to fetch the gpg key for the Ubuntu versions of Docker. curl allows us to pipe this key directly to the apt package manager.

apt-transport-https – By default, the apt package manager does not have support for the HTTPS protocol.

By installing this package, we will be able to interact with the official Docker repository, which is served over HTTPS.

ca-certificates – The final package we install is the bundle of certificates. These certificates are what helps the system know that the website it is connecting to is who they say they are.

3. Our next step is to add the GPG key for the Ubuntu repository to the APT key chain.

This key is used to verify that the packages you are installing are from that repository.

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

Once the key has been added, you should see the text “OK” appear in the terminal.

4. Finally, with the GPG key added, we can add the repository itself to the sources list.

We can do this using the following simple one-liner. We pipe our repository string directly into a new sources file called “docker.list“.

echo "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee -a /etc/apt/sources.list.d/docker.list

To ensure we add the correct line for your version of Ubuntu, we use “lsb_release -cs“.

This command retrieves the current codename for the distribution. For example, on our system, it would add “focal” as we are using Ubuntu 20.04.

5. As we have made changes to the repository sources, we need to perform another update.

Without performing an update, Ubuntu will not be aware of the Docker repository we just added.

sudo apt update

6. Finally, we can install Docker itself to our Ubuntu system.

With the repository added, all we need to do is run the following command to install the community edition of Docker.

sudo apt install docker-ce

At this point, you should now have successfully installed Docker from the official repository.

Testing your new Docker Installation

With Docker now installed on your Ubuntu system, let us test it to ensure everything is working correctly.

We will do this in two ways. The first is to get it to print out the current version number. The other is to pull a simple container.

1. The easiest way to check if Docker is now installed on your Ubuntu system is to get it to output its version.

To do that, all you need to do is run the following command within the terminal.

docker --version

From this, you should get the version of docker that you have installed to your system. The message should look a bit like what we have below.

Docker version 20.10.6, build 370c289

2. Our next step is to pull a small example container called “hello-world“.

Pulling an image using Docker is as simple as using “docker pull” followed by the name of the image you want to pull.

sudo docker pull hello-world

Docker will proceed to download the container to your system and prepare it for use.

3. Once Docker has finished pulling the container to your Ubuntu system, you can run it.

Again running a Docker container is a straightforward process. All we need to use is “docker run” followed by the container name.

sudo docker run hello-world

When this container runs, it will output text letting you know that your installation appears to be working correctly.

It will also give you a quick rundown of everything that occurred to produce that message.

Controlling the Docker Service on Ubuntu

Now you may want to start and stop the Docker service yourself. This is a reasonably straightforward process and can be controlled through systemctl.

There are four different things that you can do with the Docker service, that is to start, stop, disable, or enable it.

Additionally, you can also check the status of the service to see if it is currently running or not.

Starting the Docker Service

Getting the Docker service to start on Ubuntu, all you need to do is use the following command.

sudo systemctl start docker

When you first install Docker to your Ubuntu system, it will be automatically started as part of the installation process.

Stopping Docker on Ubuntu

Shutting the Docker service down is a very straightforward process and only requires you to use the following command.

sudo systemctl stop docker

Getting Docker to Start on Boot

If you want Docker to boot up at the same time as when your Ubuntu system does, you need to enable its service.

You can enable the service by running the command on your system.

sudo systemctl enable docker

The Docker service is automatically enabled during the installation process.

Disabling the Docker Service

If you have issues with Docker or do not want to have it automatically startup, you need to disable the service.

Disabling the service is as straightforward as using the following command.

sudo systemctl disable docker

Retrieving the Status of Docker

As Docker is running as a service, you may want to check on its status from time to time.

Systemctl keeps track of all services running on your Ubuntu system. This allows you to see whether the software is currently running or crashed.

To check the status of Docker on your Ubuntu system, you can use the following command within the terminal.

sudo systemctl status docker

Conclusion

At this stage, you should now have successfully installed Docker to your Ubuntu system.

Docker is an incredibly powerful container system that is also straightforward to use.

You can easily pull prebuilt containers from services like Docker hub with a single command.

If you have run into any issues with running Docker on Ubuntu, please leave a comment below.

Be sure to check out some of our other great Ubuntu guides.

2 Comments

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Harry,

      Thank you for pointing this out. I have updated the tutorial so we now grab the current package architecture.

      It should now work correctly if you are running an ARM64 or AMD64 system.

      Cheers,
      Emmet

Leave a Reply

Your email address will not be published. Required fields are marked *