Hasan MN
DevLogs

DevLogs

How to Install Docker Engine on Ubuntu

Hasan MN's photo
Hasan MN
·Aug 9, 2021·

2 min read

This article shows us how to install Docker Engine on Ubuntu.

Before we install Docker Engine, we need to set up the Docker Repository as follows:

First, we need to update the apt package index and then install packages to allow apt to use a repository over HTTPS.

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

Then, we need to add the GPG key for the official Docker repository to our system.

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

Next, we add the Docker repository to APT sources.

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

Finally, we can install the latest Docker Engine.

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

To verify that Docker Engine is installed, run the hello-world image as follows.

sudo docker run hello-world

The following result will be shown if the install was success.


Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. 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/

By default, the docker command can only be run by the root user or a user in the docker group. Therefore, if you run docker command without sudo using a non-root user or a user that doesn't belong to docker group, then you will get a perminssion denied error. To avoid this, we can add our current user to the docker group using the following command

sudo usermod -aG docker ${USER}

and apply the new group membership by typing the following command

su - ${USER}

Now, we can run docker run hello-world, without sudo.

It is quite likely that we also need docker-compose. To install it, run the following commands.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now, we can verify that docker-compose is successfully installed by checking its version as follows.

docker-compose --version

If the version is shown then, it is a success.

 
Share this