Install Docker on Ubuntu Server

Updated: 2019/07

As you might be already aware, Docker by Docker Inc is a widely used tool for containers. Containerization is an approach to software development in which an application and its versioned set of dependencies plus its environment configuration abstracted as deployment manifest files are packaged altogether (the container image), tested as a unit and finally deployed (the container or image instance) to the host Operating System (OS).

Containers isolate applications from each other on a shared operating system (OS). This approach standardizes application program delivery, allowing apps to run as Linux or Windows containers on top of the host OS (Linux or Windows). Because containers share the same OS kernel (Linux or Windows), they are significantly lighter than virtual machine (VM) images.

In this post, we’ll go through series of steps required to install Docker on Ubuntu Server. You need to be running on 14.04 LTS or 16.04 or 18.10 LTS version for this. We would be installing docker on Ubuntu 16.04 LTS18.10 Cosmic Release for this post’s purpose.

Uninstall previous versions of Docker

Lets first make sure that no existing versions of Docker binaries are present on your system by running below command:

sudo apt-get remove docker docker-engine docker.io containerd runc

Click yes, if it asks for confirmation. If none of the versions are installed previously, its okay:

uninstall previous versions of docker

Install Docker Binaries

First thing is to make sure that system binaries are up to date. This can be done by running:

sudo apt-get update
sudo apt-get upgrade


Unless you have a strong reason not to, install the linux-image-extra-* packages, which allow Docker to use the aufs storage drivers.

For new installations on version 4 and higher of the Linux kernel, overlay2 is supported and preferred over aufs. Docker CE uses the overlay2 storage driver by default. If you need to use aufs instead, you need to configure it manually by using below:

sudo apt-get install \
 linux-image-extra-$(uname -r) \
 linux-image-extra-virtual
Install linux image extra packages
Install linux image extra packages

Next step is to setup Docker’s repositories. This way the upgrade and installation can be made easier and it is more widely used approach. Alternatively, you can use curl to download Debian package and perform installation/upgrades manually. We would be using former method for this post.

First step would be to install packages to allow apt to use a repository over HTTPS (in most cases these should be already installed):

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

install basic pre-requisites

Now, we need to add Docker’s official GPG key and verify the same:

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

add docker GPG key and verify

Next we need to setup repository. We would be setting up stable repository path:

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

add docker repo to ubuntu

Now, we need to update the apt index:

sudo apt-get update

After this, we can check which versions of docker are available using below command:

apt-cache madison docker-ce

check docker versions available

The contents of the list depend upon which repositories are enabled, and will be specific to your version of Ubuntu. We can go with the latest version or choose specific versions of our choice. For now, we would go with latest version (but we’ll still be specifying version while using command):
sudo apt-get install docker-ce=17.03.2~ce-0~ubuntu-xenial

sudo apt-get install docker-ce docker-ce-cli containerd.io

install docker ce latest version

When prompted to go ahead, type Y (for yes).

Docker will be installed and docker daemon should be started automatically.

Verify Docker is Working Properly

Now we would be testing if docker is working correctly or not. First we can use below command for this purpose:

docker --version

verify the docker version

We can also try to run a sample image called hello-world to check if docker is running correctly:

sudo docker run hello-world

You should be seeing below output:

run a hello-world image from docker

You need to use sudo to run further commands. for example, we can use below commands to show running images in docker:

sudo docker images

view list of docker images on system

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.

If you don’t want to use sudo when you use the docker command, create a Unix group called docker (which should be created as part of installation) and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group. In our case, we would be adding current user to docker group using below command (you need to change this to your user):

sudo usermod -aG docker mogoya

add yourself to docker group

Next, we need to logout and login back to get new privileges assigned. After this, we would be able to run docker commands as non-sudo:

run the hello-world image using docker

Do note that docker group has same privileges as root user. So, all users added to this group will have root user access.

Useful Notes

A copy of source code used in this blog post has been uploaded to GitHub at this location and available under master and blog/4744 branches.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s