How to install Docker on Ubuntu 18.04

Hello everyone!
I hope you are doing fine these days. Today I am writing about how to install Docker on Ubuntu 18.04

I am using Ubuntu 18.04 as my daily OS and at the end of last year, I start using Docker for my projects. Within a short time, I like this very much and almost stop using my default local web server environment. With docker and docker-compose I can create a compact project with whatever tools and environment I need for that particular project. I will show you docker-compose later on. This tutorial is Docker installation on Ubuntu 18.04 (Basically this blog is for my future reference). We will install Docker Community Edition (CE) on Ubuntu 18.04 and work with containers and images.

Step-1: Install Docker
Docker is available in the official Ubuntu repository but to get the latest version we will install Docker from the official Docker repository. To do that, we will add a new package source and the GPG key from Docker to ensure the downloads are valid and then install the package.

First, update your existing list of packages,

sudo apt update

Next, install some prerequisite packages which let apt use packages over HTTPS

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

Now add the GPG key for the official Docker

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

And now the Docker repository to APT sources

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

Now update package database for getting information of newly added Docker repo

sudo apt update

Make sure you are about to install from the Docker repo instead of the default Ubuntu repo

apt-cache policy docker-ce

Finally, install Docker

sudo apt install docker-ce

With this command, Docker should be installed and started. Also, the process should be enabled to start on boot. Check if its running,

sudo service docker status

The output should be similar as follows,

docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-01-27 22:57:45 +06; 50min ago
     Docs: https://docs.docker.com
 Main PID: 1849 (dockerd)
    Tasks: 41
   CGroup: /system.slice/docker.service
           ├─1849 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Bingo! You have the docker installed and running on your Ubuntu 18.04 installation. It also enables you to use the docker command-line tool which is called Docker Client.

Step-2: Run docker command without sudo
By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:

Output
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:

sudo usermod -aG docker ${USER}

To apply for the new group membership, log out of the server and back in, or type the following:

su - ${USER}

You will be prompted to enter your user’s password to continue.
Confirm that your user is now added to the docker group by typing:

id -nG

Output
sammy sudo docker

If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:

sudo usermod -aG docker username

I hope now you are able to install and get some idea about docker. We will explore the docker command and dig down more gradually in my next post.

Thanks a lot to stay with me this long!
Have a nice day.

(Visited 140 times, 1 visits today)

Leave a comment

Your email address will not be published. Required fields are marked *