# docker

In a nutshell, docker or containers are used to run programs in an isolated environment that contains all required dependencies like software, libraries, configuration files, etc.

<img src="https://1468393421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M03x0eNy3rGD4mNtwvZ%2Fuploads%2FqDLtHOVtMP1wsGEyT1Hg%2Ffile.excalidraw.svg?alt=media&#x26;token=7eba5951-0a0d-4add-be4e-13dea04fc8f6" alt="" class="gitbook-drawing">

Containers are nowadays the generally accepted way to bundle, ship, and run software, especially in the server/cloud world. It is a widely accepted standardization between developers, server admins, DevOps, and cloud providers.

It more or less solves the *"it worked on my computer..."* problem. You run the container on your machine and it contains all the necessary files and dependencies to start it (however, you still need to read the documentation).

{% hint style="info" %}
Docker is also the name of the company Docker Inc. The term is closely related (and mostly used interchangeably) to containerization. Therefore "docker" in this context will be related solely to the term containerization, not to the company. If the company is meant, it will be explicitly mentioned.&#x20;
{% endhint %}

***

> There is a bit more to containers than this simple explanation. In this write down, abstraction is used  explain the topic step by step. Page by page, the high level abstraction will be revealed, therefore it can happen that some statement are not completly true. High level abstraction will be marked with an asterix (\*) in the text, drawings or images.

***

## Lightweight virtualization

Containers are often compared to virtual machines (VMs) or virtualization in general. They are similar in context. Containers are more "lightweight" in comparison.

<img src="https://1468393421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M03x0eNy3rGD4mNtwvZ%2Fuploads%2FlvSFlr79v3ZTbnoQbowM%2Ffile.excalidraw.svg?alt=media&#x26;token=c9d297e2-1678-42d1-8d02-d367aa2a5916" alt="virtual machines vs containers" class="gitbook-drawing">

The key takeaway is, that for virtual machines there is more or less one additional operating system (OS) in between. Each virtual machine has its dedicated operating system inside. This means the application first communicates with the VM operating system, which then communicates with the host OS or hypervisor which then executes the instruction on the hardware.

Containers communicate in an isolated environment directly with the Host OS. Even if the container itself has a different OS inside, this OS is just an abstraction that talks directly with the Host OS.

For details about containers vs virtual machines see the article from Microsoft: <https://learn.microsoft.com/en-us/virtualization/windowscontainers/about/containers-vs-vm>.&#x20;

## Key components

Containers have several components.

<img src="https://1468393421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M03x0eNy3rGD4mNtwvZ%2Fuploads%2FFzYH39vh7oamwFsEJscs%2Ffile.excalidraw.svg?alt=media&#x26;token=0f3e8aa0-0aae-4022-b614-a56fc8aa1f3d" alt="Components and interation between components" class="gitbook-drawing">

### Dockerfile

The Dockerfile is a file that contains all instructions to generate an [#image](#image "mention"). The instructions contain all details about the prerequisites, requirements, and dependencies.

As an example for a python application, the Dockerfile contains:

* instructions for the required base image i.e. OS such as alpine linux
* instructions to install the required software i.e. python 3.11
* instructions to install the dependencies i.e. the python requirements (pip install ...)
* instructions to copy the source code (or build source code) into the Image

Often it is not necessary to write your own Dockerfile, most applications already ship as pre-build docker image. If you want to ship your application, most of the time you can already build on top of pre-build images of your programming language of choice. The most know container image registry is [hub.docker.com](https://hub.docker.com/), where you can find a lot of pre-build images.

For more detailed information about Dockerfiles and how to write them, check out the page:

{% content-ref url="docker/dockerfile" %}
[dockerfile](https://pascalgolla.gitbook.io/knowledge/docker/dockerfile)
{% endcontent-ref %}

### Image

The container image is a pre-build bundle (generated from a [#dockerfile](#dockerfile "mention")).

After the build process of the [#dockerfile](#dockerfile "mention"), the image contains all files, dependencies, libraries, etc. to run the application. It is more or less a big archive (i.e. zip file) with all the necessary files to start a [#container](#container "mention").

<img src="https://1468393421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M03x0eNy3rGD4mNtwvZ%2Fuploads%2FNfoTklkpGpZodwEpFXmO%2Ffile.excalidraw.svg?alt=media&#x26;token=0ebc2649-2ace-47b1-9771-20170eeacd30" alt="image bundles config files, software, dependencies into a big archive" class="gitbook-drawing">

{% content-ref url="docker/image" %}
[image](https://pascalgolla.gitbook.io/knowledge/docker/image)
{% endcontent-ref %}

### Container

The container is a confined environment to run an application. It can be compared to executables, however the container wraps all content of the executable in its sandbox\*. A container always runs a specific [#image](#image "mention")which defines its use-case/application.

The idea behind the container comes from the cloud/server world. Here, it is important to easily start up multiple applications (containers) at once during high demand (and also stop containers when the load decreases).

By default containers are stateless, meaning if the container i.e. stops due to a crash, all generated data inside the container is also lost. If the data inside a container shall be persistet after a container shutdown, a [#volume](#volume "mention")is required.

{% content-ref url="docker/container" %}
[container](https://pascalgolla.gitbook.io/knowledge/docker/container)
{% endcontent-ref %}

### Volume

{% hint style="info" %}
This section is tbd.
{% endhint %}

{% content-ref url="docker/volumes" %}
[volumes](https://pascalgolla.gitbook.io/knowledge/docker/volumes)
{% endcontent-ref %}
