Module 1 · Container Foundations · Lesson 1 of 15
Images vs Containers: How Docker Really Works for .NET Developers
A Docker image is a read-only package for an application. A container is an instance of that image; stopping preserves its files, but removing it discards its writable layer.
For a .NET application, this distinction explains why restarting can preserve a scratch file while deploying a replacement loses it. It also explains why downloading a fixed image does not repair an existing container.
By the end, you can predict which files survive, explain how containers run, and make the first checks when an application exits immediately.
What is the difference between a Docker image and a container?
An image packages files and startup settings, such as the default command, environment variables, the working directory, and the user it runs as. For an ASP.NET Core application, those files might include your published application and the .NET runtime.
An image's filesystem is assembled from layers. A layer records a set of filesystem changes, such as adding application files. These image layers are read-only when used by a container.
A container is an instance created from an image. Docker gives it an identity, configuration, and its own writable layer: the place where ordinary file changes made inside that container are recorded. While running, it also has a main process.
Think of an image as a prepared application package. Creating two containers from it gives you two separate places to run that application, not two names for the same running application.
flowchart TD
I["One read-only image"] -->|"provides files to"| A["Container A"]
I -->|"provides files to"| B["Container B"]
A -->|"records changes in"| WA["A's writable layer"]
B -->|"records changes in"| WB["B's writable layer"]
Both containers use the image's files, but each records its own ordinary file changes.
For example, both containers can begin with the same application DLL. If container A writes a scratch file, that does not add the file to the image or to container B.
A stopped container is still a container. It retains its identity and writable layer even though its main process is no longer running. Microsoft's explanation of Docker containers, images, and registries describes the image as the static package from which containers are created.
The mistake to avoid. Do not define a container only as “a running image.” That hides both its separate writable state and the fact that it can exist while stopped.
In an interview, say: “An image is a read-only package of filesystem layers and execution settings. A container is an instance with its own writable layer and, while running, a main process. Many containers can use one image.”
How is a Docker container different from a virtual machine?
The kernel is the part of an operating system that manages processes, memory, and access to hardware. A normal Linux container uses the kernel of the machine it runs on (the host); it does not bring its own kernel.
A virtual machine, or VM, includes the application, its required libraries or binaries, and a full guest operating system. A container does not boot a separate guest kernel for each application.
Linux supplies two important mechanisms for containers:
- Namespaces give processes separate views of resources, such as process lists and network interfaces. These are Linux namespaces, not C# namespaces.
- Control groups, usually called cgroups, account for resource use and enforce configured limits, such as memory or processor limits.
For example, two Linux containers can have separate process views while their processes use the same Linux kernel. Container separation does not mean that every container contains a complete, independently booted operating system.
This avoids the work of booting a guest operating system for each container. The trade-off: all ordinary shared-kernel containers on a host use one kernel, so a kernel bug or a bad kernel setting can affect all of them. VMs each have their own kernel, so they are separated more strongly. Resource limits also need configuration; a container is not automatically assigned a suitable memory budget.
Linux containers need a Linux kernel. On Windows and macOS, Docker Desktop supplies a Linux VM environment for them. They do not run directly against the Windows or macOS kernel. Native Windows containers instead need a compatible Windows execution environment.
Microsoft's What is Docker? explains this host relationship and compares containers with VMs.
The mistake to avoid. A Linux image cannot run directly on the Windows or macOS kernel. It needs a Linux kernel, which Docker Desktop provides through a Linux VM.
In an interview, say: “A normal container shares its execution host's kernel; a VM has a guest kernel. Namespaces isolate resource views, and cgroups account for resources and enforce configured limits. Containers avoid guest boot, but have a different isolation boundary. Docker Desktop uses a Linux VM for Linux containers on Windows and macOS.”
How do Docker commands, registries, tags, and digests fit together?
The Docker CLI is the command-line client you use when typing commands such as docker run. The Docker Engine is the service that manages images and containers. The CLI asks the engine to do the work; the engine may run locally or on another machine.
A registry is a service that stores and distributes images. A repository groups related images within a registry. A tag is a readable label that selects an image, such as 10.0.
Consider this image reference, the full name you give Docker to find an image:
mcr.microsoft.com/dotnet/runtime:10.0
| Part | Meaning |
|---|---|
mcr.microsoft.com | Microsoft's registry address |
dotnet/runtime | The repository |
10.0 | The tag |
To pull an image means to download it from a registry into the engine's local image storage. For example, docker pull mcr.microsoft.com/dotnet/runtime:10.0 downloads the image selected by that tag.
Tags can move to newer content. A digest is a content-based identifier used to select exact image content rather than a movable label. Image references can use the form repository@sha256:..., with the full digest in place of the dots. Pulling by digest selects immutable image content.
This answers a common deployment question: why does the application still have a bug after someone publishes a fix under the same tag?
An existing container keeps the image it was created from. Pulling newer content under that tag does not change the existing container. Starting that container again also does not select the newer image. You need to pull the desired image and create a replacement container from it.
Microsoft regularly updates its .NET images. Your containers do not receive those updated files simply because their original tag now points somewhere else.
The mistake to avoid. Do not treat a tag as a live connection between a container and a registry. It helps select an image; it does not deliver updates into existing containers.
In an interview, say: “The CLI asks the engine to manage containers. Registries store images, repositories group them, and tags are movable labels. Existing containers keep their original image. To apply a fix, pull the desired image and recreate the containers. A digest identifies exact image content.”
What happens to container files after a restart or replacement?
An ordinary file written into a container's writable layer survives stopping and starting that same container. Removing the container discards that layer. A replacement begins with a new writable layer, even if you give it the old name.
Here is a small experiment using an official .NET runtime image. It needs Docker running in Linux-container mode and network access to pull the image. On Windows, Docker Desktop can run either Linux or Windows containers; make sure it is set to Linux containers (the default). Run these commands in your terminal, not in a C# console program. Use the names shown only if they are not already in use.
We override the image's default startup command with sleep, a Linux program that waits. This keeps the containers running while we inspect their files; it is not an example of starting an ASP.NET Core application.
Before running it, predict:
- Will container B see the file written in A?
- Will the file survive stopping and starting A?
- Will a replacement named A have the file?
docker pull mcr.microsoft.com/dotnet/runtime:10.0 docker run -d --name image-demo-a mcr.microsoft.com/dotnet/runtime:10.0 sleep 3600 docker run -d --name image-demo-b mcr.microsoft.com/dotnet/runtime:10.0 sleep 3600 docker exec image-demo-a sh -c 'echo scratch > /tmp/image-demo.txt' docker exec image-demo-a cat /tmp/image-demo.txt docker exec image-demo-b sh -c 'test -f /tmp/image-demo.txt && cat /tmp/image-demo.txt || echo missing' docker stop image-demo-a docker start image-demo-a docker exec image-demo-a cat /tmp/image-demo.txt docker rm -f image-demo-a docker run -d --name image-demo-a mcr.microsoft.com/dotnet/runtime:10.0 sleep 3600 docker exec image-demo-a sh -c 'test -f /tmp/image-demo.txt && cat /tmp/image-demo.txt || echo missing' docker rm -f image-demo-a image-demo-b
docker run creates and starts a new container. Here, -d leaves it running in the background, and --name gives it a convenient name. Docker also assigns a container ID independently of that name.
docker exec starts an additional command inside an already running container. We use sh, a command interpreter, to write and test the file. The path /tmp/image-demo.txt is an ordinary container path in this example; no separate storage is attached there.
Typical file-check output, ignoring download progress, IDs, and command confirmations, is:
| Check | Typical result | Why |
|---|---|---|
| Read in A after writing | scratch | A has the file in its writable layer. |
| Read in B | missing | B has a different writable layer. |
| Read in A after stop/start | scratch | This is still the same container. |
| Read in replacement A | missing | Reusing the name did not retain the old container. |
In this experiment, docker stop takes about 10 seconds. sleep is the container's first process (PID 1), and Linux ignores SIGTERM, the normal request to terminate, sent to that process unless it has set up a handler for it. sleep has none, so it keeps running until Docker sends SIGKILL to forcibly terminate it. A .NET app does handle SIGTERM, so an ASP.NET Core app normally stops sooner.
docker stop stops a container without removing it. docker start starts that existing container again. docker rm removes a container; -f allows removal of a running one by forcibly stopping it first. That makes rm -f convenient for this disposable experiment, not a model for graceful production shutdown.
By contrast, docker rmi (or docker image rm) removes an image, not a container. Docker refuses normal image removal when an existing container still uses that image; forcing removal has additional rules.
Containers are often called ephemeral, meaning you should expect to replace and discard them. That does not mean every stop deletes their files.
For durable data, use storage retained separately from the container or an external service. A volume is storage managed by Docker separately from a container's writable layer. A bind mount makes a selected host path available inside the container. Both are examples of mounts, which attach separate storage at a container path.
Mounted paths have their own lifetime and deletion rules. This experiment proves the behavior of ordinary writable-layer files, not every possible storage arrangement. Continue with Docker volumes and bind mounts before storing uploads or database files.
The mistake to avoid. A container name is not a data-retention mechanism. A replacement called image-demo-a is a new container, not the old one restored.
In an interview, say: “An ordinary writable-layer file survives stop/start of the same container, but is lost when that container is removed. Other containers do not inherit it. Durable data belongs in retained separate storage or an external service; mounted paths have separate rules.”
What happens when you run docker stop?
The main process is the process Docker starts as the container's primary command. For an application container, this should normally be the application process, such as dotnet MyApi.dll. The running lifetime of the container follows that process.
When the main process ends, the container stops. It does not stay running just because its application files still exist. In the experiment, sleep 3600 is the main process, so reaching the end of that wait also ends the container's run.
Linux uses signals to notify processes of events. By default, docker stop normally sends SIGTERM, a request to terminate, to the container's main process. Docker waits 10 seconds by default for Linux containers. If the process has not exited, Docker sends SIGKILL, which forcibly terminates it.
The stop signal and waiting period are configurable. An image can specify a different stop signal, so SIGTERM is the normal default, not an unconditional rule. Windows containers wait 30 seconds by default.
By contrast, docker kill sends SIGKILL immediately by default, without a graceful-shutdown wait. You can choose another signal, but the default gives the application no chance to finish work before termination.
For an ASP.NET Core application, graceful shutdown gives the application a chance to respond to the termination request before forced termination. It is an opportunity, not a guarantee that every request or write will complete. The container remains present after stopping, along with its writable layer.
How you start the application affects whether it receives termination signals correctly. The next step is ASP.NET Core startup commands and graceful shutdown.
The mistake to avoid. Do not equate stopping with deleting, or assume Docker immediately kills the application. The normal Linux sequence allows a configurable wait before forced termination.
In an interview, say: “For Linux containers, docker stop normally sends SIGTERM, waits 10 seconds by default, then sends SIGKILL if needed. The signal and timeout are configurable. docker kill sends SIGKILL immediately by default, or a chosen signal, without that wait. Neither command removes the container.”
Why does a Docker container exit immediately?
An immediate exit means the main process has ended. That might be a normal completion, not a crash. For example, a command that prints the .NET version and finishes is not a long-running server.
A startup failure can also end the process: perhaps the startup command is wrong, a required setting is absent, or the application cannot reach a dependency. Check evidence before choosing a cause.
For a container named my-api, start with:
docker ps -a
docker inspect --format '{{json .State}}' my-api
docker logs my-apiThese commands require an existing container named my-api; substitute your application's container name or ID.
docker ps -a lists stopped containers as well as running ones. Without -a, a container that exited can seem to have disappeared.
docker inspect shows detailed container information. Here, .State selects its state, including status, exit code, and other available state details. An exit code of zero usually indicates successful process completion; a nonzero code indicates a problem according to that program's conventions. Neither identifies the cause by itself.
Exit code 137 usually means the process was killed with SIGKILL, for example after the stop timeout or for using too much memory.
Preserve this evidence before removing the failed container. For the next investigation steps, see container logs and debugging .NET applications.
The mistake to avoid. Do not diagnose every immediate exit as a Docker failure or treat one exit code as a complete explanation. First establish what command ended and what evidence it left.
In an interview, say: “The main process ended, either normally or because startup failed. I check docker ps -a, inspect the state and exit status, and read the available startup logs. An exit code is evidence, not a diagnosis.”
Check yourself
A container writes an ordinary file into its writable layer. You stop it, start it, then remove it and create another container with the same name. What happens to the file?
Show answer
Correct answer: C
Stop/start preserves the same container and its writable layer. Removing that container discards the layer. Reusing its name does not restore its files.
You pull a corrected image under the same tag used to create an existing container. What applies the corrected application files?
Show answer
Correct answer: B
An existing container keeps the image it was created from. Pulling changes locally available image content, not the existing container. Create a replacement from the desired image.
Going deeper
Filesystem separation is not complete isolation. Independent writable layers do not prevent containers from sharing a volume, contacting the same database, or affecting shared resources. Our experiment deliberately attaches no shared storage.
Not every .NET image contains the same tools. Microsoft provides different image types for building and running applications. Some smaller image variants omit tools such as a shell, so the experiment's sh commands should not be copied unchanged into every image. The .NET container images guide describes the choices; the course lesson on .NET base images explains their trade-offs.
Windows has additional isolation choices. The shared-kernel explanation describes ordinary Linux containers and process-isolated Windows containers. Hyper-V-isolated Windows containers instead run with a separate kernel in an optimized VM, providing a different isolation boundary. Keep that qualification when an interviewer asks whether containers always share the host kernel.
Layers also affect builds. Their read-only role here is separate from how Docker reuses build work. Continue with Docker image layers and the build cache when you are ready to examine how images are assembled.
Sources
- Introduction to Docker - .NET | Microsoft Learn: Official .NET image distribution, regular image updates, and the distinction between local application development and container execution.
- .NET container images - .NET | Microsoft Learn: Different .NET image types and variants, tag characteristics, and size-oriented image choices.
- What is Docker? - .NET | Microsoft Learn: Read-only image layers, Docker Desktop's VM environment, container/VM comparisons, and Windows process versus Hyper-V isolation.
- Docker containers, images, and registries - .NET | Microsoft Learn: Images as static application packages, containers instantiated from images, and registries as image distribution services.
Practice
Checked before publishing: links opened, reviewed while it was written and read in full. How lessons are checked
Updated 30 Nov 2026 ·