Docker Community Forums

Share and learn in the Docker community.

.Net Web Api improvements

Hey guys,
I created a .Net 5 Web Api and want to add Docker support for it. I tried to get into it by playing around with a sample app. So with the terminal I create a new Web Api

mkdir project
cd project
dotnet new sln
dotnet new webapi -o Api
dotnet sln add ./Api

I check that everything is fine with

cd Api
dotnet run
call https://localhost:5001/weatherforecast in the browser
close with ctrl + c

Inside the Api project I create a Dockerfile with this content

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "Api.dll" ]

I also create a .dockerignore file with this content

.git
Dockerfile
bin/
obj/

I’m building an image with

docker build -t api .

When I now run

docker images

this image has a size of 209MB. I would like to know if this is “the right way to go” or if there is something I can improve in the Dockerfile or .dockerignore file.

Thanks in advance

In this post I would like to present a few tips to improve the performance of your Web API services.
Use the fastest JSON serializer available. …
Use compression techniques. …
Use faster data access strategies. …
Use caching. …
Use asynchronous methods judiciously.

@lewish95 I think code optimization is another topic :slight_smile:

This question is just about creating the “best” Dockerfile and add all the things to the .dockerignore that are not needed for Docker