Docker

How to Install and Use Docker on Ubuntu 18.04 LTS

To do a full system virtualization, a lot of memory and disk space is needed just as we need to run an operating system in our computer. Docker is a containerization system. A Docker virtual machine is called a container. How that works is, Docker uses the kernel of the host operating system and uses the Linux kernel’s namespacing features to isolate the containers. So a Docker container do not need a kernel installed and many other dependencies. That makes them lighter and faster. The only downside is that, a Docker container can’t have a different kernel than it’s host operating system. If you want to run a different kernel than the host operating system, you must use full virtualization, not containerization.

In this article, I will show you how to install and use Docker on Ubuntu 18.04 LTS.

Adding the Docker Package Repository:

In this section, I will show you how to add Docker package repository on Ubuntu 18.04 LTS.

First update the APT package repository cache of your Ubuntu 18.04 LTS machine with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now run the following command to install some additional packages required to add Docker package repository on Ubuntu 18.04 LTS:

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

Now press y and then press <Enter> to continue.

The required packages should be installed.

Now add the official GPG key of the Docker repository on your Ubuntu 18.04 LTS machine with the following command:

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

The GPG key should be added.

Now add the official package repository of Docker with the following command:

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

The official Docker package repository should be added and the APT package repository cache should be updated as well.

Installing Docker on Ubuntu 18.04 LTS:

Now that everything is ready, you can install Docker on Ubuntu 18.04 LTS with the following command:

$ sudo apt install docker-ce

Now press y and then press <Enter> to continue.

All the required packages should be downloaded and installed.

Docker is installed.

Now check whether Docker service is running with the following command:

$ sudo systemctl status docker

As you can see, the Docker service is running.

If it is not running, then start Docker with the following command:

$ sudo systemctl start docker

Now should add Docker service to the system startup so that it will start automatically on system boot.

Run the following command to add Docker service to the system startup:

$ sudo systemctl enable docker

Now check whether Docker is working with the following command:

$ docker -v

Running Docker without Root Privileges:

By default, you must run Docker as root user. You can also run Docker with sudo if it is configured on your Ubuntu 18.04 LTS machine. If you don’t want to use Docker as root user or with sudo, then you should add your login user to the docker group.

Add your login user to the docker group with the following command:

$ sudo usermod -aG docker $(whoami)

Now reboot your computer with the following command:

$ sudo reboot

Now you should be able to run Docker without sudo or root user.

Searching for Docker Images:

Docker image repository has images for almost anything you need. For example, if you want a php server, you can install a php server image and you will be ready to run php scripts.

You can search for a Docker image, let’s say a Docker image for Node.js, in the official Docker image repository with the following command:

$ docker search node

As you can see, all the Docker images for Node.js is displayed. To install a Docker image, you just have to type in the image name is the NAME column as marked in the screenshot below.

Downloading a Docker Image:

Now let’s say, you want to download the mhart/alpine-node image of Docker. Downloading a Docker image is called pulling a Docker image in the Docker term.

To pull mhart/alpine-node Docker image, run the following command:

$ docker pull mhart/alpine-node

The Docker image should be pulled.

Listing Downloaded Docker Image:

To list all the Docker images you pulled and is available in your machine, run the following command:

$ docker images

As you can see, the Node.js image I pulled is listed.

Running a Node.js Script with Docker:

In this section, I will show you how to run a Node.js script index.js with the Docker Node.js image mhart/alpine-node that I just pulled from the official Docker image repository.

First create a project directory with the following command:

$ mkdir -p ~/Projects/hello

Now navigate to the project directory with the following command:

$ cd ~/Projects/hello

Now create index.js and open it with nano text editor with the following command:

$ nano index.js

Now type in the following line and save the file.

Now you can run the Node.js script index.js with

$ docker run -it --rm --name hello_node -v "$PWD":/usr/src/app/hello -w /usr/src/app/
hello mhart/alpine-node node index.js

As you can see, the index.js Node.js script ran successfully and the correct output was printed.

Now let’s explain what’s happening here.

  • -it flags are used to attach the current STDIN and STDOUT to docker and run a command which is node index.js
  • –name hello_node – Set’s hello_node as the name of the running container.
  • –rm flag removes any other running container with the same name as this container.
  • -v “$PWD”:/usr/src/app/hello – Mounts the current working directory of your Ubuntu 18.04 LTS machine to the /usr/src/app/hello directory of the Docker container.
  • -w /usr/src/app/hello – Navigate to the /usr/src/app/hello directory in the Docker container.
  • mhart/alpine-node – Name of the Docker image that this container will be based on.
  • node index.js – It is the command that will be run inside the container.

So that’s how you install and use Docker on Ubuntu 18.04 LTS. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.