Containerize an ASP.NET Core Web API with Docker
Containerize a Web API for easy mocking and testing by Front-End developers
As Front-End developers we are sometimes faced with situations where we need to run back-end code on our local Dev machines just to make API requests. This is when Docker can come in handy.
Today we will look at how we can containerize an ASP.NET Web API using Docker. By so doing, instead of running back-end code on your Dev machine you instead just spin up an instance of a Docker image. The good thing is that, you can run the same Docker image in different environments without other extra configurations and customization.
Here are some of the tools or software we will use:
- ASP.NET Core SDK 3.1
- Visual Studio Code (VS Code)
Also make sure to install the Docker extension for VS Code by Microsoft. To install it, simply click on extensions or press
Ctrl + Shift + x. Search for Docker and click install.
We can now create an ASP.NET Core web API by running the following command within our projects folder:
dotnet new webapi -o IntroToMvc --no-http. This creates a dotnet web API named IntroToMvc and places it in a similarly named folder. To run the project, simply run command
dotnet run in the projects root directory.
Then in a web browser, go this URL: http://localhost:5000/WeatherForecast and you should get JSON output similar to this.
Now go to the VS Code command palette by pressing
Ctrl + Shift + p
In the command palette, type:
Docker: Add and then select the command
Add docker Files to Workspace... On application platform select
ASP.NET Core and on Operating System, select the operating system on which you intend to run your container e.g
VS Code will generate two files named
Dockerfile within the projects root folder. The contents of the Dockerfile should be similar to this.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS baseWORKDIR /appEXPOSE 80FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS buildWORKDIR /srcCOPY ["IntroToMvc.csproj", "./"]RUN dotnet restore "./IntroToMvc.csproj"COPY . .# WORKDIR "/src/."RUN dotnet build "IntroToMvc.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "IntroToMvc.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "IntroToMvc.dll"]
dockerignore file species what files to exclude from the image.
At this stage we can now build the Docker image, simply run this command in the folder where the Dockerfile is located:
docker build -t dockerwebapi -f Dockerfile.
The command above, says: build a Docker image based on the Dockerfile and give it the name
To verify that the image has been created run
docker images from the command-line interface. The output should be similar to this.
In addition to the image name you specified, i.e.
dockerwebapi Docker also created images for
asp.net core runtime and
asp.net core sdk which are specified in the Dockerfile.
As specified in the Dockerfile, the Docker container will be exposed to the outside world on port 80, in this case.
Run the Docker image via the command:
docker run -ti --rm -p 8080:80 dockerwebapi
Go to the URL http://localhost:8080/WeatherForecast in your browser and you will see some JSON data which is coming in from the Docker instance you are running.
You can now use this Docker instance to make API request without having to run back-end code on your Dev machine. If another developer wants to make similar tests on the API simply give them a copy of the Docker image. Alternatively, you could push the image to Docker registry or azure container registry for easy distribution.