Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 20, 2021 03:57 pm GMT

Web API in NET 6 Docker

Hello!

How are you? I hope all of you are stay safe and having a great day!

If you want to follow this step-by-step. Please install some tools that will be required here.

  1. .NET 6: https://dotnet.microsoft.com/download/dotnet/6.0
  2. Docker: https://docs.docker.com/get-docker/

Project Preparation

  • Create the Web API from template.
dotnet new webapi -o DockerNetExample
  • Create the solution to link the project and link the project.
dotnet new sln
dotnet sln add DocketNetExample
  • Your project is ready now. :)

Prepare for Dockerfile

  • Create your Dockerfile at the root, similar place with the .sln file.
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as buildWORKDIR /appCOPY . .RUN dotnet restoreRUN dotnet publish -o /app/published-appFROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine as runtimeWORKDIR /appCOPY --from=build /app/published-app /appENTRYPOINT [ "dotnet", "/app/DockerNetExample.dll" ]

Build the Image and Run the Container

  • Build your image use docker build . -t dotnetexample

  • Run a container with previous image. docker run --name dotnetexample -p 8081:80 -d dotnetexample

  • Check your container. docker ps. You would see like this.

Docker PS

  • Check your API use browser or another things.

Note: If you use Firefox, the return will be like this, the value maybe will be different, since it returns randomly.
Browser Firefox

  • Congrats. You are done. :)

Repository

Thank you

Thank you for visiting this tutorial. If you have any questions or suggestions, please comment in here.

Thank you


Original Link: https://dev.to/berviantoleo/web-api-in-net-6-docker-41d5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To