Skip to content

Docker


Docker is a container technology: A tool for creating and managing containers.


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.


  • 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.

  • 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.

VM.excalidraw.svg


ProCon
Separated environmentsRedundant duplication, waste of space
Environment-specific configurations are possiblePerformance can be slow, boot times can be long
Environment configurations can be shared and reproduced reliablyReproducing on another computer/server is possible but may still be tricky

Containers.excalidraw.svg


Docker ContainersVirtual Machines
Low impact on OS, very fast, minimal disk space usageBigger impact on OS, slower, higher disk space usage
Sharing, re-building and distribution is easySharing, re-building and distribution can be challenging
Encapsulate apps/environments instead of “whole machines”Encapsulate “whole machines” instead of just apps/environments

Check official sources for latest information and requirements: https://docs.docker.com/get-docker/

DockerInstallation.excalidraw.svg


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


ImagesContainers
Templates/Blueprints for containersThe running “unit of software”
Contains code + required tools/runtimesMultiple containers can be created based on one image

ImagesContainers.excalidraw.svg


# Specify a base image
FROM node:14
# Set the working directory inside the image
WORKDIR /app
# Copy the local file package.json
# to the current (working) directory inside the image
COPY package.json .
# Run a command
RUN npm install
# Copy everything from the current directory of the host
# to the current (working) directory inside the image
COPY . .
# Document which port will be accessible (optional)
EXPOSE 3000
# Set the entrypoint of the container
CMD [ "node", "app.mjs" ]

Run this command inside the folder that contains your Dockerfile:

Terminal window
docker build .

Terminal window
docker run <image_name>

or

Terminal window
docker run -p <host_port>:<container_port> <image_name>

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.


FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["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 publish
RUN dotnet publish "HelloWorld.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --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.