Building and deploying a modern web API becomes much easier when you combine ASP.NET Core, Visual Studio, and Docker. ASP.NET Core provides a fast and flexible framework for developing RESTful APIs, while Docker allows you to package your application and its dependencies into a consistent, portable container.
In this article, we’ll walk through how to create an ASP.NET Core Web API in Visual Studio, configure a simple API, create a Dockerfile, build a Docker image, and run the application inside a Docker container. You’ll also learn the basic steps required to deploy your ASP.NET Core API using Docker.
Whether you’re new to ASP.NET Core or looking to understand how Docker fits into the .NET development workflow, this step-by-step guide will help you get started with containerizing and deploying an ASP.NET Core API.
Prerequisites
Before creating and deploying the ASP.NET Core API with Docker, make sure the required development and containerization tools are installed on your machine.
You’ll need the following:- Visual Studio – Used to create and develop the ASP.NET Core Web API.
- .NET SDK – Required to build and run the ASP.NET Core application.
- Docker Desktop – Used to build Docker images and run the API inside a container.
- Basic knowledge of C# and ASP.NET Core – Helpful for understanding the API code and project structure.
Once these tools are installed and configured, we can create a new ASP.NET Core Web API project in Visual Studio.
Create an ASP.NET Core Web API Project
Follow these steps to create a new ASP.NET Core Web API project in Visual Studio.- Open Visual Studio:
Launch Visual Studio on your computer. Make sure you have the ASP.NET and web development workload installed. From the Visual Studio start screen, select Create a new project.
- Select the ASP.NET Core Web API Template
In the Create a new project window:- Search for Web API in the search box.
- Select ASP.NET Core Web API from the list of templates.
- Click Next.
- Configure the Project
On the Configure your new project screen, enter the project details:- Project name: DockerDemoApi
- Location: Select the folder where you want to store the project.
- Solution name: DockerDemoApi
- Click Next.
- Select the .NET Versionbr/>On the Additional information screen, configure the project as follows:
- Framework: Select the latest stable .NET version available in your Visual Studio installation.
- Authentication type: None
- Configure for HTTPS: Enabled
- Enable OpenAPI support: Enabled
- Use controllers: Enabled
- Then click Create.
- Explore the Project
Visual Studio will create the ASP.NET Core Web API project and open it in Solution Explorer. You should see files and folders similar to:
DockerDemoApi │ ├── Controllers │ └── WeatherForecastController.cs ├── Properties ├── appsettings.json ├── appsettings.Development.json ├── Program.cs └── DockerDemoApi.csprojThe template may also contain additional files depending on the .NET version and Visual Studio version.
- Run the API: Press F5 or click the Run button in Visual Studio.
The application will start, and Visual Studio will launch the API using the configured development server.
If OpenAPI/Swagger is enabled, you should see the Swagger UI in your browser. From there, you can view and test the available API endpoints.
At this point, the ASP.NET Core Web API is running successfully. In the next section, we'll create a simple API endpoint and then add Docker support to containerize the application.
Create a Sample API Endpoint
Now that the ASP.NET Core Web API project has been created, let's add a simple API endpoint that returns a list of products. This endpoint will be used later to test the API after it has been containerized with Docker.
- Create a Model
- In Solution Explorer, right-click the project and select Add > New Folder. Name the folder
Models. - Right-click the
Modelsfolder and select Add > Class. Name the classProduct.cs. - Add the following code:
</> C#
Thenamespace DockerDemoApi.Models { public class Product { public int Id { get; set; } public string Name { get; set; } = string.Empty; public decimal Price { get; set; } } }Product.csclass represents the data that our API will return.
- In Solution Explorer, right-click the project and select Add > New Folder. Name the folder
- Create an API Controller
- Right-click the Controllers folder and select Add > Controller.
- Select API Controller - Empty and click Add.
- Name the controller
ProductsController.cs. - Replace the generated code with:
</> C#
using DockerDemoApi.Models; using Microsoft.AspNetCore.Mvc; namespace DockerDemoApi.Controllers { [ApiController] [Route("api/[controller]")] public class ProductsController : ControllerBase { [HttpGet] public IActionResult GetProducts() { var products = new List<Product> { new Product { Id = 1, Name = "Laptop", Price = 75000 }, new Product { Id = 2, Name = "Keyboard", Price = 2500 }, new Product { Id = 3, Name = "Mouse", Price = 1200 } }; return Ok(products); } } }
- Understand the API Endpoint
The following attributes define the URL and HTTP method:</> C#
[Route("api/[controller]")] [HttpGet] - Run and Test the API: Press F5 or click the Run button in Visual Studio.
Because the controller is named ProductsController, the API endpoint will be GET /api/products. When a client sends a GET request to this endpoint, the API returns the product list as JSON.
If Swagger/OpenAPI is enabled, the Swagger UI will open in your browser. Find the GET /api/products endpoint and click Try it out, followed by Execute.
You should receive a JSON response similar to:</> JSON
[
{
"id": 1,
"name": "Laptop",
"price": 75000
},
{
"id": 2,
"name": "Keyboard",
"price": 2500
},
{
"id": 3,
"name": "Mouse",
"price": 1200
}
]
Add Docker Support to the ASP.NET Core Web API
Now that the ASP.NET Core Web API is working, the next step is to containerize the application using Docker. Docker packages the application and its dependencies into a container, making it easier to run consistently across different environments.
- Make Sure Docker Desktop Is Running
Before adding Docker support, install Docker Desktop and make sure it is running.
You can verify that Docker is available by opening a terminal and running:</> CMD
docker --versionIf Docker is installed correctly, the command displays the installed Docker version.
- Add Docker Support from Visual Studio
Visual Studio can automatically generate the Docker configuration for an ASP.NET Core project.
- In Solution Explorer, right-click the DockerDemoApi project
- select: Add > Docker Support
If Visual Studio asks you to choose a container platform, select Linux unless your deployment environment specifically requires Windows containers.
Visual Studio will add the required Docker files to the project. - Check the Dockerfile
After adding Docker support, you should see a file named Dockerfile in your project.
A typical Dockerfile generated for an ASP.NET Core application looks similar to: Build Stage# Build stage FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build WORKDIR /src COPY ["DockerDemoApi/DockerDemoApi.csproj", "DockerDemoApi/"] RUN dotnet restore "DockerDemoApi/DockerDemoApi.csproj" COPY . . WORKDIR "/src/DockerDemoApi" RUN dotnet build "DockerDemoApi.csproj" -c Release -o /app/build # Publish stage FROM build AS publish RUN dotnet publish "DockerDemoApi.csproj" -c Release -o /app/publish /p:UseAppHost=false # Runtime stage FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DockerDemoApi.dll"]Note:- The exact Dockerfile generated by Visual Studio depends on the .NET version and Visual Studio version you are using. Use the framework version that matches your project.
- Understand the Dockerfile
The Dockerfile uses a multi-stage build. This keeps the final container smaller by separating the build environment from the runtime environment.
Build StageFROM mcr.microsoft.com/dotnet/sdk:10.0 AS buildThe .NET SDK image contains the tools required to restore dependencies and compile the application.
The project file is then copied into the container:
Dependencies are restored using:COPY ["DockerDemoApi/DockerDemoApi.csproj", "DockerDemoApi/"]
The remaining source code is copied and the application is compiled:RUN dotnet restore "DockerDemoApi/DockerDemoApi.csproj"
Publish Stage- The application is published using:COPY . . RUN dotnet build "DockerDemoApi.csproj" -c Release -o /app/buildRUN dotnet publish "DockerDemoApi.csproj" -c Release -o /app/publish /p:UseAppHost=falseThis creates the files required to run the application.
Runtime Stage- The final stage uses the smaller ASP.NET Core runtime image:
The published application is copied into the runtime container:FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
Finally, the application is started with:COPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "DockerDemoApi.dll"] - Check the Generated Files
After Docker support has been added, your project structure may look similar to:DockerDemoApi │ ├── Controllers │ └── ProductsController.cs ├── Models │ └── Product.cs ├── Properties ├── Dockerfile ├── appsettings.json ├── appsettings.Development.json ├── DockerDemoApi.csproj └── Program.csVisual Studio may also add additional Docker-related configuration files depending on the project and Visual Studio version.
- Run the Application with Docker
Once Docker support has been added and Docker Desktop is running, you can select the Docker launch profile in Visual Studio and run the project.
Visual Studio will build the Docker image, start a container, and launch the application using the Docker environment.
You can then open the Swagger UI and test the GET /api/products endpoint just as you did when running the application directly from Visual Studio.
The ASP.NET Core API is now ready to be packaged as a Docker image.
Note: If you're targeting .NET 8 or .NET 9 instead of .NET 10, replace the 10.0 image tags with the corresponding version used by your project.
Build the Docker Image Using Docker CLI
Now that Docker support has been added to the ASP.NET Core Web API, we can manually build a Docker image using the Docker CLI.
A Docker image contains everything required to run the application, including the published application files, runtime, and required dependencies.
Follow the steps to build a docker image- Open a Terminal
- Open Command Prompt, PowerShell, or the Terminal in Visual Studio.
- Navigate to the directory containing your solution and Dockerfile.
- For example:
cd C:\Projects\DockerDemoApi - Make sure the Dockerfile is available in the directory from which you run the Docker command.
- You can verify the files using:
dir - On Linux or macOS, use:
ls
- Build the Docker Image
- Run the following command:
docker build -t dockerdemoapi:latest . - Let's understand the command:
- docker build tells Docker to build an image.
- -t dockerdemoapi:latest assigns the image the name dockerdemoapi and the tag latest.
.tells Docker to use the current directory as the build context.
Docker will read the instructions from the Dockerfile and execute each step to create the image.
You should see output similar to:</> Output
The exact output may vary depending on your Docker and .NET versions.[+] Building ... => [internal] load build definition from Dockerfile => [internal] load metadata ... => [build] ... => [publish] ... => [final] ... => exporting to image => => naming to docker.io/library/dockerdemoapi:latest - Run the following command:
- Verify the Docker Image
After the build completes successfully, run:</> CMD
You should see an entry similar to:docker images</> Output
REPOSITORY TAG IMAGE ID CREATED SIZE dockerdemoapi latest abc123456789 a few seconds ago ...The dockerdemoapi image is now available locally and can be used to create a Docker container.
- Build the Image with a Specific Version
Instead of using the
latesttag, you can assign a version to the image:</> CMD
docker build -t dockerdemoapi:1.0 .Using meaningful version tags can make it easier to manage different releases of your application.
For example:</> Output
dockerdemoapi:1.0 dockerdemoapi:1.1 dockerdemoapi:2.0 - Inspect the Image
You can view detailed information about the image using:</> CMD
docker inspect dockerdemoapi:latestThis command displays metadata such as the image configuration, environment settings, entry point, and layers.
- Confirm the Image Is Ready
At this point, the ASP.NET Core Web API has been successfully converted into a Docker image.
The workflow is:ASP.NET Core Source Code ↓ Dockerfile ↓ docker build command ↓ Docker Image ↓ Docker Container
The image itself is not a running application. The next step is to create and start a Docker container from this image.
Run the ASP.NET Core API in a Docker Container
After building the Docker image, the next step is to create a Docker container and run the ASP.NET Core Web API inside it.
A Docker image is a packaged application, while a container is a running instance of that image.
- Run the Docker Container
Open a terminal and run the following command:</> CMD
This command starts the ASP.NET Core API in the background.docker run -d -p 8080:8080 --name dockerdemoapi-container dockerdemoapi:latest
Let's break down the command:docker runcreates and starts a new container.-d runsthe container in detached mode.-p 8080:8080maps port8080on your computer to port8080inside the container.--name dockerdemoapi-containerassigns a name to the container.dockerdemoapi:latestspecifies the Docker image to use.
Note:-The port used by your application depends on the ASP.NET Core configuration and the Dockerfile. If your container listens on a different port, adjust the port mapping accordingly.
- Check the Running Container
To verify that the container is running, execute:</> CMD
You should see output similar to:docker ps</> Output
CONTAINER ID IMAGE PORTS a1b2c3d4e5f6 dockerdemoapi:latest 0.0.0.0:8080->8080/tcpThe
PORTScolumn confirms that port8080on your local machine is mapped to port8080inside the container. - View the Container Logs
You can view the application logs using:</> CMD
docker logs dockerdemoapi-containerThe logs can help you confirm that ASP.NET Core started successfully and can also help troubleshoot startup or configuration problems.
To continuously monitor the logs, use:</> CMD
Press Ctrl+C to stop viewing the logs. This does not stop the container.docker logs -f dockerdemoapi-container - Access the API
Once the container is running, open your browser or API testing tool and access:http://localhost:8080/api/products</> Output
This confirms that the ASP.NET Core Web API is running successfully inside the Docker container.[ { "id": 1, "name": "Laptop", "price": 75000 }, { "id": 2, "name": "Keyboard", "price": 2500 }, { "id": 3, "name": "Mouse", "price": 1200 } ]
Deploy the Dockerized ASP.NET Core API
A production server needs access to the Docker image. We therefore push the image to a container registry.
A container registry is a central location where Docker images can be stored and retrieved. Examples include Docker Hub, Azure Container Registry, and Amazon Elastic Container Registry.
For example:</> CMD
docker tag dockerdemoapi:latest <registry>/dockerdemoapi:latest
docker push <registry>/dockerdemoapi:latest
Once the image has been pushed, the deployment platform can pull it from the registry.
The next step is to configure a server or cloud service to run the Docker image. When the deployment starts, the platform typically:
- Pulls the Docker image from the registry.
- Creates a container from the image.
- Configures the required ports and environment variables.
- Starts the ASP.NET Core application.
- Makes the API accessible through a network endpoint.
At this point, our API is no longer limited to localhost. It can be accessed by other applications or clients through the server's public endpoint or domain name.
Deployment also requires production-specific configuration.
For example, database connection strings, API keys, authentication settings, and other secrets should not be stored directly in the Dockerfile or source code.
Instead, these values should be provided through environment variables or the cloud platform's secret-management features.
This allows us to use the same Docker image across different environments while keeping environment-specific configuration separate.
In a production environment, the API should normally be exposed through HTTPS rather than directly exposing an unsecured HTTP endpoint.
A typical setup looks like this:
Client
↓
HTTPS
↓
Load Balancer / Reverse Proxy
↓
Docker Container
↓
ASP.NET Core API
The reverse proxy or cloud platform can handle HTTPS termination, while the ASP.NET Core application runs inside the container.
Thanks