Docker Tutorial – How to Install Docker on Ubuntu?

We offer you a brighter future with FREE online courses - Start Now!!

In this article, you will learn what a docker is, the architecture of Docker, how to install it on ubuntu, run it and so many more topics. Let’s start!!!

What is Docker?

Let us start with the obvious question: what is Docker? Docker is a platform for building running and shipping applications inconsistent manner. If your application works on your development machine, it can run and function the same way on the other machines.

For someone who has been developing software, they might have probably come across a situation where their application works on the machine theory developed but doesn’t work elsewhere. Why does this happen?

Here are 3 reasons why it happens:

1. One or more files are missing

2. Mismatch of software version

3. Different configuration settings.

This is exactly where Docker comes to the rescue. With Docker, we can easily package up our application with everything it needs and run it anywhere on a machine with docker.

For example, if your application requires a specific version of Node and MongoDB, all of these will be included in your applications package. Now you can take this package and run it on any machine that runs docker. So if it works on your development machine, it is guaranteed to run on any other machine.

Without Docker, as we work on different projects, our development machine gets cluttered with so many libraries and tools that are used by different applications. Then after a while, we don’t know if we can remove one or more of these tools because we are afraid that we will mess up some application.

However, with Docker, we don’t have to worry about this. Each application runs with its dependencies inside an isolated environment, we can safely remove an application with all its dependencies to clean up our machine.

In a nutshell, Docker allows us to consistently build, run and ship our applications.

Need of docker

The docker container virtualizes the operating system. Docker simply is lightweight and used a fraction of the memory compared to booting an entire operating system.

1. Consistent environment

Docker gives developers the ability to create, predictable environments which are isolated from other programs and applications. Docker includes software dependencies needed by the application.

2. Flexible

Docker is very flexible and can be run on any platform or operating systems like MAC, Windows, Linux, virtual machines, production machines, cloud, developer machines and so many more.

Architecture of Docker

Let us talk about the architecture of Docker so you understand how it works. Docker uses a client-server architecture so it has a client component that talks to a server component using a restful API. The server is also called the docker engine, sits in the background, and takes care of building, and running docker containers.

Technically, a container is just a process like other processes running on your computer, but it’s a special kind of process we will talk about.

Unlike Virtual machines, containers don’t carry a full-blown operating system. Instead, all containers share the kernel of the host.

Now “what is a kernel?”, you may ask. Well, a kernel is the sole heart of an operating system, much like the engine of the car. It is the part that manages all applications and hardware resources like memory and CPU.

Every operating system has a different kernel and each kernel has a different API. That is why you cannot run a windows application on Linux.

However, we can run Linux containers in windows, because windows 10 is now shipped with a custom-built Linux kernel.

Layers of Docker container

The major difference between the container and an image is the top writable container layer. When a container is deleted the top layer is deleted, but the image remains unchanged.

You must ensure that all the changes are stored in this container layer. Multiple containers can share access to the same underlying image as each container layer has its own writable container layer.

To manage the image layers and the writable container layer, Docker uses storage drivers. These drivers use stackable image layers and the copy-on-write strategy.

Disk size of Docker container

To view the approximate size of a running container, we can use the docker ps -s command. This command displays 2 columns of information called size and virtual size.

Size: Size refers to the amount of the data on the disk, which we use for the writable layer of each container.

Virtual size: Virtual size refers to the amount of data that containers used for the read-only image data.

All of the running docker containers use the disk space of the computer. This total disk space is the sum of each of the containers’ size and virtual size.

Lifecycle of a Docker container

Here is the journey of a Docker container creation to execution, no pun intended!

  • In the beginning, the Docker container will be in the created state.
  • Then, when we use the Docker run command, it goes into the running state.
  • We use the Docker kill command to kill an existing container.
  • We use the pause command to pause an already existing and running container.
  • To put a Docker container to a running state, from a stopped state, we use the Docker run command.

How to install Docker in Ubuntu?

Docker can be installed on Windows, macOS, and Linux, and its distros like Debian, Ubuntu, CentOS, Fedora, and many more. Let us see how to install Docker in Ubuntu.

Step 1

Before you even install Docker, check if your operating system is all updated and up to date by running the command:

sudo apt-get update

Step 2

Once all the updates have been processed, install Docker by using the command:

sudo apt-get install -y docker.io

Step 3

After the Docker packages are installed, we should receive an output message saying that the Docker process has started and is running.

