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 a portable, standalone container that can run almost anywhere. Docker has become the de facto standard for container deployment, and it’s an essential tool for DevOps engineers and continuous integration and delivery lines. This service has free and premium levels. The software that houses this container is called the Docker Engine.
Docker is a suite of platform as a services (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from each other and combine their own software, libraries and configuration files; they can communicate with each other via clear channels. All containers run on a single operating system kernel and are therefore lighter than virtual machines.
In this tutorial, I will cover how to install Docker on an Ubuntu 18.04 computer and explore the basic Docker concepts and commands.
Before continuing with this tutorial on how to install docker on ubuntu, make sure you are logged in as a privileged user
sudo. All commands in this tutorial must be run as a non-root user.
How to install Docker on Ubuntu
Although the Docker installation package is available in the official Ubuntu 18.04 repositories, it may not be the latest version. The recommended thing is to install the latest Docker packages from the Docker repositories.
Enabling the Docker Repository
Start by updating the packages list and installing the dependencies needed to add the new repository over HTTPS:
$ sudo apt update $ sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Import the repository’s GPG key using the command
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add it APT Repository Docker to your system:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Install Docker CE
Now that the Docker repositories are enabled, you can install the version of Docker you need.
To install the latest version of Docker, use the command below. If you want to install a specific version of Docker, then skip this step and go to the next.
$ sudo apt update
$ sudo apt install docker-ce
To install a specific version, first list the versions available in the Docker repositories:
$ apt list -a docker-ce
This command prints the version of Docker available 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:
$ sudo apt install docker-ce=5:18.09.6~3-0~ubuntu-bionic
To prevent Docker packages from automatically updating, mark them as holding:
$ sudo apt-mark hold docker-ce
After the installation is complete, the Docker service will start automatically. You can verify it by typing:
$ sudo systemctl status docker
The output will look 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
Running Docker Commands Without Sudo
By default managing Docker requires administrator rights.
To run Docker commands as non-root user without adding
sudo You have 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:
$ sudo usermod -aG docker $USER
$USER is an environment variable that holds your username.
Log out and log in again to refresh the group membership.
To verify that Docker has been successfully installed and that you can run docker commands without adding sudo, run:
$ docker container run hello-world
The command will download a test image, run it in a container, print the “Hello from Docker” message and exit. The output will look something like this:
How to Upgrade Docker
When new versions of Docker are released, you can update packages using the standard upgrade process:
$ sudo apt update
$ sudo apt upgrade
How to Uninstall Docker
Before removing Docker, delete all containers, images, volumes, and networks.
You can uninstall Docker as another package installed with apt:
$ sudo apt purge docker-ce
$ sudo apt autoremove
Docker Command Line Interface
The Docker CLI command takes this form:
$ docker [option] [subcommand] [arguments]
To see a list of all available commands type
docker without parameters:
If you need further assistance on
[subcommand] whatever, you can use
--help switches as shown below:
$ docker [subcommand] --help
A Docker Image consists of a series of file system layers that represent the instructions in the image Dockerfile that make 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 needed to run the application.
You can think of a Docker image as a Docker container snapshot.
Most of the Docker images are available on the Docker Hub. Docker Hub is a cloud-based registry service which among other functions is used to store Docker images in public or private repositories.
Search for Docker Image
To search for images from the Docker Hub registry, use the command
For example, to search for Ubuntu images, you would type:
$ docker search ubuntu
The output will look like this:
As you can see, the lookup prints a table with five columns, which consists of: NAME, DESCRIPTION, STARS, OFFICIAL and AUTOMATED.
The official images are images that Docker co-develops with upstream partners.
Most Docker images in the Docker Hub are tagged with a version number. When no tag is specified, Docker pulls the newest one.
Download the Docker Image
For example, to download the latest official build of 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 several 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, say Ubuntu 16.04 then you need to use ubuntu docker image pull: 16.04.
To list all downloaded image types, use the command:
$ docker image ls
The output will look like this:
How to Delete Docker Image
If for some reason, you want to delete an image, you can do so with the sub-command
image rm [image_name]:
$ docker image rm ubuntu
An instance of an image is called a container. A container represents the runtime for a single application, process, or service.
This may not be the most precise comparison, but if you are a programmer you can think of a Docker Image as a Class and a Docker Container as an instance of the Class.
We can start, stop, remove, and manage a container with the sub-command
Start the Docker Container
The following command will start a Docker container based on an Ubuntu image. If you don’t have the Image locally, it will download it first:
$ docker container run ubuntu
At first glance, it seemed that nothing had happened at all. But that’s not true. The Ubuntu container stops immediately after booting because it doesn’t have a long running process, and I didn’t issue any commands. The container boots up, runs an empty command, and then exits.
-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
Output [[email protected] /]#
As you can see from the output above, once the container is started the command prompt changes. This means that you are now working from inside the container.
List Docker Container
To see a list of active containers, type:
$ docker container ls
If you don’t have a container running, the output will be empty.
To see active and inactive containers, provide a switch
$ docker container ls -a
Delete the Docker Container
To delete one or more containers, copy the container IDs (or IDs) and paste them after the container sub-command
$ docker container rm c55680af670c
You have learned how to install Docker on your Ubuntu 18.04 computer 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 on how to install and use docker on Ubuntu is just a basic guide, to learn more about Docker, check out the official Docker documentation here.