How to Install Docker on Ubuntu 16.04

By | August 24, 2018

Brief Introduction

Docker is a computer program which is build to run multiple microservices (Applications) in the same machine on top of the container (Runtime System). It is a containerization platform which usually provides the virtualization at operating system levels. Though there is a difference between the virtual machine and a container, and the major difference is that the container does not run on the top of the Hypervisor, unlike Virtual machines.  We can Consider Docker container as a lightweight alternative to the virtual machine. 

Nowadays, Containers are used heavily by software developers, sysadmins, Software testers etc to quickly build, deploy, run and test their application. Using Docker container you can quickly deploy and use almost any kind of applications quickly and in an automated fashion using a Dockerfile. Just take an example of an application “Jenkins“, which can easily we installed on top of Docker container and can be used on any machine.

This can be achieved usually by 2 ways, first is writing a Dockerfile, which is somewhat similar to the executing shell commands, only the thing is we use some Docker Directives (RUN, CMD, COPY etc.) to execute the set of organized set of commands. After the successful executions of your Dockerfile, you will get a Docker image which can be used anywhere to run that application.

Running an application on Docker container in an automated fashion using Dockerfile is the recommended and fast way to do. You can also use the manual process to create a docker image in order to run an application by simply pulling a base operating system image from the DokcerHub, Running that image, entering into it and setting up the application manually and eventually can commit that image to use anywhere to run that particular application, but ideally its not the recommended way. Though you can test installing any application manually inside Docker Container and later can automate it by writing a Dockerfile.

In this post, I have tried to explain the use case of Docker in the simplest way I can do and will not go into its architecture in details. let’s discuss some advantages of using Docker in your environment and later we will see how we can setup Docker in Ubuntu 16.

  • Flexible: It is flexible to use and can be used to containerize any complex application. This containerization of the application can also be easily automated.
  • Portable: You can build on your local machine and can deploy to any server (Onprem or Cloud) using the Docker images.
  • Scalable: Can easily create replicas of your container and can also be updated on the fly
  • Lightweight: Containers share the host kernel. The base operating system comes up with very minimal packages.

Now lets Start with the Installation Process of Docker CE (Community Edition) in Ubuntu 16. For installation of Docker on RHEL/CentOS 7 refer here

Login into the ubuntu machine and update the repository by using below command.

root@devopsage:~# apt-get update

Note: In new versions of Ubuntu we can only use apt rather than using apt-get.

Install few pre-requisite packages by using the below command, if not installed.

root@devopsage:~# apt-get install apt-transport-https ca-certificates curl software-properties-common

Docker package is available in the official ubuntu 16.04 itself, but in order to install the latest docker, we will use the official docker repository. Lets first add the GPG key of the official docker repository so that we can fetch the package from there.

root@devopsage:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

Add the docker repository to the apt source.

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

You can also very in the source list by executing below command,

root@devopsage:~# cat /etc/apt/sources.list | grep docker
deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable

Lets update the repository from the added docker repository

root@devopsage:~# apt-get update

To make sure that the  docker is installed from the docker repository, not from the local ubuntu 16.04 repository, lets cache it by using below command,

root@devopsage:~# apt-cache policy docker-ce
docker-ce:
  Installed: (none)
  Candidate: 18.06.1~ce~3-0~ubuntu
  Version table:
     18.06.1~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     18.06.0~ce~3-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     18.03.1~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     18.03.0~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     17.12.1~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     17.12.0~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     17.09.1~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     17.09.0~ce-0~ubuntu 500
        500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
     .
     .

Now we are all set to install docker

root@devopsage:~# apt-get install docker-ce -y

(Optional)Note: By default, it install the latest version of docker but if you need to install the specific version of docker use the below command

root@devopsage:~# apt-get install docker-ce=17.09.0~ce-0~ubuntu

Check whether the docker service is running or not.

root@devopsage:~# systemctl status docker.service

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-08-24 15:39:27 UTC; 5min ago
     Docs: https://docs.docker.com
 Main PID: 18302 (dockerd)
   CGroup: /system.slice/docker.service
           ├─18302 /usr/bin/dockerd -H fd://
           └─18311 docker-containerd --config /var/run/docker/containerd/containerd.toml
      .
      .

If the Docker Service is not running, make sure to start it

root@devopsage:~# systemctl start docker.service

Now docker is successfully installed on your Ubuntu machine. Verify it by checking its version.

root@devopsage:~# docker -v
Docker version 18.06.1-ce, build e68fc7a

root@devopsage:~# docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:24:56 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:23:21 2018
  OS/Arch:          linux/amd64
  Experimental:     false

root@devopsage:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

Note: You may not be able to run the docker command as a normal user by default, you can run only by root user or using sudo privileges.

testuser@devopsage:~$ docker images
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied

To avoid this issue, you can either give full permission to docker.sock or put this user to the docker group.

testuser@devopsage:~$ sudo usermod testuser -aG docker  // Logout and login to that user and check

testuser@devopsage:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

OR, a Simple but not recommended method of solving this issue is to simply give full permission to docker.sock

root@devopsage:~# chmod 777 /var/run/docker.sock

Selecting Storage Driver (Optional)

The storage driver controls how images and containers are stored and managed on your Docker host. Check the default storage driver by, (by default its overlay)

Note: Do not change unless and until it is required.

root@devopsage:~# docker info | grep -i storage
Storage Driver: overlay2

For changing this to devicemapper (not recommended for production use) or aufs or any,

root@devopsage:~# vim /etc/docker/daemon.json
{
  "storage-driver":"devicemapper"      
}
:wq
root@devopsage:~# systemctl restart docker.service
root@devopsage:~# docker info | grep -i storage
Storage Driver: devicemapper
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
         Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.

WARNING: All your images will be lost, if you change your storage driver. before changing the storage driver you have to backup your current images

 

Also, Refer

How to install Docker on RHEL/CentOS7

If you Like Our Content here at Devopsage, then please support us by sharing this post.

Please Like and follow us at, LinkedIn, Facebook, Twitter, and GitHub

Also, Please comment on the post with your views and let us know if any changes need to be done.

Thanks!!

4 thoughts on “How to Install Docker on Ubuntu 16.04

Leave a Reply to gaurav Cancel reply

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