# Introduction

## <mark style="color:red;">Containerization</mark>

Containerization is the process of encapsulating software code along with all of its dependencies inside a single package so that it can be run consistently anywhere.

## <mark style="color:red;">Docker</mark>

Docker is an open source containerization platform. It provides the ability to run applications in an isolated environment known as a container.

Containers are like very lightweight virtual machines that can run directly on our host operating system's kernel without the need of a hypervisor. As a result we can run multiple containers simultaneously.

<figure><img src="https://4247064012-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFLaJdzZGSq1DpczuSySw%2Fuploads%2FFsmFmilt6VQxcc813Pyp%2Fimage.png?alt=media&#x26;token=312e0821-ee09-4752-aad8-e1cf3e82cb4a" alt="" width="563"><figcaption></figcaption></figure>

Each container contains an application along with all of its dependencies and is isolated from the other ones. Developers can exchange these containers as image(s) through a registry and can also deploy directly on servers.

## <mark style="color:red;">Comparing Virtual Machines and Containers</mark>

<figure><img src="https://4247064012-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFLaJdzZGSq1DpczuSySw%2Fuploads%2Fw5Z7OwYLlL1NLmNYLpl2%2Fimage.png?alt=media&#x26;token=31c3d58a-0501-4636-919e-710e6db79de1" alt=""><figcaption></figcaption></figure>

### <mark style="color:yellow;">**Virtual machines**</mark>

* A virtual machine is the emulated equivalent of a physical computer system with their virtual CPU, memory, storage, and operating system.
* A program known as a hypervisor creates and runs virtual machines. The physical computer running a hypervisor is called the host system, while the virtual machines are called guest systems.
* The hypervisor treats resources — like the CPU, memory, and storage — as a pool that can be easily reallocated between the existing guest virtual machines.

<mark style="color:purple;">**There are two types of hypervisors:**</mark>

> **Type 1 Hypervisor** (VMware vSphere, KVM, Microsoft Hyper-V).&#x20;
>
> **Type 2 Hypervisor** (Oracle VM VirtualBox, VMware Workstation Pro/VMware Fusion).

### <mark style="color:yellow;">**Containers**</mark>

* A container is an abstraction at the application layer that packages code and dependencies together.&#x20;
* Instead of virtualizing the entire physical machine, containers virtualize the host operating system only.
* Containers sit on top of the physical machine and its operating system. Each container shares the host operating system kernel and, usually, the binaries and libraries, as well.

| <mark style="color:blue;">**What's Different?**</mark> | <mark style="color:blue;">**VMs**</mark> | <mark style="color:blue;">**Containers**</mark> |
| ------------------------------------------------------ | ---------------------------------------- | ----------------------------------------------- |
| Size                                                   | Heavyweight (GB)                         | Lightweight(MB)                                 |
| Boot Time                                              | Startup time in minutes                  | Startup time in seconds                         |
| Performance                                            | Limited performance                      | Native performance                              |
| OS                                                     | Each VM runs in its own OS               | All containers share the host OS                |
| Runs on                                                | Hardware-level virtualization(Type1)     | OS virtualization                               |
| Memory                                                 | Allocates required memory                | Requires less memory space                      |
| Isolation                                              | Fully isolated and hence more secure     | Process-level isolation, possibly less secure   |

## <mark style="color:red;">Getting Docker set up and running</mark>

### <mark style="color:yellow;">Choosing which Docker product based on requirements</mark>

In a production environment that runs containers hosting critical applications, you would rather have your favorite admins install Docker Enterprise.

However, on your development machine or a continuous integration build machine, you can use the free Docker Engine Community or Docker Desktop depending on your machine type.

| <mark style="color:blue;">**Use**</mark> | <mark style="color:blue;">**Product**</mark> |
| ---------------------------------------- | -------------------------------------------- |
| Developer machine                        | Docker Engine Community or Docker Desktop    |
| Small server, small expectations         | Docker Engine Community                      |
| Serious stuff/Critical applications      | Docker Engine Enterprise or Kubernetes       |

### <mark style="color:yellow;">Installing Docker</mark>

{% tabs %}
{% tab title="Ubuntu" %}

#### OS requirements[🔗](https://docs.docker.com/engine/install/ubuntu/#os-requirements) <a href="#os-requirements" id="os-requirements"></a>

#### <mark style="color:purple;">To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:</mark>

* Ubuntu Focal 20.04 (LTS)
* Ubuntu Bionic 18.04 (LTS)
* Ubuntu Xenial 16.04 (LTS)

Docker Engine is supported on `x86_64` (or `amd64`), `armhf`, and `arm64` architectures.

#### Remove old versions <a href="#uninstall-old-versions" id="uninstall-old-versions"></a>

Older versions of Docker were called `docker`, `docker.io`, or `docker-engine`.&#x20;

