Install Docker on Ubuntu

install docker on ubuntu

Docker is a great way to build multiple apps with different environments for various purposes, all in your local system without changing much of the settings of your local system. Since everything is run in Linux Containers, we do not need to worry about any changes happening to our Host OS.
There are multiple ways to install Docker in your Ubuntu system like Installing directly from Ubuntu Software centre, Downloading the Deb package and install it from the .deb file etc.

We will install by adding the Docker repositories in our System and then install it from those repository. This way we will get the latest stable Docker version for our system.

Below are the steps.

Install Docker on Ubuntu

Step 1: A pre-step of Updating before we start:

Let’s update the apt package index first:

sudo apt update

Step 2: Install required packages

Install packages to allow apt to use a repository over HTTPS:

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

Step 3: Add Docker GPG Key

Add Docker’s official GPG (GNU Public Guard) key:

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

Step 3.1: Verify the Key

Verifying the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

sudo apt-key fingerprint 0EBFCD88
# Output

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

Step 4: Select a Docker Channel and target CPU Arch to install

There are multiple repository channels like stable, nightly, test where you can install Docker from. You just need to give the name of the particular channel. We will install from the stable repository. If you wish to see the nightly and test channels, you can check here.

Docker is available for Multiple CPU architectures like x86_64/amd64, armhf, arm64 etc.

Step 4.1: Determine CPU Architecture

Before starting the Install, let’s determine our CPU architecture

dpkg --print-architecture
# Output

amd64

My system has the amd64 architecture, so we will install Docker for that architecture.
If you get some other value for your architecture like armhf/arm64/ppc64el/s390x please replace the arch value in the below command accordingly. For eg. [arch=s390x].

Step 4.2: Add Channel and CPU Arch name

So, Let’s add the amd64 arch in our docker repository name, since our CPU Arch is amd64 and stable channel for the stable release of Docker.

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

Step 5: Update package index with new repositories added

Update the apt package index.

sudo apt update

Step 6: Install Docker CE on Ubuntu

We will now Install the latest version of Docker

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

If you wish To install a specific version of Docker CE, list the available versions in the repo, then select and install: 
1. List the versions available in your repo:

    apt-cache madison docker-ce
# Output
docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
docker-ce | 18.06.1~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages

b. Install a specific version using the version string from the second column, for example, 5:18.09.0~3-0~ubuntu-xenial.

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

Step 7: Run and Verify Docker is installed in our Linux system

Now, Verify that Docker CE is installed correctly by running the hello-world image.

sudo docker run hello-world

This will download the hello-world pre-compiled docker image, run it, print a message and exit.

So our docker installation is ready to go and you can start creating and downloading containers to run multiple apps as you wish.

The After Steps

If you are still with me here, you might want to go through a few more steps to work with more ease with your docker installation.

Step 8: Check docker group

So, Docker Community Edition (Docker CE) is installed. The installation also creates a docker group in our system. Let’s check our /etc/group file to confirm.

sudo less /etc/group

Last line of the above file shows:

docker:x:999:

Step 8.1: Add User to Docker group

Now, though the docker group was added in our system, like we saw in the /etc/group file, there were no users added to the docker group yet.

You will have to add your user to the docker group in order to use the docker commands without sudo. Else, you will always need to use sudo to allow non-privileged users to run docker commands.

So let’s go ahead and add our user to docker group so that we can use the docker commands without needing to pre-pend sudo.

sudo usermod -aG docker <your_username>

Now, we will have to logout and login again into the system for the changes to take effect. Since, we’ve added our username to the docker group, we don’t need to use sudo, after logging in.

Note: 
If you get Permission error, it's probably because your user does not have the necessary permissions to work with the docker commands yet. 

If you are testing on a Virtual Machine, you might have to restart the Virtual Machine.

So now, let’s run docker without prepending sudo and verify. We will run the hello-world image, again.

docker run hello-world

And it does the expected thing!

So our Docker installation is now good to go and we can now create our containers and start developing our apps inside them!

Author: Admin

Hello Friends, welcome to Hackerdays.com. I am the Admin of the site. I have worked as a software programmer for 7 years. I have started this site to learn as well as share whatever little knowledge i have gained over the period of time. If you wish to contact me, you can drop in an email to: admin[AT]hackerdays[DOT]com or you can also use the contact page. Thank you and I'll see you around.

Let us know your thoughts: