How To Install Docker on Ubuntu 18.04

Install Docker on Ubuntu 18.04

Docker is an application that manages the deployable application code in containers. As we discuss in the last post, containers run the application in a resource-isolated process.

By using Docker, we can build, test and deploy the application that runs anywhere as portable containers. These containers only hold the configuration for an environment instead of actual applications or libraries which are required for applications to be functional.

Docker can be installed on any operating system which supports virtualization such as Linux, Windows, and Mac. There are different flavors of Linux available in the market like Ubuntu, Centos, Debian, Red Hat, and many others.

In this post, we will look at how we can install Docker on Ubuntu operating system.

How To Install Docker on Ubuntu 18.04

Before we starting installing docker on Ubuntu, you must have the root user access for the system.

Prerequisites

To install Docker on any operating system there are some requirements. To use Docker you must:

  • Be running a 64 bit architecture (x86_64 or amd64)
  • Be running a Linux 3.10 or later kernel.
  • The kernel should support appropriate storage driver e.g. Device Mapper, AUFS, vfs, ZFS and so on.
  • cgroups and namespaces features must be supported and are enabled.

Install Docker on Ubuntu

In this post, we will install Docker on Ubuntu form Latest Docker’s Repository. Follow the simple steps below to install Docker on your Ubuntu machine.

Firstly, we need to update the package manager index and upgrade the system by running the following command on the command-line terminal using a root user (or with sudo permissions).

sudo apt update && sudo apt upgrade

Next, we need to install the dependencies required to enable the Docker Repository on Ubuntu OS ( Operating System ).

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

Now we will import the GPG key for repository using following curl command:

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

Finally, add the repository to your Ubuntu system by running the following command:

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

Lastly, we will update the package manager index of our Ubuntu Operating System using the following command.

sudo apt update

At this point, we have successfully added the Docker repository to our Ubuntu machine. Now we will install the Docker on our machine.

Run the below command in the command line terminal to install Docker Community Edition.

sudo apt install docker-ce

The above command will install the Docker CE on your machine and now you are ready to use Docker. Welcome On Board

We will confirm the Docker installation by running the below command which will output the currently installed version of Docker.

docker -v

The output should be:

$ docker -v
Docker version 19.03.1, build 74b1e89e8a

Or for more detailed information run the below command.

docker info

You can check the Docker service status using the following systemctl command:

sudo systemctl status docker

The output should be:

● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-10-14 20:22:00 PDT; 6min ago
 Docs: https://docs.docker.com
Main PID: 10647 (dockerd)
Tasks: 21
CGroup: /system.slice/docker.service

Executing the Docker Command Without Sudo

Docker commands can only be run by a sudo user or a memeber of docker gorup which created while installing Docker on your machine.

If you want to run Docker commmands without prefixing with sudo then you have to add your user to the docker group.

If you are already logged in then run the below command to add the current user to docker group.

sudo usermod -aG docker $USER

To apply the membership run below command or you can log out and log in again:

su - $USER

To confirm, you have added to docker group run following command:

id -nG

The output should be:

dockerjet sudo docker

How to Use Docker Commands

There are many commands ( we will cover that in the upcoming post )which Docker offers to use Docker more efficiently. Following is the basic syntax for a Docker command.

docker [option] [sub-command] [arguments]

You can list all commands of Docker by running below:

docker

If you want to get some help of information about a specific Docker command, add a --help argument. For example, we want to know what does docker images command for, we will run like below:

docker images --help

The output will be something like below:

$ docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

What are Docker Images?

Docker containers are based on an image. Images are the underlying definition of what gets reconstituted into a running container, much like our virtual disk becomes a virtual machine when we start it up.

Docker images provide the basis for everything that you will ever deploy and run with Docker. To launch a container, you must either download a public image from Docker Public Image Repository like Docker Hub or create your own (In future post will discuss how we can create our own images).

Docker Hub

On the Docker Hub, anyone can host their images, because of this most of the app images and Linux distro’s are already available on Docker Hub.

Searching for Docker Images

You can search for a Docker image on the Docker Hub repository using the following command.

docker search ubuntu

The output should be:

$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   10042               [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   352                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   231                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   188                                     [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   100                 [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                                      [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   59                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   40                  [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                                      [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   18
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                                      [OK]
ppc64le/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   13
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                                      [OK]
1and1internet/ubuntu-16-nginx-php-5.6                     ubuntu-16-nginx-php-5.6                         8                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                                       [OK]
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   2
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                                       [OK]
smartentry/ubuntu                                         ubuntu with smartentry                          1                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              0

You can see all images come with version number still if the version number is not specified at that time docker picks the latest version available.

Download Docker Image

We can pull/download docker image from Docker repository using the docker pull command. To download the Ubuntu image, we will run the below command.

docker pull ubuntu

The output will be:

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
Digest: sha256:b88f8848e9a1a4e4558ba7cfc4acc5879e1d0e7ac06401409062ad2627e6fb58
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

Remove Docker Image

If you want to remove an image from your system, you can use the docker image rm command. The following command will remove the Ubuntu image from your system.

docker image rm ubuntu

Using Docker Containers

Docker container is an instance of the Docker image. In the previous section, we pulled the Ubuntu image from Docker Repository, we can start a Ubuntu container from the Ubuntu image. Containers are resource-friendly virtual machines.

To manage Docker container, we use the docker container command.

Starting a Docker Container

To run/start a container, we can use the below command:

docker container run ubuntu

Note that ubuntu in our above command is the image name we pulled from the Docker repository. If the container image is not available on your machine, Docker will first download the image and then run it.

To interact with the Ubuntu container after booting up you have to use the argument -it like below:

docker container run -it ubuntu /bin/bash

The output should be:

$ docker container run -it ubuntu /bin/bash
[email protected]:/#

Listing Active Docker Containers

You can list all active containers using the below command. It will list all the active containers otherwise the output will be empty:

docker container ls

If you want to list all Active and InActive containers type:

docker container ls -a

Removing a Docker Container

If you want to remove a Docker container, use the below command.

docker container rm 2ca708c1c9cc

In the above command, 2ca708c1c9cc is the container ID.

Conclusion

In this post, we have successfully installed the Docker on Ubuntu operating system. In the coming posts, we will install on other distros of Linux OS and also on Windows.

If you have any comments or suggestions about this post, please let us know in the comments box below.

Leave a Reply

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

*

*
*