Introduction
In the previous lesson, we examined images, containers, and port mapping. In this lesson, we will run our own ASP.NET Core Web API inside a container. We will not yet create a production-optimized image or write a Dockerfile. Instead, we will run the official .NET SDK image directly and mount the project source into the container. This approach is useful during Docker-based development because it allows us to test code changes quickly and observe the relationship between the application and the container before introducing a Dockerfile.
By the end of the lesson, the application will run inside a container, ASP.NET Core will accept requests on all network interfaces, and we will access the Swagger interface from a browser on the host machine. These details matter: if the application listens only on localhost, it cannot be reached from outside the container. Likewise, if the container port is not published to the host, the browser cannot connect to the application.
The commands in this lesson target Docker Desktop or Docker Engine using Linux containers. The examples assume a .NET 8 SDK image and a classic ASP.NET Core Web API template with Swagger enabled.
The container execution approach
In a normal development workflow, we install the .NET SDK on the computer and run dotnet run directly on the host operating system. In this lesson, we will run the same command inside an SDK container. The container provides the .NET SDK, while the project files are made available from the host through a bind mount into the container's /src directory.
The following components work together:
- SDK image: Provides the .NET tools required to restore, build, and run the application.
- Bind mount: Makes the project directory on the host visible inside the container.
- Working directory: The -w /src option makes commands run from the directory containing the project source.
- Port mapping: Connects a host port to the ASP.NET Core port inside the container.
- ASPNETCORE_URLS: Specifies the URL and network interface on which the Kestrel server listens.
Preparing the project for container execution
First, verify that you have an ASP.NET Core Web API project. The real project from the previous lesson may have a different name. In the examples below, the project directory is assumed to be Catalog.Api and the current working directory is the solution root.
dotnet --version dotnet run --project Catalog.Api/Catalog.Api.csproj
When you run the application on the host, the terminal displays one or more listening addresses. Open the Swagger address in a browser to confirm that the application works. Depending on the template and .NET version, the Swagger path is commonly /swagger. For example:
http://localhost:5000/swagger
If Swagger is not configured in the project, you can use the following basic configuration for .NET 8. Make sure that the required NuGet package is present in the project:
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.MapControllers(); app.Run();
This example enables Swagger only in the Development environment. Because we will start the container in Development, the interface will be available. We will also test over HTTP, so HTTPS redirection must not interfere with the first container test. If your project contains app.UseHttpsRedirection() and you have not configured an HTTPS certificate in the container, temporarily remove the line or execute it only in environments where HTTPS is configured.
Starting the first SDK container
Now run the official SDK image directly without creating a Dockerfile. Open a terminal in the solution or project directory. For Linux/macOS Bash:
docker run --rm -it \ -p 8080:8080 \ -v "$(pwd):/src" \ -w /src \ -e ASPNETCORE_ENVIRONMENT=Development \ -e ASPNETCORE_URLS=http://+:8080 \ mcr.microsoft.com/dotnet/sdk:8.0 \ dotnet run --project Catalog.Api/Catalog.Api.csproj --no-launch-profile
If you are using PowerShell, the volume argument can be written as follows:
docker run --rm -it ` -p 8080:8080 ` -v "${PWD}:/src" ` -w /src ` -e ASPNETCORE_ENVIRONMENT=Development ` -e ASPNETCORE_URLS=http://+:8080 ` mcr.microsoft.com/dotnet/sdk:8.0 ` dotnet run --project Catalog.Api/Catalog.Api.csproj --no-launch-profile
Let us break down the command. --rm automatically removes the temporary container when it stops. -it attaches an interactive terminal. -p 8080:8080 maps port 8080 on the host to port 8080 inside the container. The value on the left is the host port; the value on the right is the container port.
The -v option mounts the project directory at /src inside the container. Therefore, the dotnet run command can see the source code. -w /src sets the working directory. With -e ASPNETCORE_ENVIRONMENT=Development, the application runs in the Development environment. Finally, ASPNETCORE_URLS=http://+:8080 uses the + wildcard so that the application listens on all network interfaces in the container rather than only on its internal localhost address.
Listening on all network interfaces
A common networking problem in containers occurs when the application listens on localhost. The address localhost or 127.0.0.1 refers to the container's own loopback interface. Even if a request reaches the container from the host, the application may not accept it if it listens only on that loopback address.
For container scenarios, use one of the following address forms:
- http://0.0.0.0:8080
- http://+:8080
You can also configure the URL directly in code:
var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseUrls("http://0.0.0.0:8080");
However, using an environment variable instead of hard-coding the URL is more flexible. The same image can run with different ports in different environments. For that reason, this lesson sets ASPNETCORE_URLS through the Docker command.
Testing the Swagger endpoint through the container
After the container starts, the terminal displays logs related to restore, build, and the addresses on which the application is listening. If the application is running, open the following address in a browser on the host machine:
http://localhost:8080/swagger
When the Swagger interface opens, the request first arrives at host port 8080. Docker forwards it to container port 8080. Kestrel is listening inside the container at http://+:8080, so it accepts the request. The host and container ports do not have to be identical. For example, the following command also works:
docker run --rm -it \ -p 5005:8080 \ -v "$(pwd):/src" \ -w /src \ -e ASPNETCORE_ENVIRONMENT=Development \ -e ASPNETCORE_URLS=http://+:8080 \ mcr.microsoft.com/dotnet/sdk:8.0 \ dotnet run --project Catalog.Api/Catalog.Api.csproj --no-launch-profile
In this case, the browser address is http://localhost:5005/swagger. The application still uses port 8080 inside the container; only the host-side entry port has changed.
You can inspect the container from another terminal:
docker ps docker logs <container-id> docker port <container-id>
If you want to assign a name to the container, add --name catalog-api-dev to the command. You can then use the name instead of the long container ID:
docker logs catalog-api-dev
Common mistakes and solutions
1. The Swagger address refuses the connection
First, check whether the container is running with docker ps. If it stops immediately, inspect the output of docker logs. The port mapping may be missing, or the application may be listening on a different port. Make sure that -p 8080:8080 and ASPNETCORE_URLS=http://+:8080 use matching container ports.
2. The application listens only on localhost
The profiles in launchSettings.json are not automatically used when the container is started this way. Therefore, --no-launch-profile makes the behavior explicit. Also verify that ASPNETCORE_URLS is set to http://+:8080 or http://0.0.0.0:8080, not http://localhost:8080.
3. HTTPS redirection causes an error
A development certificate may exist on the host machine, but the SDK container does not automatically have access to it. For a simple HTTP test, temporarily disable UseHttpsRedirection or configure certificate mounts and environment settings separately. HTTPS will be addressed later from a deployment perspective.
4. The project file cannot be found
Make sure the source path of the volume points to the solution root and that -w /src uses that location. Remember that the --project path is resolved inside the container file system. A Windows path from the host does not appear inside the container as C:\...; it appears below the mounted Linux path, /src.
5. The host port is already in use
If another process is using port 8080, change only the host side of the mapping:
docker run --rm -it -p 8090:8080 ...
The application still runs on port 8080 inside the container. Open http://localhost:8090/swagger in the browser.
Short practice exercise
- Confirm that the Swagger interface of your Web API works on the host.
- Start the application from the SDK image without a Dockerfile.
- Map host port 8090 to container port 8080.
- Run the application with ASPNETCORE_URLS=http://+:8080.
- Open http://localhost:8090/swagger in the browser.
- Call one controller endpoint through Swagger and then inspect the output of docker logs.
As an additional task, stop and restart the container. Make a small change to an endpoint or model in the source directory and start the application again. Observe how the bind mount makes the host files available to the SDK container.
Conclusion
In this lesson, we ran an ASP.NET Core Web API without a Dockerfile by using the official .NET SDK image. We mounted the project directory into the container, started the application with dotnet run, published the container port to the host, and configured Kestrel to listen on all network interfaces with ASPNETCORE_URLS.
This approach does not yet produce a deployment-ready image, but it is a valuable step for understanding the basic execution model between Docker and ASP.NET Core. In the next stage, we will build the same application with a Dockerfile and separate SDK responsibilities from runtime responsibilities. This will allow us to create smaller, repeatable ASP.NET Core images that are better suited to deployment.
Sources
- https://learn.microsoft.com/en-us/dotnet/core/docker/
- https://learn.microsoft.com/aspnet/core/host-and-deploy/docker/building-net-docker-images
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?tabs=basicconfiguration
- https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-7.0
- https://docs.docker.com/get-started/docker-concepts/running-containers/publishing-ports/
- https://docs.docker.com/reference/cli/docker/container/run