How To Install Docker on Ubuntu 18.04

Image from “Download Docker For Ubuntu” tutorial.

Introduction

Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.

In this tutorial, you’ll learn how to install and use it on an existing installation of Ubuntu 18.04.

Note: Docker requires a 64-bit version of Ubuntu as well as a kernel version equal to or greater than 3.10. The default 64-bit Ubuntu 18.04 server meets these requirements.

Installing Docker

Note: All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo.

The Docker installation package available in the official Ubuntu 18.04 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. This section shows you how to do just that.

First, add the GPG key for the official Docker repository to the system:

Add the Docker repository to APT sources:

Note: If you get an E: Package 'docker-ce' has no installation candidate error when using only stable APT source this is because the stable version of docker for Ubuntu 18.04 doesn’t exist yet.
Meanwhile you have to use the edge / test version.
Stable releases are done quarterly, so .03.06.09 and .12 are stable releases.
Starting with Docker 17.06, stable releases are also pushed to the edge and test repositories.

Next, update the package database with the Docker packages from the newly added repo:

Make sure you are about to install from the Docker repo instead of the default Ubuntu 18.04 repo:

apt-cache policy docker-ce

You should see output similar to the follow:

Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 18.04. The docker-ce version number might be different.

Finally, install Docker:

sudo apt-get install -y docker-ce

Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:

sudo systemctl status docker

The output should be similar to the following, showing that the service is active and running:

Installing Docker now gives you not just the Docker service (daemon) but also the docker command line utility, or the Docker client. We'll explore how to use the docker command later in this tutorial.

Executing the Docker Command Without Sudo (Optional)

By default, running the docker command requires root privileges — that is, you have to prefix the command with sudo. It can also be run by a user in the docker group, which is automatically created during the installation of Docker. If you attempt to run the docker command without prefixing it with sudoor without being in the docker group, you'll get an output like this:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

sudo usermod -aG docker ${USER}

To apply the new group membership, you can log out of the server and back in, or you can type the following:

su ${USER}

You will be prompted to enter your user’s password to continue. Afterwards, you can confirm that your user is now added to the docker group by typing:

id -nG

Output:

username sudo docker

If you need to add a user to the docker group that you're not logged in as, declare that username explicitly using:

sudo usermod -aG docker username

The rest of this article assumes you are running the docker command as a user in the docker user group. If you choose not to, please prepend the commands with sudo.

Run Docker Containers (Optional)

Run a docker container using the docker run command to download and start the container.

docker run hello-world

Output: This confirms us that Docker is correctly installed.

Here are some interesting tutorials for you to get started: