Publish your first .net 5 API on Docker

Hiran
2 min readApr 22, 2021

Containerizing smart and modern way to publish your application, so you might think dockerize your API.

This article is focusing on,

  • .net 5 REST API
  • docker
  • Azure devops
  • Azure app service for container
  1. setup the solution with your API projects and other supporting projects

2. Add .dockerignore , which will prevent unnecessary files copy into the the docker images

3. Add .Dockerfile files to root level of the solution

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source
COPY . ./RUN dotnet restoreWORKDIR /source/[API project]RUN dotnet publish -c release -o /DockerOutput/Website --no-restoreFROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /DockerOutput/Website
COPY --from=build /DockerOutput/Website ./ENTRYPOINT ["dotnet", "[API project].dll"]

list of things we have done in above dockerfile

  • select a build server (image)
  • create a new folder on the build server
  • copy all source files/folders to build server
  • restore packages on all projects
  • navigate to API project and build it
  • select runtime image, copy published files to it
  • define an entry point for the API (name of the API assembly)

To test this locally, run the docker build command

docker  build  -t  [name  for  the  image]  .

By default docker build command will take only the last statement in docker file, so, only runtime image will take.

Next step:: publish docker image in Azure devops ….

--

--