That’s it, you have installed Docker!

Further steps

After installing Docker, check the version you have installed by typing the command:

docker --version

If you wish to check if docker is running or enabled, you can do so by typing the command:

sudo systemctl status docker

If at all your docker is inactive and not running, you can activate it by typing the command:

sudo systemctl enable --now docker

Development workflow while using Docker

Now that you have installed Docker, let us familiarize ourselves with some of the terminologies and development workflow in Docker. To start off, let us take an application (any application) and dockerize it, which means we mack a small change so that it can be run by docker.

HOW? We just add a docker file to it.

A Docker file is a plain text file that includes instructions that docker uses to package up this application into an image. This image contains everything our application needs to run. It typically contains:

  • A cut down OS
  • A run time environment (eg. python)
  • Application files
  • Third-party libraries
  • Environment variables and many more

In a nutshell, we create a docker file and give it to Docker for packaging our application into an image.

Once we have an image we tell docker to start a container using that image. so a container as I told you is just a process but it’s a special kind of process because it has its own file system which is provided by the image. So our application gets loaded inside a container or a process and this is how we run our application locally on our development machine.

So instead of directly launching the application and running it inside a typical process, we tell docker to run it inside a container – an isolated environment.

Now here’s the beauty of docker once we have this image we can push it to a Docker registry like Docker hub. Docker hub to Docker is like GitHub to Git it’s a storage for docker images that anyone can use.

once our application image is on the Docker hub then we can put it on any machines running Docker, because this machine has the same image we have on our development machine. It contains a specific version of our application with everything it needs so we can start our application the same way we started it on our development machine. We just tell docker to create a container using this image.

so with Docker, we no longer need to maintain long complex release documents that have to be precisely followed. all the instructions for building an image of an application are written in a docker file, with that we can package up our application into an image and run it virtually anywhere.

Docker in action

Let us say there is a hello docker file on our PC. using Visual Studio Code as an editor (you can use any editor). In the hello-docker file, say I created a file named app.js, where I type only one line of java code: console.log (“Hello Docker”);, this code prints “Hello Docker” in the terminal.

Now let us say this is my application (printing “Hello Docker” in the terminal). Now, I want to dockerize it, in other words, we want to build, run and ship it with Docker. Here is the set of instructions for deploying this program:

1. we need an operating system

2. Install a node (which is the execution environment for javascript code)

3. Copy application files

4. Run ‘node appl.js’

We can write these instructions inside a docker file and let Docker package up our application.

Now back to VS Code, we are going to add another file to the project called ‘Dockerfile’ (no extensions needed). I fit is your first time creating a Docker file in VS Code, it automatically asks you to download the recommended extensions (you can go ahead with that). Now in the VS Code, we start writing our instructions to package our application:

The first line of code creates an operating system (alpine is a small distro of Linux). The second line copies all the files into the ‘app’ directory. The third line states for it to go to the app directory, and the last line executes our application with a single java code.

Now we go to the terminal and tell Docker to package up our application by running the command: docker build -t hello-docker .

To see all the images on the computer we type the command:
“docker image ls”, since it is our first time we get only the package we created now (the hello-docker image)

The reason we used the alpine distro is because it is very small so it takes less time to transfer between computers.

Now to run the image, you can simply run the command:

docker run hello-docker

I can publish this image in the docker hub so that anyone can use it. Then I can go on another machine (test or production) and pull and run this image.

Docker container commands

1. Docker run command

docker run -i -t ubuntu /bin/bash

The above command runs a docker command

2. Docker top command

docker top <containerID>

The above command shows you the top processes within a container.

3. Docker stop command

docker stop <containerID>

The above command stops an already running Docker container

4. Docker remove command

docker rm <containerID>

The above command deletes an existing container.

5. Docker statistics command

docker stats <containerID>

The above command shows the statistics of a running Docker container.

6. Docker attach command

docker attach <containerID>

The above command attaches a running Docker container

7. Docker pause command

docker pause <containerID>

The above command pauses the process of a running container

8. Docker unpause command

docker unpause <containerID>

The above command unpauses the processes in a running container.

9. Docker kill command

docker kill <containerID>

This command kills the processes in a running Docker container.

Summary

Docker is a really amazing and helpful tool that helps you build, run and ship applications hassle-free. It is very simple to use yet a powerful tool. By now, you have learned what Docker is, the in-depth architecture of Docker, how to install docker on ubuntu, and a full example of Docker in action.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

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