How to Install Docker on Ubuntu 16.04 “Xenial” and Ubuntu 14.04 “Trusty” Linux systems

undefined

In this article, I’ll show you how to install Docker on Ubuntu 16.04 “Xenial” and Ubuntu 14.04 “Trusty” Linux systems. This is your first step to get started with Docker engine on Ubuntu Linux. As most of developers use Ubuntu Linux on their machines, this article is for them. I strongly recommend using CentOS 7 or Debian 8 Linux for production and staging servers for their stability and security.
On this machine we will install Docker engine, allowing you to run a docker image (container) within docker engine.

Introduction to Docker.

Docker is an open-source tool that makes creating & managing Linux containers(LXC) easy. Containers are like lightweight VMs which can be started & stopped in milliseconds. Dockers help the system administrators & coders to develop their application in a container and can further scale up to 1000 of nodes. Working with Docker containers adds many benefits for developers: better separation of application and the server configuration.

The main difference between container and VM(Virtual machine) is that dockers provide process based isolation , whereas VM provides full isolation of resources. Virtual machine takes a minute to start where as container can be started in a second or less than a second. Container uses the Kernel of host OS , whereas VM uses the separate Kernel. Container uses less disk space and memory than VM.

Prerequisites

Docker requires a 64-bit OS and version 3.10 or higher of the Linux kernel. So, we need to check these two requirements against our machine.

Hints:

1. You must be the root user to run the following steps and do not use sudo command.
2. If you used sudo command, some of the following commands will give errors.

To be a root user, run the following command:

$ sudo su -

To check your current kernel version and OS architecture, run the following command:

# uname -r
3.13.0-107-generic
Hint:

1. Here's I'm using Ubuntu 14.04 and the kernel version is 3.13.0-107,for Ubuntu 16.04 the kernel version will be above 4.4 in general.

Perfect, our server is has the prerequisites for installing Docker. Let’s start our steps for installing Docker.

Step 1: Recommended extra packages

Now, as its recommended, we’ll gonna install the Linux Kernel Extras in our machine running Ubuntu. We’ll need to install these packages as its important for us to enable the use of aufs storage driver. So, to install the linux-image-extras kernel packages in our machine, we’ll need to run the following command:

Hints:

1. The following command may fail to install the two packages, in this case, it's save to ignore it and continue with the next step.
2. The success of failure of the following command depends on your Ubuntu release and the installed packages on it.
# apt-get -y install linux-image-extra-$(uname -r) linux-image-extra-virtual

For adding the Apt repository for Docker, we need to make sure that the following points are successfully ran on the server. These points for removing old Docker packages “if exists” and for adding the new APT repository fro Docker, run the following commands:

      1. Purge any older repositories.
         # apt-get purge "lxc-docker*"
         # apt-get purge "docker.io*"
        
      2. Update package information, ensure that APT works with the https method, and that CA certificates are installed.
         # apt-get update
         # apt-get -y install apt-transport-https ca-certificates gnupg2
        
      3. Add the new GPG key.
         # apt-key adv \
               --keyserver hkp://ha.pool.sks-keyservers.net:80 \
               --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
        
      4. Now, according to your Ubuntu release “16.04 Xenial or 14.04 Trusty”, run one of the following command to add the docker repository:
        • For Ubuntu 16.04, to create the docker.list file in  /etc/apt/sources.list.d/ directory in your terminal run the following command:
        # echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" >>/etc/apt/sources.list.d/docker.list
        • For Ubuntu 14.04, to create the docker.list file in  /etc/apt/sources.list.d/ directory in your terminal run the following command:
        # echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" >>/etc/apt/sources.list.d/docker.list
      5. Update the APT package index.
        # apt-get update
        
      6. Verify that APT is pulling from the right repository.
        # apt-cache policy docker-engine
        

        From now on when you run apt-get upgrade, APT pulls from the new apt repository.

Before installing Docker, make sure you have set your APT repository correctly as described in the prerequisites. Run the following command to update the APT package index:

 # apt-get update

We can install docker-engine with the following command:

# apt-get -y install docker-engine

Perfect!, We installed the docker engine on Ubuntu Linux system.

The Docker engine is now installed on different Ubuntu releases, we need to enable it to run at boot and start the service.

  • For Ubuntu 16.04

Run the following commands:

# systemctl enable docker 
# systemctl start docker
  • For Ubuntu 14.04

Run the following commands:

# update-rc.d docker enable
# service docker restart
Hints: 

1. If you want normal user to use docker, you need to add this normal user to the docker group.
2. By adding a user to the docker group, you'll have a full privileges on different docker commands, See the following optional step.

Step 5: Running Docker with Normal User Privileges “Optional”

You need to add your normal user to the docker group created by default when we installed the Docker packages. Here’s I’ll add user “msemari” to docker group:

# usermod -aG docker msemari

Now, logout and login again using this user “your normal user” and enjoy playing with Docker.

Here’s I give you two extra points for upgrading your Docker engine to the latest version and to completely remove Docker engine from your Ubuntu Linux.

  • Upgrade Docker

To install the latest version of Docker with apt-get:

$ sudo apt-get upgrade docker-engine
  • Uninstall

To only uninstall the Docker package:

$ sudo apt-get purge docker-engine

To uninstall the Docker package and dependencies that are no longer needed:

$ sudo apt-get autoremove --purge docker-engine

The above commands will not remove images, containers, volumes, or user created configuration files on your host. If you wish to delete all images, containers, and volumes run the following command:

$ sudo rm -rf /var/lib/docker

Locate and delete any user-created configuration files.

Summary

I think, Docker if the future replacement of the VMs as it’s a lightweight server, it do not need a dedicated memory or disk or CPU power. Your server can run number of containers higher than the number of VMs with same server’s hardware. Personally, I’m moving from using VMs to use Docker containers to save my machines resources. Finally, with help of this article you could install Docker engine and enable, start it. I also included how to completely remove Docker packages.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

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