Docker Fundamentals
Tags: codenewbie, 100daysofcode, docker, devops
Published: 2020-08-06 09:04:41

## Introduction

Docker is one of the most popular software tools in recent times that has revolutionised the process of software installation and deployment. However, it has been always quite a struggle to explain what Docker is or what problem Docker solves.

In this post, I have covered the answers to the following questions -

- [Why use Docker?](#why-docker)
- [What is Docker?](#what-docker)
- [What is a Docker Container?](#docker-container)
- [What is a Docker Image?](#docker-image)


### Quick Note

**_The entire article is based on the concepts that I had learnt from Stephen Grider's Udemy course. You can find the link for the course [over here.](https://www.udemy.com/course/docker-and-kubernetes-the-complete-guide/learn/)_**

## Why use Docker? <a name="why-docker"></a>

Before we go into what a Docker is, let us try to understand what Docker is trying to solve.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tycx6gkf8uapg00hsimm.png)

The image you see above details the steps involved when a user tries to install a piece of software on a computer. The user encounters some error during the installation process and repeats the steps as shown in the workflow diagram above, iteratively.

Often, when the user tries to reach out to other support groups, he realizes that the installation process and the set of errors encountered varies based on the underlying operating system and the set of libraries that the software is dependent on. And, this is what Docker precisely solves.

Docker provides an environment (container) that allows the user to install and run the software without worrying about the setup or dependencies. In other words, a user installing the software in a Windows, MacOS, or a Linux operating system will experience the same behavior, and hence troubleshooting/debugging will be agnostic of the underlying operating system.

## What is Docker? <a name="what-docker"></a>

Docker is a platform or an ecosystem of different projects, tools, and pieces of software around creating and running containers.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aojpt8fs65jv79iqg4g2.png)

So, what is a Docker Container? In order, to answer this, we would have to first revisit the fundamentals of what an operating system is.

## Fundamentals of an Operating System

The following diagram provides a quick overview of the Linux Operating system.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8sz5iqktg1jka0nfwz3s.png)

As you can see from this diagram, the Kernel is the running software that governs the access between the programs that are running on the computer and the physical hardware that is connected to the computer.

When a user issues a command through the software, e.g. Play a song through Spotify, the command is conveyed by the software program to the Kernel through a system call. Every program has a unique system call to communicate to the Kernel.

The kernel receives the command and communicates with the hard disk/memory to find the song that has to be played by the software.

## Namespacing & Control Groups

From the above example, let us assume a hypothetical scenario where two software programs, say Photoshop and Spotify, require different versions of Python software.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9upyvfp6u3o53plj343o.png)

Having separate Python versions based on the requesting software can be achieved by a concept called namespacing.

### Namespacing

Namespacing is the process of allocating dedicated hardware resources to a single process or a set of processes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j3kxbbgrf33n3e1sdykr.png)

In the above example, the hard disk can be segmented to have a specific version of a python software installed using namespacing.

When a software makes a call to interact with the hard disk, the Kernel determines which process is making the system call and redirects that call to the segment based on the version required for the software to operate. The following illustration explains the same.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zrgm9tsftinto25aihrl.png)

The diagram below describes how namespacing can be used to isolate resources such as network, users, hostnames, etc.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5kxrhaz8fcqeav0jnthb.png)

### Control Groups

Control Groups (cgroups) is used to limit or control the amount of resources available to a particular process or a set of processes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/exkof1ca8xws8wvr8xhv.png)

In the diagram shown above, using control groups, one can limit the memory, CPU, Hard disk I/O, and network bandwidth available for a process.

**Note: The features of namespacing and control groups are specific to the Linux operating system.**

## Docker Container <a name="docker-container"></a>

Now that we have established the concepts of namespacing and control groups, a container can simply be explained as a process or a set of processes that have a grouping of resources specifically assigned to it.

Going back to the same example as above, a container can be depicted as a logical grouping of processes. The resources allocated with the container is shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ozlwhh408e72501o0jmk.png)

So, in essence, a container is a logical construct that has a process or a set of processes with dedicated resources allocated to it. The following visual summarizes the same. Note that a portion of the hard drive, memory, CPU, and network is dedicated to the process inside a container.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c2pwoe3858js7gc0dz3t.png)

Now that we have understood what a docker container is, let us find out what a Docker image is and what is the relation between a docker image and a docker container.

## Docker Image <a name="docker-image"></a>

A Docker image is a file system snapshot that is used to create a container and run any processes defined within.

A Docker image can be visualized as a blueprint and the instance created out of the blueprint becomes the container.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fvyk46rmfvb85tl143vm.png)

The following visual explains the step-by-step process of how exactly an image is turned into a container.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hoafgkznchmcqtfvfe05.png)

**Step 1** - The contents of the file system snapshot, in this case, Photoshop and Python-related files, are copied over to the hard disk.

**Step 2** - The run Photoshop command specified in the file system is executed. This runs the application from inside the hard drive and now you have Photoshop as a running process within the container.

Note that the Photoshop application communicates via the Kernel (that resides outside the container) to the hardware resources allocated to the application.

## Conclusion

I hope this article could help in understanding the fundamentals of Docker, Docker Image, and Docker containers. There are other components within the Docker ecosystem such as Docker client, Docker server, Docker hub that also helps in creating, managing, and deploying Docker containers.

Thanks for reading the article and do not forget to connect with me on Twitter [@skaytech.](https://twitter.com/skay_tech)

If you enjoyed this, you may also enjoy:
- [Git Hooks](https://dev.to/skaytech/what-are-git-hooks-99m)
- [JavaScript - Understanding Closure](https://dev.to/skaytech/understanding-closures-1501)
- [JavaScript - Async/Await Functions](https://dev.to/skaytech/async-functions-chapter-3-async-await-89)
- [JavaScript - Promises](https://dev.to/skaytech/async-functions-chapter-2-promises-1c6i)
- [JavaScript - Higher Order Functions](https://dev.to/skaytech/es6-higher-order-functions-495e)