Read in 6 minutes
How To 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 virtually run everywhere. 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.
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.
Install 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.
To install Docker on Ubuntu 18.04, follow these steps:
First, update the packages index and install the dependencies necessary to enable a new repository over HTTPS:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Import the repository’s GPG key using the following curl command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker APT repository to your system’s software repository list by typing:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Now that the Docker repository is enabled, update the apt package list and install the latest version of Docker CE (Community Edition) with:
sudo apt update sudo apt install docker-ce
When the installation is completed the Docker service will start automatically. You can verify it by typing:
sudo systemctl status docker
The output will look something like this:
* docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-05-20 0122 PDT; 3s ago Docs: https://docs.docker.com Main PID: 14152 (dockerd) Tasks: 10 CGroup: /system.slice/docker.service `-14152 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
At the time of writing, the latest version of Docker available for Ubuntu 18.04 is
18.09.6. Check the Docker version by typing:
Docker version 18.09.6, build 481bc77
Executing the Docker Command Without Sudo
By default managing, Docker requires administrator privileges. If you want to run Docker commands as a non-root user without prepending
sudo you need to add your user to the docker group which is created during the installation of the Docker CE package. You can do that by running the following command:
sudo usermod -aG docker $USER
Log out and log back in so that the group membership is refreshed.
To verify that Docker has been successfully installed and that you can run docker commands without prepending
sudo, type the following command which will download a test image, run it in a container, print a “Hello from Docker” message and exit:
docker container run hello-world
The output should look like the following:
Docker command line interface
The Docker CLI command takes this form:
docker [option] [subcommand] [arguments]
To list all available commands run
docker with no parameters:
If you need more help on any
[subcommand], you can use the
--help switch as shown below:
docker [subcommand] --help
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 either in a public or private repository.
Search Docker Image
To search for an image from Docker Hub registry just use the
For example, to search for an Ubuntu image, you would type:
docker search ubuntu
The output should look like this:
As you can see the search results prints a table with five columns,
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 image.
Download Docker Image
Foe example to download the latest official build of the Ubuntu 18.04 image, you would use the following
image pull command:
docker image pull ubuntu
Depending on your Internet speed, the download may take a few seconds or a few minutes.
When not specifying a tag, docker will pull the latest Ubuntu image which is 18.04. If you want to pull some of the previous Ubuntu versions, let’s say Ubuntu 16.04 then you need to use
docker image pull ubuntu:16.04.
To list all downloaded images type:
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:
docker image rm ubuntu
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:
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, so the container booted up, ran an empty command and then exited.
-it allows us to interact with the container via the command line. To start an interactive container type:
docker container run -it ubuntu /bin/bash
[[email protected] /]#
As you can see from the output above, once the container is started, the command prompt is changed meaning that you’re now working from inside the container:
List Docker Containers
To list active containers, type:
docker container ls
To view both active and inactive containers, pass it the
docker container ls -a
Remove Docker Containers
To delete one or more containers just copy the container ID (or IDs) and paste them after the
container rm subcommand:
docker container rm c55680af670c
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 question, please leave a comment below.