How to Install and Use Docker on Ubuntu 18.04

By 

Updated on

7 min read

Install and Use Docker on Ubuntu 18.04

Docker is a containerization technology that allows you to quickly build, test, and deploy applications as portable, self-sufficient containers that can run virtually anywhere. Docker has become the de facto standard for container deployment, and it is an essential tool for DevOps engineers and their continuous integration and delivery pipeline.

In this tutorial, we’ll cover how to install Docker on an Ubuntu 18.04 machine and explore the basic Docker concepts and commands.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges . All the commands in this tutorial should be run as a non-root user.

Installing Docker on Ubuntu

Although the Docker installation package is available in the official Ubuntu 18.04 repository, it may not always be the latest version. The recommended approach is to install the latest Docker package from the Docker’s repositories.

Enabling Docker repository

  1. Start by updating the packages list and installing the dependencies necessary to add a new repository over HTTPS:

    Terminal
    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
  2. Import the repository’s GPG key using the following curl command:

    Terminal
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. Add the Docker APT repository to your system:

    Terminal
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Installing Docker CE

Now that the Docker repository is enabled, you can install any Docker version you need.

  1. To install the latest version of Docker use the command below. If you want to install a specific Docker version, skip this step and go to the next one.

    Terminal
    sudo apt update
    sudo apt install docker-ce
  2. To install a specific version, first list the available versions in the Docker repository:

    Terminal
    apt list -a docker-ce

    The command prints the available Docker versions in the second column.

    output
    docker-ce/bionic 5:18.09.7~3-0~ubuntu-bionic amd64
    docker-ce/bionic 5:18.09.6~3-0~ubuntu-bionic amd64
    docker-ce/bionic 5:18.09.5~3-0~ubuntu-bionic amd64

    For example, to install version 18.09.6 you would type:

    Terminal
    sudo apt install docker-ce=5:18.09.6~3-0~ubuntu-bionic

    To prevent the Docker package from being automatically updated, mark it as held back:

    Terminal
    sudo apt-mark hold docker-ce

Once the installation is completed, the Docker service will start automatically. You can verify it by typing:

Terminal
sudo systemctl status docker

The output will look something like this:

output
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor p
   Active: active (running) since Tue 2019-07-02 11:28:40 UTC; 15min ago
     Docs: https://docs.docker.com
 Main PID: 11911 (dockerd)
    Tasks: 10
   CGroup: /system.slice/docker.service

Executing docker command without sudo

By default, running Docker commands requires administrator privileges.

To run Docker commands as a non-root user without prepending sudo you need to add your user to the docker group. This group is created during the installation of the Docker CE package. To do that run the following command:

Terminal
sudo usermod -aG docker $USER

$USER is an environment variable that holds your username.

Log out and log back in to refresh the group membership.

To verify that Docker has been successfully installed and that you can run docker commands without prepending sudo, run:

Terminal
docker container run hello-world

The command will download a test image, run it in a container, print a “Hello from Docker” message and exit. The output should look like the following:

Docker Hello World

Upgrading Docker

When a new Docker version is released you can update the package using the standard upgrade process:

Terminal
sudo apt update
sudo apt upgrade

Uninstalling Docker

Before uninstalling Docker remove all containers, images, volumes, and networks .

You can uninstall Docker as any other package installed with apt:

Terminal
sudo apt purge docker-ce
sudo apt autoremove

Docker Command-Line Interface

The Docker CLI command takes this form:

Terminal
docker [option] [subcommand] [arguments]

To list all available commands type docker with no parameters:

Terminal
docker

If you need more help on any [subcommand], you can use the --help switch as shown below:

Terminal
docker [subcommand] --help

Docker Images

A Docker image is made up of a series of filesystem layers representing instructions in the image’s Dockerfile that makes up an executable software application. An image is an immutable binary file including the application and all other dependencies such as libraries, binaries, and instructions necessary for running the application.

You can think of a Docker image as a snapshot of a Docker container.

Most Docker images are available on Docker Hub. The Docker Hub is cloud-based registry service which among other functionalities is used for keeping the Docker images in public or private repositories.

Search Docker Image

To search for an image from the Docker Hub registry, use the search subcommand.

For example, to search for an Ubuntu image, you would type:

Terminal
docker search ubuntu

The output should look like this:

Docker Search Image

As you can see, the search prints a table with five columns, NAME, DESCRIPTION, STARS, OFFICIAL and AUTOMATED.

The official image is an image that Docker develops in conjunction with upstream partners.

Most Docker images on Docker Hub are tagged with version numbers. When no tag is specified, Docker will pull the latest one.

Download Docker Image

For example, to download the latest official build of the Ubuntu 18.04 image, you would use the following image pull command:

Terminal
docker image pull ubuntu
Docker Pull Image

Depending on your Internet speed, the download may take a few seconds or minutes.

When not specifying a tag, Docker pulls the latest Ubuntu image, which at the time of writing this article is 18.04.

If you want to download a previous Ubuntu release , let’s say Ubuntu 16.04 then you need to use docker image pull ubuntu:16.04.

To list all downloaded images type:

Terminal
docker image ls

The output will look something like this:

Remove Docker Image

If for some reasons, you want to delete an image, you can do that with the image rm [image_name] subcommand:

Terminal
docker image rm ubuntu
Docker Remove Image

Docker Containers

An instance of an image is called a container. A container represents a runtime for a single application, process, or service.

It may not be the most appropriate comparison, but if you are a programmer, you can think of a Docker image as class and Docker container as an instance of a class.

We can start, stop, remove, and manage a container with the docker container subcommand.

Start Docker Container

The following command will start a Docker container based on the Ubuntu image. If you don’t have the image locally, it will download it first:

Terminal
docker container run ubuntu

At first sight, it may seem to you that nothing happened at all. Well, that is not true. The Ubuntu container stops immediately after booting up because it does not have a long-running process, and we didn’t provide any command. The container booted up, ran an empty command, and then exited.

The switch -it allows us to interact with the container via the command line. To start an interactive container type:

Terminal
docker container run -it ubuntu /bin/bash
output
[root@719ef9304412 /]#

As you can see from the output above, once the container is started, the command prompt is changed. This means that you’re now working from inside the container .

List Docker Containers

To list active containers, type:

Terminal
docker container ls
Docker List Containers
Info
If you don’t have any running containers, the output will be empty.

To view both active and inactive containers, pass it the -a switch:

Terminal
docker container ls -a
Docker List All Containers

Remove Docker Containers

To delete one or more containers copy the container ID (or IDs) and paste them after the container rm subcommand:

Terminal
docker container rm c55680af670c

Conclusion

You have learned how to install Docker on your Ubuntu 18.04 machine and how to download Docker images and manage Docker containers. You may also want to read about Docker Compose , which allows you to define and run multi-container Docker applications.

This tutorial barely scratches the surface of the Docker ecosystem. In some of our next articles, we will continue to dive into other aspects of Docker. To learn more about Docker check out the official Docker documentation .

If you have any questions, please leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page