Dockerize an ASP.NET Core Application
Overview
In this article we are trying to run an Asp.Net Core Web application on containers using docker. We have an example to demonstrate how to dockerize an ASP.NET Core application in this post.
What is ASP.NET Core?
ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. With ASP.NET Core, you can:
- Build web apps and services, IoT apps, and mobile backends.
- Use your favorite development tools on Windows, macOS, and Linux.
- Deploy to the cloud or on-premises.
- Run on .NET Core or .NET Framework.
What is docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Prerequisites
We need an ASP.NET Core and Docker application on machine.
Steps to dockerize ASP.NET Core Application
Step 1: Create a ASP.NET Core Web Application in visual studio. Refer below screenshot
Step 2: Create Docker file with name (Dockerfile) with no extension at parent folder of your application. Refer below screenshot
Docker file contains following text in it:
# This is base image which has dot net core install in it
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY WebApplication/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
#Give executable dll path of your application after publish
ENTRYPOINT ["dotnet", "/app/WebApplication/bin/Release/netcoreapp2.1/WebApplication.dll"]
Step 3: To make your build context as small as possible add a .dockerignore file to your project folder and copy the following into it.
bin\
obj\
Step 4: Now we will build and run the docker image
- Open a command prompt and navigate to your project folder.
- Use the following commands to build and run your Docker image:
$ docker build -t webapplication .
$ docker run -d -p 8080:80 --name webapp webapplication
On execution of first command it will download asp.net core base image from docker hub and create a customize image with the image name as "webapplication" which has web application files in it.
In second command, we have to give image name which we have used in build command. This image is used to run container and application on port 8080.
Refer below screenshot of same
Step 5: You can view the web page running from a container.
- Go to localhost:8080 to access your app in a web browser.
Refer below screenshot:
Now you have ASP.NET Core Web application that is running on docker.
Thanks for reading this article. Please don’t forget to share this article if you like it.
Thanks for reading this article. Please don’t forget to share this article if you like it.
Comments
Post a Comment