Docker
Docker
Section titled “Docker”What is Docker?
Section titled “What is Docker?”Docker is a container technology: A tool for creating and managing containers.
Container
Section titled “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.
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
Section titled “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
Section titled “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
Section titled “Virtual Machines”Virtual Machines (2)
Section titled “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
Section titled “Containers”Containers (2)
Section titled “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
Section titled “Install Docker”Check official sources for latest information and requirements: https://docs.docker.com/get-docker/
Using Docker
Section titled “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
Section titled “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
Section titled “One Image Multiple Containers”Build an image using a Dockerfile
Section titled “Build an image using a Dockerfile”# Specify a base imageFROM node:14
# Set the working directory inside the imageWORKDIR /app
# Copy the local file package.json# to the current (working) directory inside the imageCOPY package.json .
# Run a commandRUN npm install
# Copy everything from the current directory of the host# to the current (working) directory inside the imageCOPY . .
# Document which port will be accessible (optional)EXPOSE 3000
# Set the entrypoint of the containerCMD [ "node", "app.mjs" ]Build an image
Section titled “Build an image”Run this command inside the folder that contains your Dockerfile:
docker build .Run a container based on the image
Section titled “Run a container based on the image”docker run <image_name>or
docker run -p <host_port>:<container_port> <image_name>Images are read-only
Section titled “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
Section titled “Multi stage Dockerfiles”FROM mcr.microsoft.com/dotnet/runtime:7.0 AS baseWORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS buildWORKDIR /srcCOPY ["HelloWorld/HelloWorld.csproj", "HelloWorld/"]RUN dotnet restore "HelloWorld/HelloWorld.csproj"COPY . .WORKDIR "/src/HelloWorld"RUN dotnet build "HelloWorld.csproj" -c Release -o /app/build
FROM build AS publishRUN dotnet publish "HelloWorld.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "HelloWorld.dll"]Are most commonly used to differ between build an run environments. build contains the SDK, final only contains the runtime.