{"id":8484,"date":"2020-08-17T22:35:29","date_gmt":"2020-08-17T22:35:29","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8484"},"modified":"2020-08-17T22:35:29","modified_gmt":"2020-08-17T22:35:29","slug":"blazor-asp-net-core-on-docker","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/blazor-asp-net-core-on-docker\/","title":{"rendered":"Blazor\/ASP.NET core on docker"},"content":{"rendered":"<p>I wanted to get a Blazor server application up and running on Ubuntu within a docker container (I&#8217;m running the whole thing on a Raspberry Pi 4 with Ubuntu Server).<\/p>\n<p>The first stage for this post will simply be about creating a Dockerfile and creating a Blazor server application via the dotnet template.<\/p>\n<p>We&#8217;re going to want the latest version of dotnet core, so let&#8217;s start by creating a very bare bones Dockerfile which will create an image based upon<br \/>\nmcr.microsoft.com\/dotnet\/core\/sdk:3.1, it will also expose the standard HTTP port used in the Blazor server template, i.e. port 5000<\/p>\n<p>Here&#8217;s the Dockerfile<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFROM mcr.microsoft.com\/dotnet\/core\/sdk:3.1\r\nENV APP_HOME \/home\r\nRUN mkdir -p $APP_HOME\r\nWORKDIR $APP_HOME\r\nEXPOSE 5000\r\nCMD &#x5B; &quot;bash&quot; ]\r\n<\/pre>\n<p>To build this image run the following <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndocker rmi dotnet-env --force\r\ndocker build -t dotnet-env .\r\n<\/pre>\n<p>The first line is solely there to remove any existing image (which will be especially useful whilst developing the image). The second line will build the Dockerfile and name it dotnet-env.<\/p>\n<p>Once built, let&#8217;s run the image to see all is good with it. So simply run<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndocker run -it --rm -p 5000:5000 -v \/home\/share:\/home\/share dotnet-env\r\n<\/pre>\n<p>In this example we&#8217;ll run docker in interactive mode and map ports using -p to map the host port 5000 to the exposed port in the image. We&#8217;ll also also created a volume link from the container to the host.<\/p>\n<p>Once we&#8217;ve run the image up we should be placed into a BASH command prompt, now we can simply run<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndotnet new blazorserver -o MyServer\r\n<\/pre>\n<p>To create the project MyServer, once created <em>cd<\/em> into the MyServer folder. Now run<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndotnet run\r\n<\/pre>\n<p>A kestrel server should start up, and you might be able to access the server using http:\/\/server-ip-address. I say might, because you may well see an error at startup, saying something like<\/p>\n<p><em>Unable to bind to http:\/\/localhost:5000 on the IPv6 loopback interface: &#8216;Cannot assign requested address&#8217;.<\/em><\/p>\n<p>What you need to do is go into the Properties folder and open launchSettings.json, change the line<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&quot;applicationUrl&quot;: &quot;https:\/\/localhost:5001;http:\/\/localhost:5000&quot;,\r\n<\/pre>\n<p>to <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&quot;applicationUrl&quot;: &quot;http:\/\/0.0.0.0:5001;http:\/\/0.0.0.0:5000&quot;,\r\n<\/pre>\n<p><strong>Next Step<\/strong><\/p>\n<p>This obvious next step to our docker build is to create a Docker image which contains our application and runs it when the container is started. We&#8217;re going to build and publish the using <em>dotnet publish -c Release -o publish<\/em> and then include the published files in our docker container, alternatively you might prefer to have the Dockerfile build and publish the project as part of its build process.<\/p>\n<p>For now let&#8217;s just build our Blazor server application, then publish it to a folder. <\/p>\n<p>We&#8217;re going to host the application in Kestrel, so before we go any further open the appsetting.json file from the publish folder and add the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;Kestrel&quot;: {\r\n  &quot;EndPoints&quot;: {\r\n    &quot;Http&quot;: {\r\n      &quot;Url&quot;: &quot;http:\/\/0.0.0.0:5000&quot;\r\n    }   \r\n  }\r\n},\r\n<\/pre>\n<p>Now we&#8217;ll make the changes to the Dockerfile to copy the published folder to the image and start up the Kestrel server when the image is run, here&#8217;s the Dockerfile<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nFROM mcr.microsoft.com\/dotnet\/core\/sdk:3.1\r\n\r\nENV APP_HOME \/home\r\nRUN mkdir -p $APP_HOME\r\n\r\nWORKDIR $APP_HOME\r\n\r\nCOPY .\/BlazorServer\/BlazorServer\/publish ${APP_HOME}\r\n\r\nEXPOSE 5000\r\n\r\nCMD &#x5B; &quot;dotnet&quot;, &quot;BlazorServer.dll&quot; ]\r\n<\/pre>\n<p>Now you should be able to access your server using http:\/\/your_server_name:5000.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to get a Blazor server application up and running on Ubuntu within a docker container (I&#8217;m running the whole thing on a Raspberry Pi 4 with Ubuntu Server). The first stage for this post will simply be about creating a Dockerfile and creating a Blazor server application via the dotnet template. We&#8217;re going [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[200,305,102],"tags":[],"class_list":["post-8484","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-blazor","category-docker"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8484"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8484\/revisions"}],"predecessor-version":[{"id":8507,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8484\/revisions\/8507"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}