How to Install Docker

Continuing our previous blog post, What is Docker, we will now see what it takes to install Docker on Ubuntu 20.04 and test to ensure we can start a container.

Prepare the host

  • Uninstall previous Docker versions if they exist and update the apt repository
sudo apt-get remove docker docker.io containerd runc
sudo apt-get update 

Dependency Installation

Install the required dependencies to get docker running. These packages are:

  • apt-transport-https: allows usage of HTTPS in /etc/apt/sources.list.
  • ca-certificates: contains certificate authorities shipped with Mozilla to check if a certificate is authentic.
  • curl: a tool for transferring data with URL syntax.
  • gnupg: used for secure communication and data storage.
  • lsb-release: a simple tool to help identify Linux distributions
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

Docker Installation

Add Docker GPG key to the keyring

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, set up the Docker repository. Typically, people will use the stable repository. The other two options are nightly or test.

echo
\\\"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu
$(lsb_release -cs) stable\\\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

What did this command do? It took what we had wrapped in quotes and put it in a docker file list in our apt sources. When we issue an apt command, this is one of the sources it will look at.  The key we downloaded in a previous step will authenticate to that repo and, as a result, allow us to grab or update the package.

cat /etc/apt/sources.list.d/docker.list
deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable

Now, update apt-cache so the newly added docker repo can be used.

sudo apt-get update

Let’s Install Docker!

To test to see if Docker is installed correctly by running a container called hello-world to ensure that the install is working as it should

docker run hello-world

Upon running this docker command, we receive an error

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post https://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create: dial unix /var/run/docker.sock: connect: permission denied. See \\\'docker run --help\\\'.

The first step in troubleshooting is to ensure that Docker is running

sudo systemctl status docker | grep Active
Active: active (running) since Tue 2021-05-18 21:37:52 EDT; 14min ago

It is.  The root user owns the Unix socket for Docker. This is why you have to run with sudo. It is not a TCP socket. 

mike@docker:~# sudo docker run hello-world
Unable to find image \\\'hello-world:latest\\\' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the \\\"hello-world\\\" image from the Docker Hub.
(amd64)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

There is a way to run docker as a regular non-root user.

  • Create a group called docker
  • Add your user to the group
  • Log out and log back in again
sudo groupadd docker 
sudo usermod -aG docker $USER

After a logout, we should be able to rerun hello-world.

mike@docker:~# docker run hello-world
Unable to find image \\\'hello-world:latest\\\' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the \\\"hello-world\\\" image from the Docker Hub.
(amd64)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/