#### <mark style="color:purple;">If these are installed, uninstall them:</mark>

```bash
$ sudo apt-get remove docker docker-engine docker.io containerd runc
```

*It’s OK if `apt-get` reports that none of these packages are installed.*

#### Installation methods <a href="#installation-methods" id="installation-methods"></a>

#### <mark style="color:purple;">You can install Docker Engine in different ways, depending on your needs:</mark>

* Most users [set up Docker’s repositories](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository) and install from them, for ease of installation and upgrade tasks. This is the recommended approach.
* Some users download the DEB package and [install it manually](https://docs.docker.com/engine/install/ubuntu/#install-from-a-package) and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.
* In testing and development environments, some users choose to use automated [convenience scripts](https://docs.docker.com/engine/install/ubuntu/#install-using-the-convenience-script) to install Docker.

#### Install using the repository <a href="#install-using-the-repository" id="install-using-the-repository"></a>

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

#### **Set up the repository**

#### <mark style="color:purple;">Update the</mark> <mark style="color:purple;"></mark><mark style="color:purple;">`apt`</mark> <mark style="color:purple;"></mark><mark style="color:purple;">package index and install packages to allow</mark> <mark style="color:purple;"></mark><mark style="color:purple;">`apt`</mark> <mark style="color:purple;"></mark><mark style="color:purple;">to use a repository over HTTPS:</mark>

```bash
$ sudo apt-get update

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

#### <mark style="color:purple;">Add Docker’s official GPG key:</mark>

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

Verify that you now have the key with the fingerprint `9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88`, by searching for the last 8 characters of the fingerprint.

```bash
$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
```

#### **Install docker engine**

#### <mark style="color:purple;">Update the</mark> <mark style="color:purple;"></mark><mark style="color:purple;">`apt`</mark> <mark style="color:purple;"></mark><mark style="color:purple;">package index, and install the</mark> <mark style="color:purple;"></mark>*<mark style="color:purple;">latest version</mark>* <mark style="color:purple;"></mark><mark style="color:purple;">of Docker Engine and containerd, or go to the next step to install a specific version:</mark>

```bash
 $ sudo apt-get update
 $ sudo apt-get install docker-ce docker-ce-cli containerd.io
```

#### <mark style="color:purple;">If you would like to use Docker as a non-root user, you should now consider adding your user to the “docker” group with something like:</mark>

```bash
sudo usermod -aG docker your-user
```

*Remember to log out and back in for this to take effect!*
{% endtab %}
{% endtabs %}

### <mark style="color:yellow;">Hello World in Docker</mark>

#### <mark style="color:purple;">Now that we have Docker ready to go on our machines, it's time for us to run our first container. Open up terminal and run following command:</mark>

```bash
docker run hello-world
```

#### <mark style="color:purple;">If everything goes fine you should see some output like the following:</mark>

```bash
docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
```

To understand what just happened, you need to get familiar with the Docker Architecture, Images and Containers, and Registries.

## <mark style="color:red;">Docker Engine</mark>

#### <mark style="color:purple;">Docker Engine is a client-server application with these major components:</mark>

* A **server** which is a type of long-running program called a daemon process (the dockerd command).
* A **REST API** which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
* A **command line interface (CLI)** client.

<figure><img src="https://4247064012-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFLaJdzZGSq1DpczuSySw%2Fuploads%2FzYg4VzaLT0DCLCic3XWZ%2Fimage.png?alt=media&#x26;token=6c9cf2e6-8f29-40c3-b260-493b4b5db09f" alt=""><figcaption></figcaption></figure>

## <mark style="color:red;">Docker Architecture</mark>

Docker’s architecture is also client-server based. However, it’s a little more complicated than a virtual machine because of the features involved.&#x20;

#### <mark style="color:purple;">It consists of four main parts:</mark>

<figure><img src="https://4247064012-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFLaJdzZGSq1DpczuSySw%2Fuploads%2FO0viTFfnRIRVClPqW1Bj%2Fimage.png?alt=media&#x26;token=3d534f2b-8795-432c-a9f3-c288b6231187" alt="" width="563"><figcaption></figcaption></figure>

1. <mark style="color:purple;">**Docker Client:**</mark> This is how you interact with your containers. Call it the user interface for Docker.
2. <mark style="color:purple;">**Docker Objects:**</mark> These are your main components of Docker: your containers and images. We mentioned already that containers are the placeholders for your software, and can be read and written to. Container images are read-only, and used to create new containers.
3. <mark style="color:purple;">**Docker Daemon:**</mark> A background process responsible for receiving commands and passing them to the containers via command line.
4. <mark style="color:purple;">**Docker Registry:**</mark> Commonly known as Docker Hub, this is where your container images are stored and retrieved.

{% hint style="info" %}
Docker objects are the docker images, containers, volumes, and networks.
{% endhint %}
