{"id":35374,"date":"2019-02-27T12:40:03","date_gmt":"2019-02-27T19:40:03","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/premier-developer\/?p=35374"},"modified":"2019-02-25T12:52:24","modified_gmt":"2019-02-25T19:52:24","slug":"running-a-net-core-web-application-in-docker-container-using-docker-desktop-for-windows","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/running-a-net-core-web-application-in-docker-container-using-docker-desktop-for-windows\/","title":{"rendered":"Running a .NET Core Web Application in Docker container using Docker Desktop for Windows"},"content":{"rendered":"<p>In this post, App Dev Managers <a href=\"https:\/\/www.linkedin.com\/in\/anand-shukla-45b7081\/\">Anand Shukla<\/a> and <a href=\"https:\/\/www.linkedin.com\/in\/sash-kavalaparambil-b497539\/\">Sash Kavalaparambi<\/a>l provide step by step instruction to run a .NET Core Web Application in Docker container using Docker Desktop for Windows.<\/p>\n<hr \/>\n<p>If you do not have Docker Desktop for Windows already installed, we will need to install it from <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fdocs.docker.com%2Fdocker-for-windows%2Finstall%2F&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383055586&amp;sdata=CvBv7Md%2BgOkio1j6r1ULy9PqrXT5PRnxcL1I50esDgI%3D&amp;reserved=0\">https:\/\/docs.docker.com\/docker-for-windows\/install\/<\/a> . Please check the system requirements to make sure it is supported on your OS. This will redirect you to <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fhub.docker.com%2Feditions%2Fcommunity%2Fdocker-ce-desktop-windows&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383055586&amp;sdata=uj5XTpVCsieAIW17wmz6oWYUPJS7hidkniA7fb2Qawo%3D&amp;reserved=0\">https:\/\/hub.docker.com\/editions\/community\/docker-ce-desktop-windows<\/a> where you will need to login, and download the Docker Desktop for Windows. You may have to logout \/ login or restart your system depending on whether Hyper-V is enabled on your system or not.<\/p>\n<p>Once you have installed Docker Desktop for Windows, you will find the Docker icon in your system tray. You can right click, and access the menu to change some of the settings. As you can see from screen below, we are currently running Linux containers, and can switch to Windows container by clicking the menu option.<\/p>\n<p><img decoding=\"async\" width=\"185\" height=\"206\" class=\"wp-image-35375\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage005-png01d4c511-c832b7f0.png\" alt=\"cid:image005.png@01D4C511.C832B7F0\" \/><\/p>\n<p><img decoding=\"async\" width=\"451\" height=\"311\" class=\"wp-image-35376\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage007-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image007.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage007-jpg01d4c511-c832b7f0.jpeg 451w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage007-jpg01d4c511-c832b7f0-300x207.jpeg 300w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/p>\n<p>You can access the settings by clicking on Settings and can change some of the behaviors such as \u201cStart Docker Desktop when you log in\u201d<\/p>\n<p>Now we have our Docker Desktop installed, the next step is to create a new ASP.NET core project. You can use VS Code, command line or Visual Studio to create an ASP.NET core project. I will be using Visual Studio to create a new ASP.NET core project but you can use VS Code or command line to create one as well.<\/p>\n<p>Click on File -&gt; New Project, and then select ASP.NET Core Web Application from .NET Core template. Enter name as aspnetapp, and choose a location for your application.<\/p>\n<p><img decoding=\"async\" width=\"689\" height=\"506\" class=\"wp-image-35377\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage008-png01d4c511-c832b7f0.png\" alt=\"cid:image008.png@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage008-png01d4c511-c832b7f0.png 689w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage008-png01d4c511-c832b7f0-300x220.png 300w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/p>\n<p>Use \u201cWeb Application (Model-View-Controller)\u201d on the next screen and click OK to create the project.<\/p>\n<p><img decoding=\"async\" width=\"500\" height=\"310\" class=\"wp-image-35378\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage009-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image009.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage009-jpg01d4c511-c832b7f0.jpeg 500w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage009-jpg01d4c511-c832b7f0-300x186.jpeg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Once the project is created, create 2 files (Dockerfile, dockerignore.txt -&gt; to be renamed to .dockerignore) in the project directory. Alternatively, you could have chosen \u201cEnable Docker Support\u201d and Visual Studio would have created these files for you. The Dockerfile contains instruction to build the container image, and .dockerignore file contains content to ignore when building the image.<\/p>\n<p>Please note that Windows will not allow you to create a file which starts with a dot. That\u2019s why we create the file with name dockerignore.txt and then use the command line to rename it.<\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"66\" class=\"wp-image-35379\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage010-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image010.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage010-jpg01d4c511-c832b7f0.jpeg 600w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage010-jpg01d4c511-c832b7f0-300x33.jpeg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Put the following in Dockerfile. This files 10 steps which builds the application using a separate image which contains the SDK to build, and packages it with an image which just has the ASP.NET Core runtime. Description for each of these steps is provided when we will run the Docker command to build the image.<\/p>\n<p><code>FROM microsoft\/dotnet:sdk AS build-env<\/code><\/p>\n<p><code>WORKDIR \/app<\/code><\/p>\n<p><code># Copy csproj and restore as distinct layers<\/code><\/p>\n<p><code>COPY *.csproj .\/<\/code><\/p>\n<p><code>RUN dotnet restore<\/code><\/p>\n<p><code># Copy everything else and build<\/code><\/p>\n<p><code>COPY . .\/<\/code><\/p>\n<p><code>RUN dotnet publish -c Release -o out<\/code><\/p>\n<p><code># Build runtime image<\/code><\/p>\n<p><code>FROM microsoft\/dotnet:aspnetcore-runtime<\/code><\/p>\n<p><code>WORKDIR \/app<\/code><\/p>\n<p><code>COPY --from=build-env \/app\/out .<\/code><\/p>\n<p><code>ENTRYPOINT [\"dotnet\", \"aspnetapp.dll\"]<\/code><\/p>\n<p>Put the following in .dockerignore<\/p>\n<p><code>bin\\<\/code><\/p>\n<p><code>obj\\<\/code><\/p>\n<p>Open PowerShell, and run the command \u201cdocker build -t aspnetapp .\u201d. The option -t will tag the image with name \u201caspnetapp\u201d.<\/p>\n<p><img decoding=\"async\" width=\"641\" height=\"381\" class=\"wp-image-35380\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage011-png01d4c511-c832b7f0.png\" alt=\"cid:image011.png@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage011-png01d4c511-c832b7f0.png 641w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage011-png01d4c511-c832b7f0-300x178.png 300w\" sizes=\"(max-width: 641px) 100vw, 641px\" \/><\/p>\n<p><img decoding=\"async\" width=\"604\" height=\"41\" class=\"wp-image-35381\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage012-png01d4c511-c832b7f0.png\" alt=\"cid:image012.png@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage012-png01d4c511-c832b7f0.png 604w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage012-png01d4c511-c832b7f0-300x20.png 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/p>\n<p><img decoding=\"async\" width=\"616\" height=\"115\" class=\"wp-image-35382\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage013-png01d4c511-c832b7f0.png\" alt=\"cid:image013.png@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage013-png01d4c511-c832b7f0.png 616w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage013-png01d4c511-c832b7f0-300x56.png 300w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/p>\n<p><img decoding=\"async\" width=\"620\" height=\"339\" class=\"wp-image-35383\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage014-png01d4c511-c832b7f0.png\" alt=\"cid:image014.png@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage014-png01d4c511-c832b7f0.png 620w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage014-png01d4c511-c832b7f0-300x164.png 300w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>Here is what happens on each of the step:<\/p>\n<ol>\n<li>Step 1\/10: FROM microsoft\/dotnet:sdk AS build-env\nPulls the image with tag \u201csdk\u201d from Microsoft\/dotnet registry on docker hub &#8211; <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fhub.docker.com%2Fr%2Fmicrosoft%2Fdotnet%2Ftags&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383065580&amp;sdata=PaI90QMcY5vQHmZytRnHoApVH9yuJydT%2F2MQibyry4U%3D&amp;reserved=0\">https:\/\/hub.docker.com\/r\/microsoft\/dotnet\/tags<\/a><\/p>\n<p>\u201cbuild-env\u201d name can be given to the new build stage. This will be used to copy file in the later steps.<\/li>\n<li>Step 2\/10: WORKDIR \/app\nThe WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn\u2019t exist, it will be created even if it\u2019s not used in any subsequent Dockerfile instruction.<\/li>\n<li>Step 3\/10: COPY *.csproj .\/\nCopies all CS Project files to the working directory inside the container.<\/li>\n<li>Step 4\/10: RUN dotnet restore\nThis is the .NET command line which restores all the required Nuget packages to build the project.<\/li>\n<li>Step 5\/10: COPY . .\/\nThis copies all the content of Visual Studio project into the working directory.<\/li>\n<li>Step 6\/10: RUN dotnet publish -c Release -o out\nThis step uses .NET CLI to create a release build with output directory as \u201cout\u201d<\/li>\n<li>Step 7\/10: FROM microsoft\/dotnet:aspnetcore-runtime\nPulls the image with tag \u201caspnetcore-runtime\u201d from Microsoft\/dotnet registry on docker hub &#8211; <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fhub.docker.com%2Fr%2Fmicrosoft%2Fdotnet%2Ftags&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383065580&amp;sdata=PaI90QMcY5vQHmZytRnHoApVH9yuJydT%2F2MQibyry4U%3D&amp;reserved=0\">https:\/\/hub.docker.com\/r\/microsoft\/dotnet\/tags<\/a> which provides the minimum components to run an ASP.NET Core Web Application.<\/li>\n<li>Step 8\/10: WORKDIR \/app\nSame as Step 2 above.<\/li>\n<li>Step 9\/10: COPY &#8211;from=build-env \/app\/out .\nThis copies the content of \/app\/out folder from the \u201cbuild-env\u201d to this net container.<\/li>\n<li>Step 10\/10: ENTRYPOINT [&#8220;dotnet&#8221;, &#8220;aspnetapp.dll&#8221;]\nThis allows the container to run as an executable.<\/li>\n<\/ol>\n<p>Now we have our Docker Image created, you can list all the images present by using \u201cdocker images ls\u201d command<\/p>\n<p><img decoding=\"async\" width=\"630\" height=\"90\" class=\"wp-image-35384\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage015-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image015.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage015-jpg01d4c511-c832b7f0.jpeg 630w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage015-jpg01d4c511-c832b7f0-300x43.jpeg 300w\" sizes=\"(max-width: 630px) 100vw, 630px\" \/><\/p>\n<p>Now let\u2019s go ahead and create a container from the image by using \u201cdocker run -d -p 8080:80 &#8211;name myapp aspnetapp\u201d. This command will create a container named myapp from the image aspnetapp, and map port 80 of the container to port 8080 on the host machine.<\/p>\n<p><img decoding=\"async\" width=\"635\" height=\"33\" class=\"wp-image-35385\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage016-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image016.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage016-jpg01d4c511-c832b7f0.jpeg 635w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage016-jpg01d4c511-c832b7f0-300x16.jpeg 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/p>\n<p>Now you can browse to your application by going to <a href=\"http:\/\/localhost:8080\">http:\/\/localhost:8080<\/a> and see your application running.<\/p>\n<p><img decoding=\"async\" width=\"742\" height=\"451\" class=\"wp-image-35386\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage017-jpg01d4c511-c832b7f0.jpeg\" alt=\"cid:image017.jpg@01D4C511.C832B7F0\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage017-jpg01d4c511-c832b7f0.jpeg 742w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2019\/02\/cidimage017-jpg01d4c511-c832b7f0-300x182.jpeg 300w\" sizes=\"(max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>As you can see from steps above, it is very simple and straight forward to get an ASP.NET Core web application running in a Docker container. For more examples, please visit examples on Docker ( <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fdocs.docker.com%2Fengine%2Fexamples%2Fdotnetcore%2F&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383075574&amp;sdata=2YntacRJrSTWaA1hNbJgHM7scR3x%2FhYZ5fxQvS7APmg%3D&amp;reserved=0\">https:\/\/docs.docker.com\/engine\/examples\/dotnetcore\/<\/a> ) or ASP.NET Core Website (<a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fhost-and-deploy%2Fdocker%2F%3Fview%3Daspnetcore-2.2&amp;data=02%7C01%7Cashukla%40microsoft.com%7C4c735de7113249774e0308d69352d99c%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636858381383075574&amp;sdata=pqfb4HV5sOEIUy8eFbshbrsjq6q5B6tb3rGzpthLHgU%3D&amp;reserved=0\">https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/host-and-deploy\/docker\/?view=aspnetcore-2.2<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App Dev Managers Anand Shukla and Sash Kavalaparambil provide step by step instruction to run a .NET Core Web Application in Docker container using Docker Desktop for Windows.<\/p>\n","protected":false},"author":582,"featured_media":35387,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[80],"tags":[83,29,3],"class_list":["post-35374","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","tag-net-core","tag-docker","tag-team"],"acf":[],"blog_post_summary":"<p>App Dev Managers Anand Shukla and Sash Kavalaparambil provide step by step instruction to run a .NET Core Web Application in Docker container using Docker Desktop for Windows.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/35374","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=35374"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/35374\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/35387"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=35374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=35374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=35374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}