Hosting and ASP.NET Core API in a Container Part 1 of 2 – Building the Container
App Dev Manager Greg Roe explores hosting cross platform ASP.NET Core Applications with containers in this two part series.
ASP.NET Core is a cross-platform, open-source framework for building modern , cloud-based, connected applications. With ASP.NET core you can build web apps, API APS, Microservices, mobile backends, and IoT apps. You can use your development tools of choice on Windows MacOS, and Linux. You can deploy in any cloud or on premises. You can run in native cloud services such as Azure Web Apps, Linux, or, Containers. Today we will build and demonstrate hosting an ASP.NET core api app “Blue Yonder Hotel Service” in a Docker Containers. The api will expose api service endpoint to call hotel services.
We’ll use the dotnet cli a cross platform took for developing, building, running, and publishing .Net Core apps and Visual Studio Code, Microsoft’s open source, cross-platform code editor that runs everywhere.
- Installation Prerequisites:
- Create a sample ASP.NET Core Web App by pulling down a sample from Docker Hub.
- C:\demo\Host_In_Docker dotnet new webapi –name BlueYonder.Hotels.Service –output \Demo\Host_In_Docker
- Containerize the app: add a DockerFile
- Run Visual Studio Code: C:\demo\Host_In_Docker\ code .
- In EXPLORER panel right click, add new file namded Dockerfile
- Paste the following Docker commands into the Dockerfile :
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base WORKDIR /app EXPOSE 55419 EXPOSE 44398 FROM microsoft/dotnet:2.1-sdk AS build WORKDIR /src COPY Host_In_Docker/BlueYonder.Hotels.Service.csproj Host_In_Docker/ RUN dotnet restore Host_In_Docker/BlueYonder.Hotels.Service.csproj COPY . . WORKDIR /src/Host_In_Docker RUN dotnet build BlueYonder.Hotels.Service.csproj -c Release -o /app FROM build AS publish RUN dotnet publish BlueYonder.Hotels.Service.csproj -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "BlueYonder.Hotels.Service.dll"]
- Build the containerized app from the DockerFile. From C:\demo
docker build -t hotels_service -f Host_In_Docker\DockerFile .
- Run the docker container: docker run –rm -it -p 8080:80 hotels_service
- Open a browser and navigate to: localhost:8080/api/values
- Check that you are getting the expected response [“value1”,”value2”]
- Or find the local IP address docker network inspect nat and run x.x.x.x/api/values
- Push the Container to Azure Container Registry
- Login to ACR: Docker login gjracractivateazure.azurecr.io
- Get the image id: Docker Image
- Tag the image: docker tag 5d4fb8198d39 gjracractivateazure/hotels_service:1.0
Curious, why are you using the 2.1 containers? Your link directs readers to the 3.1 SDK, which ensures their docker build will fail with a mismatch.
This while thing feels rushed and doesn’t really work as is.
Came here to agree with Kitty. Walkthough fails at the second step