How To Install Docker On Ubuntu

sohail
sohail

Table of Contents

Docker containers contain the application and all its dependencies to be run in isolation like virtual machines. But Docker containers isolate the operating system instead of the hardware. Unlike virtual machines, docker containers also use the host’s kernel, making the containers more portable and lighter.

If you use Ubuntu 20.04 or earlier version, this article will help you install Docker on Ubuntu.

What is Docker?

Docker was developed by Solomon Hykes and Sebastien Pahl and publicly released in 2013. It uses libcontainer to use resource isolation features of the Linux kernel. You can learn more about docker containers and how to create docker containers in the following article – What is docker container?

You can install docker easily on all Linux distributions. If you are using Ubuntu 16.04 or newer, you can follow this guide to install docker containers. Before we move forward, make sure that your distro is up-to-date. Use apt to check & install any pending update –

sudo apt update
sudo apt upgrade -y

After all the updates are installed, we are ready to start the docker installation.

How to install Docker on Ubuntu?

Uninstall old docker version

To install docker on Ubuntu using this guide, please remove any existing version. The following guide will install the latest version of docker containers, so get rid of the existing setup.

sudo apt remove docker docker-engine docker.io

Next up, install the packages that will let apt install packages over HTTPS.

sudo apt install apt-transport-https ca-certificates curl software-properties-common
Install apt transport https support
Install apt transport HTTPS support

Add docker GPG key

Add the docker’s GPG key to your distro –

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

Add docker repository

Now add the docker repository. Before using the following command, please note that the following command should run perfectly fine on versions from Ubuntu 16.04 to Ubuntu 20.04. If you are using any distro that is based on Ubuntu, then replace the command $(lsb_release -cs) with the codename of the Ubuntu version that your distro is based on.

For example, if you are using Elementary OS Hera, then you will have to replace $(lsb_release -cs) with bionic because its codebase is bionic.

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

Update system

That’s it. The docker repository is now added to your distro. Now update the apt cache and install the latest docker.

sudo apt-get update

Install latest version of docker

Install the latest docker version using the following command –

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

Install specific version of docker

Or, install any specific docker version; please mention the version number in the package name.

To list the available versions of docker, run the following command in the terminal –

sudo apt-cache madison docker-ce
List docker versions install docker on ubuntu
List docker versions
sudo apt-get install docker-ce=(docker_version) docker-ce-cli=(docker_version) containerd.io

Here is the example command for installing docker 19.03.12~3-0~ubuntu-focal.

sudo apt-get install docker-ce=19.03.12~3-0~ubuntu-focal docker-ce-cli=19.03.12~3-0~ubuntu-focal containerd.io

Run docker container

That’s it. Docker is now installed. You can verify it by creating a Ubuntu container using docker run command –

sudo docker run -it --name=Ubuntu20 ubuntu:latest

Fix docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

If you run into the above error, the docker service is not running on your system. To fix it, run the following command to start docker service –

sudo service docker start

or

sudo systemctl start docker

Now run the command to create a docker container. It should work this time.

Create docker container on Ubuntu
Create docker container on Ubuntu

Conclusion

So that was it. This is how you can install docker on Ubuntu. It is important to learn docker containers as it makes the development process easier and quicker for developers. Dockers are lighter, so we can run more containers on a system than virtual machines. To know more about how Docker containers work, please read this article covered on LinuxAndUbuntu.

DockerUncategorizedhow-to