{"id":38719,"date":"2020-03-28T12:43:22","date_gmt":"2020-03-28T19:43:22","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/premier-developer\/?p=38719"},"modified":"2020-03-18T13:27:02","modified_gmt":"2020-03-18T20:27:02","slug":"hosting-and-asp-net-core-api-in-a-container-part-1-of-2-building-the-container","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/hosting-and-asp-net-core-api-in-a-container-part-1-of-2-building-the-container\/","title":{"rendered":"Hosting and ASP.NET Core API in a Container Part 1 of 2 &#8211; Building the Container"},"content":{"rendered":"<p>App Dev Manager <a href=\"https:\/\/www.linkedin.com\/in\/greg-roe-3a7b66b\/\" target=\"_blank\" rel=\"noopener noreferrer\">Greg Roe<\/a> explores hosting cross platform ASP.NET Core Applications with containers in this two part series.<\/p>\n<hr \/>\n<p>ASP.NET Core is a cross-platform, <a href=\"https:\/\/github.com\/dotnet\/aspnetcore\">open-source<\/a> 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 \u201cBlue Yonder Hotel Service\u201d in a Docker Containers. The api will expose api service endpoint to call hotel services.<\/p>\n<p>We\u2019ll use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/tools\/\">dotnet cli<\/a> a cross platform took for developing, building, running, and publishing .Net Core apps and <a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio Code<\/a>, Microsoft\u2019s open source, cross-platform code editor that runs everywhere.<\/p>\n<ol start=\"1\">\n<li>Installation Prerequisites:<\/li>\n<\/ol>\n<ul>\n<li><a href=\"https:\/\/www.docker.com\/products\/docker-desktop\">Docker Desktop<\/a><\/li>\n<li>.<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/install\/sdk?pivots=os-windows\">Net Core SDK<\/a><\/li>\n<li><a href=\"https:\/\/code.visualstudio.com\/\">Visual Studio Code<\/a>,<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Create a sample ASP.NET Core Web App by pulling down a sample from Docker Hub.<\/li>\n<\/ol>\n<ul>\n<li>C:\\demo\\Host_In_Docker dotnet new webapi &#8211;name BlueYonder.Hotels.Service \u2013output \\Demo\\Host_In_Docker<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>Containerize the app: add a DockerFile<\/li>\n<\/ol>\n<ul>\n<li>Run Visual Studio Code: C:\\demo\\Host_In_Docker\\ code .<\/li>\n<li>In EXPLORER panel right click, add new file namded Dockerfile<\/li>\n<li>Paste the following Docker commands into the Dockerfile :<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">FROM microsoft\/dotnet:2.1-aspnetcore-runtime AS base\r\nWORKDIR \/app\r\nEXPOSE 55419\r\nEXPOSE 44398\r\nFROM microsoft\/dotnet:2.1-sdk AS build\r\nWORKDIR \/src\r\nCOPY Host_In_Docker\/BlueYonder.Hotels.Service.csproj Host_In_Docker\/\r\nRUN dotnet restore Host_In_Docker\/BlueYonder.Hotels.Service.csproj\r\nCOPY . .\r\nWORKDIR \/src\/Host_In_Docker\r\nRUN dotnet build BlueYonder.Hotels.Service.csproj -c Release -o \/app\r\nFROM build AS publish\r\nRUN dotnet publish BlueYonder.Hotels.Service.csproj -c Release -o \/app\r\nFROM base AS final\r\nWORKDIR \/app\r\nCOPY --from=publish \/app .\r\nENTRYPOINT [\"dotnet\", \"BlueYonder.Hotels.Service.dll\"]<\/pre>\n<ol start=\"4\">\n<li>Build the containerized app from the DockerFile. From C:\\demo<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true \">docker build -t hotels_service -f Host_In_Docker\\DockerFile .<\/pre>\n<ol start=\"5\">\n<li>Run the docker container: docker run \u2013rm -it -p 8080:80 hotels_service<\/li>\n<\/ol>\n<ul>\n<li>Open a browser and navigate to: localhost:8080\/api\/values<\/li>\n<li>Check that you are getting the expected response [\u201cvalue1\u201d,\u201dvalue2\u201d]<\/li>\n<li>Or find the local IP address docker network inspect nat and run x.x.x.x\/api\/values<\/li>\n<\/ul>\n<ol start=\"6\">\n<li>Push the Container to Azure Container Registry<\/li>\n<\/ol>\n<ul>\n<li>Login to ACR: Docker login gjracractivateazure.azurecr.io<\/li>\n<li>Get the image id: Docker Image<\/li>\n<li>Tag the image: docker tag 5d4fb8198d39 gjracractivateazure\/hotels_service:1.0<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/premier-developer\/push-an-asp-net-core-api-container-to-azure-containter-registry-part-2-of-2\/\">Continue reading Part 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. <\/p>\n","protected":false},"author":582,"featured_media":38721,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[80],"tags":[67,2,29,3],"class_list":["post-38719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","tag-asp-net","tag-containers","tag-docker","tag-team"],"acf":[],"blog_post_summary":"<p>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. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=38719"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38719\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/38721"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=38719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=38719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=38719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}