# Docker

## Problem

* Traditionally, installation of software requires a lot of steps namely,
  1. Downloading the software package/installer for your specific platform.
  2. Run the package/installer.
  3. Troubleshoot issues if any like missing dependencies _(i.e download all the dependencies)_, dependency version conflicts etc rerun the installer and go through the loop untill all the issues are resolved.
  4. Finally after all the issues are resolved may be the software might be running in your specific environment.
  5. Want to install in a different machine with different configuration? Start from step 1 again and again go through painstaking troubleshooting process, just to get the software installed.

## Solution

* This is where docker comes to rescue.
* With Docker, installation and managing software becomes breeze.
* Just install docker for your machine and use the commandline/docker file/docker-compose to spin up the [container](#terminology) for your software.
* No environment specific setup would be needed from hence forth to install softwares, as docker images handles them for you.

## About

* Docker is an open platform for developing, shipping, and running applications.
* It was first released in 2013 and is developed by `Docker, Inc`.
* Docker is a tool that is used to automate the deployment of applications in lightweight containers so that applications can work efficiently in different environments in isolation.
* Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

### Terminology

| Term      | Description                                                                                                                                                                                                                               |
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Image     | A single file which consists of all the dependencies and configuration required to run a program, read more [here](https://docs.docker.com/get-started/docker-concepts/the-basics/what-is-an-image/). Images are immutable, once created. |
| Container | Instance of an image, basically a running program with isolated hardware resources _(grouping of resources)_, read more [here](https://docs.docker.com/get-started/docker-concepts/the-basics/what-is-a-container/).                      |
| Namespace | A feature of the Linux kernel that partition kernel resources such that one set of processes sees one set of resources, while another set of processes sees a different set of resources                                                  |
| cgroup    | A Linux kernel feature that limits, accounts for, and isolates the resource usage _(CPU, memory, disk I/O, etc)_ of a collection of processes                                                                                             |

### Docker Platform

![Docker Components](https://574639531-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJDnjsv52I3fJ56WbLgHu%2Fuploads%2Fgit-blob-eb1405107729add0766d526b531e9e2b296f0673%2FDocker_Components.png?alt=media)

* Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.
* Docker provides the ability to package and run an application in a loosely isolated environment called a container.
* The isolation and security lets you run many containers simultaneously on a given host.
* **Containers** are lightweight and contain everything needed to run the application, so you don't need to rely on what's installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.

#### Components

| Component                | Description                                                                                      |
| ------------------------ | ------------------------------------------------------------------------------------------------ |
| Docker Client            | Tool that we are going to use to issue the command, which will be executed by Docker Server      |
| Docker Server _(Daemon)_ | Tool that runs behind the scene, which is responsible for creating images, running container etc |

### Docker Working

* When you run a `docker run` command the following are the series of steps that ensue,
  1. Docker cli processs the command and sends the command to docker server/daemon running in the machine.
  2. Docker Server looks for the image in the image cache.
  3. If the image is found in the cache it executes the image and spin up a container.
  4. If the image is not found, it communicates to the docker hub _(a public repositories with docker images)_ and looks for the image with version specified or latest version if no version is specified and downloads them and puts them in its local cache and executes them.

#### Docker Setup in non-linux machine

![Docker Setup in non linux machine](https://574639531-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJDnjsv52I3fJ56WbLgHu%2Fuploads%2Fgit-blob-7b4b46cdc63f15ed9530e1e7824aac127caf3cc3%2Fdocker-non-linux-setup.png?alt=media)

### Port Mapping

* An application running within a container cannot access ports of the host in which it has the docker container running.
* Container basically has its own isolated set of ports.
* So, when any **incoming traffic** tries to access a port of the container it will be blocked as the container runs in an isolated environment.
* To solve this problem we use the **port mapping** functionality of the docker, which will basically forward the traffic received at the host to port of the container.
* Outgoing traffic from the container to the outside world is allowed and need no extra configuration.

## References

* [Container v/s VM](https://www.docker.com/resources/what-container/)
* [Docker Training](https://docs.docker.com/get-started/)
* [Guides](https://docs.docker.com/guides/)
* [LXC v/s Docker](https://www.docker.com/blog/lxc-vs-docker/)
