Docker
Docker
What is Docker?
Docker is a container technology: A tool for creating and managing containers.
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 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
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
Build an image
Run this command inside the folder that contains your Dockerfile:
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
Are most commonly used to differ between build an run environments. build
contains the SDK, final
only contains the runtime.