Build ASP.NET Core webapp with Docker

by | Feb 19, 2021 | .NET Core, Docker

It’s a time ago I wrote something on my blog, so it is time again. In this post I want to explain how I build Microsoft NET core applications in an docker container and run them also as an docker. Since some time also Microsoft SQL server is available in an Linux docker container and that makes it an good combination with NET core and Entity Framework. Also I have one application running with NET Core 2.0 and Entity Framework on MySQL (5.7), I will post on this later.

Docker

First of all we need to have an Docker installation available, you can find really good instructions for this on https://docs.docker.com/install/#server . Pick the Docker Community Edition and install this on your Windows, Mac OS or Linux operating system. By picking the latest on you make sure all the Docker files will work, for example I found out that the ‘apt install docker.io’ on Ubuntu didn’t had the right version to support the docker file I use in this post.

Build configuration

Now we have docker running we are creating 2 new files in the root of you NET Core project. To learn how to start your first ASP.NET Core web application check the documentation from Microsoft https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-2.1&tabs=aspnetcore2x

With an Docker ignore file we exclude the already localy builded solutions and objects. Create an new file name “.dockerignore” and add this text:

bin/
obj/

After this create an file named “Dockerfile” and add the following text:

FROM microsoft/aspnetcore-build:latest AS builder
WORKDIR /app
 
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
 
# Copy everything else and build
COPY . .
RUN dotnet publish --output /app/ --configuration Release
 
# Build runtime image
FROM microsoft/aspnetcore:latest
WORKDIR /app
COPY --from=builder /app .
ENTRYPOINT ["dotnet", "WebApp.dll"]

This latest Dockerfile will pull 2 new docker images from Microsoft named aspnetcore-build and aspnetcore. From both the latest image will be downloaded, make sure this is in line with your NET Core version locally. Once the images are downloaded it will copy the files in the first aspnetcore-build containter and start building the solution. Once that is done it will copy the builded file into the new docker image that will actually run the web application.

In the last line the entrypoint for your docker container is defined, also make sure here that you choose the name of the DLL file that is builded. Usually this is the name of your Project.

Run the container

Now we have the build configuration in place we build and run the Docker images. Start building with docker with the following command:

docker build -t webapp .

The docker image name will be ‘webapp’ in this example but you are free to choose your own here. The dot on the end refers that we run this command in the same directory where the Dockerfile and .dockerignore files are. Now we have the image ready we can run the container:

docker run -d -p 5001:5001 --name webapp webapp

With the ‘-d’ we run the container as an Deamon and we don’t see the output in our screen, check if the container is running with “docker ps” or check the output with “docker logs webapp”. If you want to follow the container output you can run it without the ‘-d’ or use the following command “docker logs -f webapp”. Next to ‘-d’ we also used ‘-p 5001:5001’ to connect the container on the local network on port 5001.

Test the solution

Now you can use your webbrowser locally to open the WebApp with “http://localhost:5001” or remote with your Server IP ‘http://serverip:5001’.

Thanks for reading and let me know when there are questions,

Cheers,

eighty data blog

Terug naar kantoor

Terug naar kantoor

Nu het thuiswerk advies langzaam komt te vervallen trekken steeds meer mensen weer naar kantoor. Gaan we weer terug naar het oude "9 tot 5" of omarmen we het hybride werken? Met een modern ingerichte IT omgeving op basis van Microsoft 365 heb je de flexibiliteit om op...

Windows Server herstart loop

Windows Server herstart loop

Na het installeren van de recent uitgebrachte januari 2022 beveiliging updates voor Windows Server 2012R2 raakte de server in een herstart loop. Het probleem houdt verband met een Active Directory component en komt daardoor voor op domain controllers. Hoewel het in ons geval om een Windows Server 2012R2 versie gaat, blijkt uit diverse andere blogs dat het probleem ook op andere Windows Server versies voor kan komen. Wees dus voorzichtig met het installeren van deze updates. Heb je de updates al geïnstalleerd en is je server in een reboot-loop terecht gekomen? Lees dan onze stappen om de server weer operationeel te maken.

Azure Static Webapps

Azure Static Webapps We are using Azure Static Webapps to host several apps & tooling developed for our customers. However how do you setup the hosting / deployment. In the upcoming series of blogs we will share some tips and tricks to setup that multi environment...

New office and branding!

New office and branding!

New office! This month we moved in to a brand new office located on Zomerdijk 2a in Maassluis. Having our own office is a major milestone which comes with many benefits. It allows us to work together even more closely on our projects, receive customers and hosts a...

Azure Cli Alternative with Powershell on macOS

Azure Cli Alternative with Powershell on macOS

Last year I didn’t post much on my blog. So here again a post that is the first for this year. Let’s see if I can find the time to do more this year. Recently I am kept busy with Microsoft Azure and often switch between Mac OS and Windows. PowerShell is working great...