30th Mar 2023
Dockerize .NET web API
In this blog, we will explore how to containerize your .NET WebAPI applications using Docker, a popular containerization platform.
Creating the project
We migrated from Web API to .net core for the following reasons listed below,
Click create a new project in the home menu of visual studio,
Select “ASP.NET Core Web API” and click next,
Choose the project and solution name and click “Next”,
Next choose the default framework and click Create.
Next delete the weatherforecast.cs file in the “controller” folder and in the root where program.cs is located.
Create a New controller By “Right-clicking” on the “controller” folder and click “add” choose “controller” , then choose “API” and click “next”
Change the name to “TestController.cs” and click “Add”
Copy paste the below code into “TestController.cs”
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace DockerWebAPI.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public string Hello()
{
return "Hello world";
}
}
}
Now run the app and check if everything is working fine using postman as follows,
Dockerize
Also Create the build files by first “right-click” on the project folder then click publish then choose “folder” as target destination and click “close” now click “publish” again.The Build files will be generated. Within it we have the .dll file which we are going to use to run our app within our docker container.
Now create a file called “Dockerfile” in the root of the project and copy paste the below code,
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY bin/Release/net6.0/publish .
ENTRYPOINT ["dotnet", "DockerWebAPI.dll"]
In the above “Dockerfile” we are using the “aspnet” image from dockerhub as our base image and copy the build files and running the command “dotnet DockerWebAPI.dll” inside the container which will run our app.
Now we can generate our docker image by running the following command in the root of our project where the “Dockerfile” is located,
docker build -t docker-webapi
Then we can create a container from our docker image as follows,
docker run -p 8080:80 docker-webapi
Now we can test it using postman as follows,
Conclusion
We have successfully containerized and tested the dotnet webAPI Thus by leveraging Docker's containerization technology, you can optimize your web API deployment process and achieve a more efficient and reliable application delivery workflow.
About Us
- VS Online Services : Custom Software Development
VS Online Services has been providing custom software development for clients across the globe for many years - especially custom ERP, custom CRM, Innovative Real Estate Solution, Trading Solution, Integration Projects, Business Analytics and our own hyperlocal e-commerce platform vBuy.in and vsEcom.
We have worked with multiple customers to offer customized solutions for both technical and no technical companies. We work closely with the stake holders and provide the best possible result with 100% successful completion To learn more about VS Online Services Custom Software Development Solutions or our product vsEcom please visit our SaaS page, Web App Page, Mobile App Page to know about the solution provided. Have an idea or requirement for digital transformation? please write to us at siva@vsonlineservices.com