13 Working with containers

 

This chapter covers

  • Why to use containers
  • What a container image is and how to build one
  • How to set and read configuration values
  • How to use secrets

Containers have become increasingly prevalent in software development. The core concept boils down to providing an isolated and portable environment to run software. One of the difficulties of developing software is the fact that things run differently depending on the machine. The OS provides access to all kinds of resources: network ports, filesystem, environment variables, other processes, kernel APIs, and so on. These resources may change from one machine to another (which is where the infamous line “It works on my machine” comes from).

13.1 Why use containers?

Machine-specific settings lead to diagnosing problems such as not having a certain dependency installed, having network ports already in use, or needing environment variables for a process that hamper other processes. These problems can take a lot of time to figure out—time that developers would rather spend writing software. By writing software that runs in a container, a developer guarantees that resources and dependencies are specified clearly so that the host OS can provide them. Containers can also be built on top of other container images.

13.2 Container landscape

13.3 Building a container image

13.3.1 Using .NET 6 and earlier or custom Dockerfile

13.3.2 Using .NET 7 and later

13.4 Configuration

13.4.1 Controlling the .NET environment

13.4.2 Configuration order

13.4.3 Hierarchical configuration

13.4.4 Applying configuration in Docker Desktop

13.4.5 Mapping configuration to objects

13.5 Secrets

Summary