How to Install Docker on Ubuntu 18.04 Server

Docker is a lightweight operating system-level virtualization solution that allows you to run multiple containers simultaneously on a single host or across a fleet of servers with the help of Orchestration tools. Docker containers are isolated from each other using Kernel Control Groups and Namespaces.

In this blog post, we'll look at How to install docker on Ubuntu 18.04 Server.

Docker provides an image-based deployment model which makes it easy to package an application with its all dependencies and share across multiple environments. Docker also automates the deployment of applications inside this container environment. Before we look at the installation and usage of Docker, let's define some terms which are common in the world of Docker containers.

  • Docker Engine: This is a server daemon which manages all Docker objects - Networking, containers, storage, images e.t.c. Docker Engine also provides REST API used by applications to communicate with the daemon.
  • Docker Image - An image is a read-only template used to create containers on docker engine.
  • Container - This is a running instance based on Docker image. Each container has a unique ID
  • Registry - Docker registry is storage for already created images. It can be a restricted repository (private) or a public repository.

Docker Installation on Ubuntu 18.04

There are three ways to install Docker on Ubuntu 18.04, namely:

  1. Installing Docker from apt repository
  2. Installing Docker from the compiled Debian package
  3. Installing Docker edge using automation script

We'll look installation of Docker from above two methods.

Installing Docker from apt repository

Before we add Docker repository, ensure there are no older packages of docker, unless you want to do upgrade instead of fresh installation.

$ sudo apt-get remove docker docker-engine docker.io

Note that the Docker CE package is now called docker-ce.

Install dependency packages

To allow apt to use a repository over https, install the packages below:

$ sudo apt-get install  software-properties-common ca-certificates apt-transport-https

Once the installation is successful, proceed to add docker apt repository.

Import official Docker gpg key

To install docker signed packages, you need to import gpg key:

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

Add docker-ce stable repository

Now let's add docker repository using add-apt-repository command.

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

Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:3 https://repos.influxdata.com/ubuntu bionic InRelease 
Get:2 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB] 
Hit:4 http://mirrors.digitalocean.com/ubuntu bionic InRelease 
Get:5 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease [83.2 kB] 
Hit:6 http://mirror.zol.co.zw/mariadb/repo/10.3/ubuntu bionic InRelease 
Get:7 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease [74.6 kB] 
Hit:8 https://packagecloud.io/grafana/stable/debian stretch InRelease 
Fetched 222 kB in 1s (149 kB/s)
Reading package lists... Done

Above command will add a line to /etc/apt/sources.list

deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable

This is where docker packages will be downloaded from.

Update apt index and install docker

Once the repository has been added to the system

$ sudo apt-get update
$ sudo apt-get install docker-ce

Start and Enable Service to start on Boot

With installation being successful, you should be able to start docker service using systemctl command.

$ sudo systemctl start docker
$ sudo systemctl enable docker
$ sudo systemctl is-enabled docker
enabled

Check the status of the service using:

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
 Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
 Active: active (running) since Tue 2018-06-05 19:37:30 UTC; 39min ago
 Docs: https://docs.docker.com
 Main PID: 4235 (dockerd)
 Tasks: 18
 CGroup: /system.slice/docker.service
 ├─4235 /usr/bin/dockerd -H fd://
 └─4284 docker-containerd --config /var/run/docker/containerd/containerd.toml

Jun 05 19:37:30 dokcer.linoxide.com  dockerd[4235]: time="2018-06-05T19:37:30.458294292Z" level=warning msg="Your kernel does not support swap memory limit"
Jun 05 19:37:30 docker.linoxide.com dockerd[4235]: time="2018-06-05T19:37:30.458346988Z" level=warning msg="Your kernel does not support cgroup rt period"
Jun 05 19:37:30 docker.linoxide.com dockerd[4235]: time="2018-06-05T19:37:30.458358517Z"

Running Docker as a non-root user

If you would like to use Docker as a non-root user, you should now consider adding your user to the “docker” group with something like:

$ sudo  getent group docker
docker:x:998:
$ sudo usermod -aG docker <username>

E.g

$ sudo usermod -aG docker jmutai

For immediate group change without having to log out run:

$ sudo gpasswd -a username groupname

E.g

$ sudo gpasswd -a jmutai docker

Installing Docker .deb package

An alternative installation method is downloading the latest release of Docker for Ubuntu 18.04. The problem with this method is that you need to download a new file each time you want to upgrade Docker CE.

Head over to  https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version and browse to pool/stable/ then choose your OS architecture. You need to download the .deb file for the Docker version you want to install.

Install the package by running the command:

$ sudo dpkg -i /path/to/package.deb

Installing Docker edge version using a script

Docker provides convenience scripts at get.docker.com for installing edge versions of docker. This version is not recommended for production purposes but can be used for test and general purpose. Note that the scripts require root or sudo privileges to run:

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

The script will do everything for you, no manual work needed. Only that if you would like to use Docker as a non-root user, you should now consider adding your user to the "docker" group with something like:

$ sudo usermod -aG docker your-user

Check docker version installed using docker version command:

$ sudo docker version
Client:
Version: 18.05.0-ce
API version: 1.37
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:16:13 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version: 18.05.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:14:23 2018
OS/Arch: linux/amd64
Experimental: false

Using Docker on Ubuntu 18.04

Now that we have everything we need ready, let's consider few docker operations to help you get started. We'll download two docker images, then create containers from them. Then later destroy these containers.

By default, docker images are downloaded from docker public repository, no authentication needed to pull publicly shared images. Let's download Ubuntu 18.04 and CentOS 7 image. The docker pull command is used for this:

$ sudo docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
a48c500ed24e: Pull complete 
1e1de00ff7e1: Pull complete 
0330ca45a200: Pull complete 
471db38bcfbf: Pull complete 
0b4aba487617: Pull complete 
Digest: sha256:c8c275751219dadad8fa56b3ac41ca6cb22219ff117ca98fe82b42f24e1ba64e
Status: Downloaded newer image for ubuntu:18.04

$ sudo docker pull centos:latest
latest: Pulling from library/centos
7dc0dca2b151: Pull complete 
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for centos:latest

Check images available:

$ sudo  docker images
REPOSITORY     TAG     IMAGE ID C     REATED        SIZE
centos         latest  49f7960eb7e4   22 hours ago  200MB
ubuntu         18.04   452a96d81c30   5 weeks ago   79.6MB

Create a container from Docker image

The docker run command is used to run docker container.  A container is a running instance of a docker image. It is also possible to create a custom docker image using Dockerfile. Refer to the link for how to write your own Dockerfile.

The example below will create a container named ubuntu18.04 from Ubuntu 18.04 image downloaded earlier.

$ docker run -it --rm --name ubuntu-cont ubuntu:18.04 /bin/bash
[email protected]:/#

The options used are:

  • -it : Get interactive shell session
  • --rm : This instructs docker to delete container on exit
  • --name: Used to specify the name of the container. The name given is ubuntu-cont
  • ubuntu:18.04: This is the name of docker image we're creating a container from
  • /bin/bash : Command to execute on the container

If you exit from container shell using exit command or ctrl+d

Read also :

With running Docker engine, you can begin playing with docker to understand its ins and outs. We have good guides on Linoxide to help you get started. Docker documentation is also a good place to start.

Josphat Mutai 12:05 am

About Josphat Mutai

Red Hat Certified Engineer with more than 2 years experience managing critical Cloud Infrastructures and Services. Passionate about Technology and loves testing and reviewing gadgets and Open-source tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

1 Comment