How to install Docker on Ubuntu 18.04

I’m sure you must have heard the word Docker being thrown about many times but somewhat got scared of the entire concept. Well, be frightened no more. In this tutorial, we’ll demystify Docker and show you how you can install Docker on Ubuntu 18.04 Bionic Beaver. Docker is a free and open source platform that enables developers to run their applications in containers.  Containers are an equivalent of virtual machines only that they are lightweight, more portable and resource friendly in term of CPU and memory usage.

Installing Docker

To get the latest Docker version, it’s recommended that you install Docker from Docker’s official repository. But first, You need to add the GPG key for the official Docker repository to your Ubuntu 18.04 system.

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

Output

Ok

Next, You need to add the Docker repository to APT sources as shown

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

Next, update your System with Docker packages from the newly added repository

sudo apt-get update

Sample Output

Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [1        09 kB]
Get:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease         [107 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Get:5 https://download.docker.com/linux/ubuntu xenial InRelease [65.8 kB]
Get:6 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages [3,6        53 B]
Fetched 392 kB in 0s (727 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

Finally, install Docker in your System as shown

sudo apt-get install -y docker-ce

At this point, Docker should now be up and running. To confirm this run

systemctl status docker

Sample Output

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-07-14 11:58:48 UTC; 1min 28s ago
     Docs: https://docs.docker.com
 Main PID: 20309 (dockerd)
   CGroup: /system.slice/docker.service
           ├─20309 /usr/bin/dockerd -H fd://
           └─20315 docker-containerd --config /var/run/docker/containerd/containerd.toml

Working with Docker and Docker images

By default, containers pull images from Docker Hub which is a registry managed by Docker. practically anyone can create and host their Docker images on Docker Hub. In fact, a majority of Linux distros that require Docker containers have images that are already hosted on Docker Hub.

To verify if you can access and download images from Docker Hub, Run

docker run hello-world

You should get output similar to the one below

latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

To search for images on Docker Hub use the search sub-command. In the example below, we’ll search for Fedora

NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
fedora                               Official Docker builds of Fedora                671                 [OK]
mattdm/fedora                        A basic Fedora image corresponding roughly t…   49
fedora/apache                                                                        34                                      [OK]
mattsch/fedora-nzbhydra              Fedora NZBHydra                                 5                                       [OK]
vbatts/fedora-varnish                https://github.com/vbatts/laughing-octo/tree…   2                                       [OK]
darksheer/fedora22                   Base Fedora 22 Image -- Updated hourly          2                                       [OK]
darksheer/fedora25                   Hourly updated Fedora 24 Docker Hub Image       1                                       [OK]
darksheer/fedora                     Hourly update latest Fedora Image               1                                       [OK]
darksheer/fedora23                   Hourly updated Fedora 23                        1                                       [OK]
darksheer/fedora24                   Hourly update Fedora 24                         1                                       [OK]
ovirtguestagent/fedora-atomic        The oVirt Guest Agent for Fedora Atomic Host…   0
resin/artik710-fedora-golang         Go (golang) is a general purpose, imperative…   0
resin/artik10-fedora                 Docker builds of Fedora from Resin for Samsu…   0
mattsch/fedora-sonarr                Fedora Sonarr                                   0                                       [OK]
smartentry/fedora                    fedora with smartentry                          0                                       [OK]
vergissberlin/fedora-development     Docker fedora image to use for development, …   0                                       [OK]
vcatechnology/fedora                 A Fedora image that is updated daily            0                                       [OK]
rhub/fedora-gcc-devel                R-devel on Fedora latest                        0
buildstream/buildstream-fedora       A container image that provides the BuildStr…   0
chrisshort/docker-fedora25-ansible   Docker container for Ansible Role/Playbook C…   0                                       [OK]
urbaniak/fedora                      fedora with additional packages for python      0

To download an image an image from Docker Hub use the pull sub-command as shown

docker pull fedora

Sample Output

Using default tag: latest
Using default tag: latest
latest: Pulling from library/fedora
e71c36a80ba9: Pull complete
Digest: sha256:7ae08e5637170eb47c01e315b6e64e0d48c6200d2942c695d0bee61b38c65b39
Status: Downloaded newer image for fedora:latest

To view the number of images that have been downloaded to your computer, run

docker images

Output

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              2cb0d9787c4d        3 days ago          1.85kB
mysql               latest              8d99edb9fd40        2 weeks ago         445MB
httpd               latest              2a7d646dbba8        2 weeks ago         178MB
fedora              latest              cc510acfcd70        2 months ago        253MB
daduy/cacti         latest              90fd4a0f3281        3 years ago         526MB

Upon downloading the image, you can run a container using the downloaded image using the run sub-command.

docker run fedora

To have interactive shell access to the container use the -it flags

docker run -it fedora bash

Output

[root@6a0596b8fa81 /]#

Note that the 6a0596b8fa81 digit represents the container id.
To view active containers, run

docker ps 

To view both active and passive containers, run

docker ps -a

Output

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
9d48f3506f78        httpd               "httpd-foreground"       9 minutes ago       Exited (0) 9 minutes ago                           objective_mcnulty
2ed1fe816a33        mysql               "docker-entrypoint.s…"   11 minutes ago      Exited (1) 11 minutes ago                          affectionate_shockley
c19c66828c1d        hello-world         "/hello"                 11 minutes ago      Exited (0) 11 minutes ago                          elegant_northcutt
c7ec951bcbf0        httpd               "httpd-foreground"       12 minutes ago      Exited (0) 11 minutes ago                          quizzical_carson
0af73fe05c6e        hello-world         "/hello"                 About an hour ago   Exited (0) About an hour ago                       mystifying_golick

To stop a running container, run

docker stop container-id

That’s all we had for today, stay tuned for more engaging and informative articles. Your thoughts on this tutorial are welcome.

About James

Hey there! This is James, a Linux administrator and a tech enthusiast. I love experimenting with various distributions of Linux and keeping tabs on what's new in the Linux world.
Deploy a managed hourly billed Cloud Server!
Read More