Introduction
In the previous lesson, we prepared the ASP.NET Core project and the Docker development environment for the course. This lesson focuses on the fundamental building blocks of Docker. Before running an application in a container, it is important to understand how images, containers, registries, layers, and port mapping differ from one another.
These concepts form the foundation for the Dockerfile, SQL Server container, and Docker Compose work that follows. By the end of this lesson, you will pull an image, create a container from it, publish an ASP.NET Core application through a host port, stop the container, and clean up the resources.
Docker's execution model
What is an image?
A Docker image is a read-only template used to create containers. It can contain operating system files, a runtime, application files, and default settings required to start the application. An image is not a running process by itself; when it is started, it becomes an instance of a container.
An image name commonly follows this format:
registry/namespace/repository:tag
For example, in mcr.microsoft.com/dotnet/aspnet:8.0, mcr.microsoft.com identifies the registry, dotnet/aspnet identifies the image repository, and 8.0 is the tag. When the registry and namespace are omitted, Docker commonly uses Docker Hub as the default registry.
What is a container?
A container is a running instance of an image. Multiple containers can be started from the same image, and each container can have different names, ports, environment variables, and network connections. Containers run as isolated processes from the host and from one another; unlike virtual machines, they do not normally boot a complete operating system for each instance.
A container filesystem is created by adding a writable layer on top of the image layers. When the container is removed, changes stored only in that temporary layer disappear. We will use volumes in later lessons when data must survive the container lifecycle.
Registries and layers
A registry is a server that stores and distributes Docker images. Docker Hub is a public registry, while Microsoft Container Registry is an important source for .NET images. The docker pull command downloads an image from a registry into the local image cache.
Images are built from layers, which represent filesystem changes such as adding, modifying, or deleting files. An image can be based on another base image. Layer caching makes repeated builds faster and allows common layers to be shared by multiple images.
Pulling the first image
Verify that Docker Desktop or Docker Engine is running:
docker version
Now download the official ASP.NET Core sample image to the local machine:
docker pull mcr.microsoft.com/dotnet/samples:aspnetapp
List the images stored locally:
docker image ls
The output includes the repository, tag, image ID, creation information, and size. If the same repository and tag already exist in the local cache, Docker may reuse the cached image instead of downloading it again. To inspect a particular image, run:
docker image inspect mcr.microsoft.com/dotnet/samples:aspnetapp
You can remove an image with docker image rm. However, if a container still uses that image, remove the related container first.
Running a container
Let us create a container from the image. The --name option assigns a readable name, while --detach, or its short form -d, runs the container in the background.
docker run --name aspnet-demo -d mcr.microsoft.com/dotnet/samples:aspnetapp
List running containers:
docker ps
List all containers, including stopped containers:
docker ps -a
If the image is not available locally, docker run can attempt to pull it using the default pull behavior. It then creates a new container and starts its main process. The container lifecycle is tied to that main process. When the main process exits, the container is considered stopped.
View the container logs:
docker logs aspnet-demo
Inspect the container configuration, environment variables, and network details:
docker inspect aspnet-demo
How port mapping works
A port inside a container is not the same as a port on the host machine. The ASP.NET Core application listens on a port inside the container. To make that port reachable from the host, we must publish it. Docker's short port publishing syntax is:
-p HOST_PORT:CONTAINER_PORT
In .NET 8 and later official ASP.NET Core container images, the default HTTP port is generally 8080. Therefore, using the correct container port is essential. The port 80, which appears in many older examples, may not be correct automatically for newer images.
Stop and remove the previous container:
docker stop aspnet-demo docker rm aspnet-demo
Start it again while mapping container port 8080 to host port 8080:
docker run --name aspnet-demo -d -p 8080:8080 mcr.microsoft.com/dotnet/samples:aspnetapp
Open http://localhost:8080 in a browser. If the application responds, requests arriving at host port 8080 are being forwarded to port 8080 inside the container. Verify the mapping with:
docker ps
In the PORTS column, you should see a value similar to 0.0.0.0:8080->8080/tcp.
The host port and container port can be different:
docker stop aspnet-demo docker rm aspnet-demo docker run --name aspnet-demo -d -p 5000:8080 mcr.microsoft.com/dotnet/samples:aspnetapp
In this case, the application still listens on port 8080 inside the container, but it is available from the host at http://localhost:5000. We changed only the externally used host port, not the port used by the application inside the container.
Difference between publishing a port and EXPOSE
The EXPOSE instruction in a Dockerfile documents which port an image expects to use. It is metadata and does not, by itself, make the port reachable through the host. To create an actual host mapping, use the -p option with docker run.
For example, -p 5000:8080 connects host port 5000 to container port 8080. Writing only EXPOSE 8080 does not automatically open host port 8080.
Stopping, starting, and removing containers
Stop a running container:
docker stop aspnet-demo
A stopped container is not deleted and can be started again:
docker start aspnet-demo
If the container is no longer needed, remove it:
docker rm aspnet-demo
To stop and remove a running container with one command, use -f:
docker rm -f aspnet-demo
For temporary test containers, you can ask Docker to remove the container automatically when it exits:
docker run --rm -d --name aspnet-temporary -p 8080:8080 mcr.microsoft.com/dotnet/samples:aspnetapp
With this option, the container object is automatically removed when its main process ends. During development, you may prefer to keep the container without --rm so that you can inspect its logs and final state.
Running your own ASP.NET Core image
If you created a Dockerfile or application image in the first lesson, replace the sample image with your own image name. For example, if your image is called catalog-api:dev:
docker run --name catalog-api -d -p 5000:8080 catalog-api:dev
The key point is that the application must actually listen on port 8080 inside the container. If it uses another port, change the right side of the port mapping accordingly. You can explicitly configure the port with an environment variable when necessary:
docker run --name catalog-api -d \ -p 5000:8080 \ -e ASPNETCORE_HTTP_PORTS=8080 \ catalog-api:dev
Applications using older hosting APIs may require ASPNETCORE_URLS=http://+:8080 instead of ASPNETCORE_HTTP_PORTS. Verify the result through the application logs and an HTTP request.
Common mistakes
1. Mapping the wrong container port
Using -p 5000:80 while the application listens on port 8080 causes a connection failure. First determine the internal port used by the image or application, then put that port on the right side of the mapping.
2. The application listens only on localhost
If the application binds only to localhost inside the container, it may not be reachable from outside even when Docker port mapping is configured. Confirm that ASP.NET Core listens on an address suitable for container use. Explicitly setting the .NET container port environment variable can help avoid ambiguity.
3. The host port is already in use
If you receive an error similar to Bind for 0.0.0.0:5000 failed, another process or container may already be using the host port. Choose another host port:
docker run --name catalog-api -d -p 5001:8080 catalog-api:dev
4. The container starts and immediately stops
Use docker ps -a to inspect its state and docker logs container-name to read the error. The main process may have exited, a required configuration value may be missing, or an exception may have occurred during application startup.
5. Confusing an image with a container
An image is a template, while a container is a running or stopped instance created from that template. docker image ls lists images, and docker ps -a lists containers. Removing a container does not automatically remove its image.
Short practice exercise
Pull the mcr.microsoft.com/dotnet/samples:aspnetapp image.
Run a container named demo-web in detached mode and map host port 7070 to container port 8080.
Verify the mapping with docker ps and open http://localhost:7070 in a browser.
Inspect the container logs and then stop the container.
Start the stopped container again and finally remove it.
The expected command flow is:
docker pull mcr.microsoft.com/dotnet/samples:aspnetapp docker run --name demo-web -d -p 7070:8080 mcr.microsoft.com/dotnet/samples:aspnetapp docker ps docker logs demo-web docker stop demo-web docker start demo-web docker stop demo-web docker rm demo-web
Conclusion
In this lesson, we saw that a Docker image is a distributable template and a container is a running instance created from that image. We practiced pulling an image from a registry, listing images, running a container, checking logs, and managing the container lifecycle.
The most important practical detail is the port mapping syntax: -p HOST_PORT:CONTAINER_PORT. You can change the host port, but the container port must be the port on which the application is actually listening. In the next lesson, we will use this knowledge to create a Dockerfile for the ASP.NET Core project and build our own image.
Sources
- https://docs.docker.com/get-started/docker-overview
- https://docs.docker.com/get-started/docker-concepts/the-basics/what-is-an-image/
- https://docs.docker.com/get-started/docker-concepts/the-basics/what-is-a-registry/
- https://docs.docker.com/reference/cli/docker/container/run
- https://docs.docker.com/reference/cli/docker/container/rm
- https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/aspnet-port