How to install docker engine on Ubuntu

Hi All, this is a small guide on how to install the docker engine on Ubuntu (and fix the docker socket permission problem).

Please note that I’m running these commands on a freshly installed VM so this might change based on your system so make sure you will have all the proper backup procedures in place.

Let’s start from here (https://docs.docker.com/engine/install/) that will drive you here (https://docs.docker.com/engine/install/ubuntu/)

Let’s follow the steps then:

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

Now let’s setup the repository

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add the GPG key

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

Then setup the stable repository with $(lsb_release -cs) stable

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

And then….install

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

The second command gave me an error (related to my ubuntu version, the same will happen with Linux Mint)

Package docker-ce is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'docker-ce' has no installation candidate
E: Unable to locate package docker-ce-cli
E: Unable to locate package containerd.io
E: Couldn't find any package by glob 'containerd.io'
E: Couldn't find any package by regex 'containerd.io'

To overcome this issue we have to run this command instead

sudo apt-get install docker.io

The last command will install the docker engine and we can verify by typing

$ docker -v
Docker version 20.10.2, build 20.10.2-0ubuntu1~20.04.2

That’s great so far, however there is one last thing we should do because is not working as expected. In fact if we want to list the docker containers, or docker images we will get this error

docker ps: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json: dial unix /var/run/docker.sock: connect: permission denied

docker images: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/json: dial unix /var/run/docker.sock: connect: permission denied

This will not happen if we run the command as sudo

$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

What we need to do for fixing this problem is to:

# register your user to the docker group
sudo usermod -aG docker ${USER}
# logout and login again

And now everything is magically working as expected (there is no magic in computer science…only spells)

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

Of course this is now empty, but in one the next articles we will see how to populate these.

d3

d3 is an experienced Software Engineer/Developer/Architect/Thinker with a demonstrated history of working in the information technology and services industry. Really passionate about technology, programming languages and problem solving. He doesn't like too much the self celebration and prefers to use that time doing something useful ...i.e. coding

You may also like...