Build Docker Image of .NET Core Application using Visual Studio 2019

Build Docker Image of .NET Core Application using Visual Studio
Photo by Ian Taylor / Unsplash

Deploying Kubernetes with a sample image from Docker Hub or any other registry is straightforward and easy but when we need to build the image of your own application for Kubernetes then we start looking here and there if you are from the non-development background and new to the container world like me :).

In this post, I will show how to create a docker image of your .Net application using Visual Studio 2019. Here, I am assuming you are familiar with the basic Docker command, and docker is already installed on your machine. Docker image is created based on the DockerFile. If you are good at creating DockerFile then you don't need to use Visual Studio, you can use any command-line tool. If you are a beginner then its visual studio is a good place to start.  I am here using Visual Studio to generate DockerFile rather than writing DockerFile.

  1. Below is the sample.NetCore application and it's running fine locally. We want to run this application in Docker as a container. There is no DockerFile yet.
1

2. To add/generate DockerFile for the application.

2

3. DockerFIle has added content of it as below.

3
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["DockerImage.csproj", ""]
RUN dotnet restore "./DockerImage.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DockerImage.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "DockerImage.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerImage.dll"]

4. Press Ctrl+F5 to build the docker image. You can see the build has been completed an application is running in docker. Verify it with the below docker command.

4

5. We can also check this with the Docker command - docker ps.

5

6. Below is the video demonstration of creating a Docker Image and running the application locally.

I hope you enjoyed the post and it gave you fair information to start with. Please visit sakaldeep.com.np for more posts.