Build ASP.NET Core WebApp with 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,