# Docker

Commercial product.

* Evolution of the concept of Solaris Containers and Linux Containers (LXC).
* Released with an OSS license at 2013.

Main concept: Container.

* Build with an application base (disconnected from the infrastructure).
* Composed of multiple functionalities (*namespaces*) that cooperate with each other.
* 1 Container - 1 Application (that can however make *spawn/fork*).

Containers contain all that is necessary to run an application, regardless of the hardware.

* Configurations, dependencies, auxiliary data, etc.

## Concepts

* **Image:** the data of the container (application, libraries, images, etc).
* **Container:** The instance of a **running application**.
  * Composed of one or more *images*, each image adds a functionality.
* **Engine:** Software that executes the *containers*.
* **Registry:** Repository of Docker images.
* **Control Plane:** Infrastructure responsible for managing images and containers.

## A container can run on VMs

<figure><img src="https://2387753953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9sVneYw7epWOEEPKsfCf%2Fuploads%2FZTu6m9LrU4hcIbdglcc3%2Fa.png?alt=media&#x26;token=153751bc-5660-43d6-995e-91e7ee79fd35" alt=""><figcaption></figcaption></figure>

## Architecture

<figure><img src="https://2387753953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9sVneYw7epWOEEPKsfCf%2Fuploads%2FjNQZGfBt2xfi2tkoOd5H%2Fa.png?alt=media&#x26;token=65cb4e7a-7e4a-4b69-a3a5-740d87be4ec3" alt=""><figcaption></figcaption></figure>

## Docker Registry

A central repository that stores and delivers images.

* Indexed by ***name*** and ***tag***.
* Can be private or public (Docker Hub).

Clients can push images to the registry.

* Local client.

Docker Engine creates a pull of the images and executes the container.

* Runs on the server.

Layers are *hashed* and indexed which allows for re-utilization.

* An image pull only needs to download the layers that do not exist locally.

## Workflow

<figure><img src="https://2387753953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9sVneYw7epWOEEPKsfCf%2Fuploads%2FIpzdgDLpinvnxrE2DNbp%2Fa.png?alt=media&#x26;token=f56d6489-cb5a-4e42-ab48-dfffcdee6fa4" alt=""><figcaption></figcaption></figure>

Look for an image.

```bash
$ docker search nginx
```

Image pull.

```bash
$ docker pull nginx
```

List local images.

```bash
$ docker image ls
```

Run a container.

```bash
$ docker run -d -name frontend-server nginx
```

## Dockerfile

A sequence of commands that prepare the container for execution.

* List of dependencies.
* List of commands to execute inside the container.
* Set of commands to execute to initiate the container.

Used locally or distributed in a registry.

Can define versions of the same software.

```docker
FROM debian:stretch-slim

LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"

ENV NGINX_VERSION 1.15.9-1~stretch

ENV NJS_VERSION 1.15.9.0.2.8-1~stretch

RUN set -x \ && apt-get update \ && apt-get install --no-install-
recommends --no-install- suggests -y gnupg1 apt-transport-https ca-
certificates \ && \

...

RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf
/dev/stderr /var/log/nginx/error.log

EXPOSE 80

STOPSIGNAL SIGTERM

CMD ["nginx", "-g", "daemon off;"]
```

## Images

*Containers* have their initial data in images.

* Data, application, libraries.
* Have an *"entrypoint"* that is executed when the container is initiated.

Composed of multiple layers.

* Base image.
* Various images, alter the content (with differences).
* Use *filesystems* by layer (overlayfs, aufs, btrfs, ZFS).

*Read-only* or non-persistent.

* Act as a base from where to initialize the *container*.
* Multiple *containers* can share the same image.

### Layers

<figure><img src="https://2387753953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9sVneYw7epWOEEPKsfCf%2Fuploads%2FiS8E7HNPNSrMgqRCdSTY%2Fa.png?alt=media&#x26;token=363dc2f1-19c0-4261-91d6-cdf5cd1d0206" alt=""><figcaption></figcaption></figure>

It is important to keep images small.

* Do not base images in complete distributions, prefer distros specially designed for *containerization* (ex. Alpine).

<figure><img src="https://2387753953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9sVneYw7epWOEEPKsfCf%2Fuploads%2FjCOKxRUuYJRThc9Z3s6O%2Fa.png?alt=media&#x26;token=1ada55b1-a561-46ac-b67f-bdaf650d7495" alt=""><figcaption></figcaption></figure>

Available in the host.

* */var/lib/Docker/overlay2/\<ID>*
  * */merged*: filesystem view from inside the *container*.
  * */diff*: differences to the base.
