# Docker
# What is Docker?
Docker is a container technology: A tool for creating and managing containers. With Docker always imagine using Linux.
# Docker Engine
Is the technology behind docker. Is a software, which makes it possible to use kernel-operations for creating containers. It also allows, that every container is seperated from others.
# Container
A container is a standardized unit of software.
A container encapsulates code alongside with its dependencies that are needed to run the code.
For example a C# Application and the .NET Runtime.
# Why?
Containers solve the problem of “it runs on my machine”. This happens when you develop an application, test it on your local environment (your development machine), deploy it to production and then something breaks, because some part of the environment is different than on your local machine.
We want to build and test in exactly (!) the same environment as we later run our app in.
# Containers
- The same container always yields the exact same application and execution behavior! No matter where or by whom it might be executed.
- Support for Containers is built into modern operating systems.
- Docker simplifies the creation and management of containers.
# Reliability and Reproducible Environments
- We want to have the exact same environment for development and production →\rightarrow→ This ensures that it works exactly as tested.
- It should be easy to share a common development environment with (new) colleagues.
# Virtual Machines
# Virtual Machines (2)
Pro | Con |
---|---|
Separated environments | Redundant duplication, waste of space |
Environment-specific configurations are possible | Performance can be slow, boot times can be long |
Environment configurations can be shared and reproduced reliably | Reproducing on another computer/server is possible but may still be tricky |
# Containers
# Containers (2)
Docker Containers | Virtual Machines |
---|---|
Low impact on OS, very fast, minimal disk space usage | Bigger impact on OS, slower, higher disk space usage |
Sharing, re-building and distribution is easy | Sharing, re-building and distribution can be challenging |
Encapsulate apps/environments instead of “whole machines” | Encapsulate “whole machines” instead of just apps/environments |
# Install Docker
Check official sources for latest information and requirements: https://docs.docker.com/get-docker/
# Using Docker
There are several ways to interact with docker. Most of the features nowadays can conveniently be accessed through Docker Desktop.
However, we will focus on using the command-line interface (CLI) to better see and understand what is going on.
When running docker commands in the CLI always make sure that Docker Desktop is running.
Commands: https://docs.docker.com/get-started/docker_cheatsheet.pdf
# Images vs Containers
Images | Containers |
---|---|
Templates/Blueprints for containers | The running “unit of software” |
Contains code + required tools/runtimes | Multiple containers can be created based on one image |
# One Image Multiple Containers
# Build an image using a Dockerfile
The file itself is only called dockerfile
.
Note: Every command in a dockerfile creates a new layer. Docker saves these layers in the cache. When a layer does not change docker reuses this layer from the cache.
# Build an image
Run this command inside the folder that contains your Dockerfile:
enter a name for image (we can also enter a tag/“latest”)
# Run a container based on the image
or
# Images are read-only
After building an image, changes to the source code files won’t be reflected inside the image. You have to build the image again in order to make changes to the image.
# Multi stage Dockerfiles
# .NET build & run
SDK - Tools for creating software application.
- ”Software Development Kit”
- Creates .dll, .exe
Runtime - takes output (.ddl, .exe) & runs them.
With this method we don’t need a SDK:
Are most commonly used to differ between build an run environments. build
contains the SDK, final
only contains the runtime.
# Inspect Image
Container-Name basically has a Hash-value. Hash from filesystem.
- Hash does some mathematical operations
- Hash is one-line - can’t be reconverted
# .Dockerignore
For ignoring things in a dockerfile. Same as .gitignore
# Interpreter vs Compiler
Interpreter: JavaScript
- has node:14, … Compiler: C#
- compiles files - generates .exe, .dll