Dockerize .NET Core Application + Check docker container error

Hendi Suhardja
5 min readJun 9, 2020

ASP.NET Core Introduction

ASP.NET Core is a redesign of ASP.NET 4.x, with architectural changes that result in a leaner, more modular framework.

Some of the benefits that make ASP.NET Core an excellent framework to develop your apps :

  • Integration of modern, client-side frameworks and development workflows.
  • Built-in dependency injection.
  • Ability to host on IIS, Nginx, Apache, Docker, or self-host in your own process.
  • Ability to build and run on Windows, macOS, and Linux.
  • Open-source and community-focused.

Docker: Introduction

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises. It is a great tool to make it easier to create, deploy and run applications by using containers to do such tasks.

Containers are the idea of running multiple applications on a single host. It’s similar to compute virtualization, but instead of virtualizing a server to create multiple operating systems, containers offer a more lightweight alternative by essentially virtualizing the operating system, allowing multiple workloads to run on a single host.

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. By doing so, thanks to the container, the developer can rest assured that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Prerequisites

Docker Installation

  • Download docker from download.docker.com then install it
  • If your computer have Hyper-V and Containers windows features disabled, docker will ask you to enable them and then will restart your computer automatically. Go ahead and let docker enable these features for you.
  • Configure your docker by right click on docker icon , then click “Settings”
  • Navigate to “Resources” tab, for good performance, please set Memory minimum to “2 GB”
  • Navigate to “File Sharing”, check the drive that you use for your code and all configuration are done

Create .Net Core project

  • Open your visual studio, create new web application “ASP Net Core Web Application” -> Web MVC
  • Run the project to test it
  • Now let’s dockerize the application, if you use Visual Studio, you could add Dockerfile by right click your project -> Add -> Docker support

Change Dockerfile content , especially the path of csproj

  • If you use Visual Code , create new file with name “Dockerfile” on root of project file then put below code on Dockerfile content
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /appEXPOSE 80EXPOSE 443FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY *.csproj ./RUN dotnet restore "DockerUI.csproj"COPY . .WORKDIR "/src"RUN dotnet build "DockerUI.csproj" -c Release -o /app/buildFROM build AS publishRUN dotnet publish "DockerUI.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "DockerUI.dll"]
  • With visual studio, you could dockerize by run “Docker” on top action bar, but we will use command prompt for dockerizing our application. Right ck on project, choose “Open Folder in File Explorer”
  • It will display file explorer, on address bar, type cmd, it will open command prompt with our project path
  • We could build docker image using below command. Note : Docker image name should be lowercase and no space
docker build -t [docker image name] .

or if your Docker file name is not “Dockerfile”, you could use argument -f [Dockerfile name]

docker build -t [docker image name]-f [Dockerfile name].

I will give docker image name = “dockerui” , so my command is

docker build -t dockerui -f Dockerfile .

Wait till the download and build process finished

  • You could make sure if your docker image is created successfully by below command
docker images
  • Then you could run container by attaching your docker image to container on port 80 by below command
docker run -d -p 8080:80 --name [Docker Container Name] [Docker Image Name]

Below is my command

docker run -d -p 8080:80 —-name DockerUI dockerui
  • You could check if your container is running or not by below command
docker ps -a

If status is “Up”, then your container is running well. but if status is Exited, then you need to check the error log, you could check docker container error log by using below command

docker logs --tail 100 -f [Docker Container ID]

In my case

docker logs --tail 100 -f be1c9fff2f64
  • Now you could open your browser and navigate to http://localhost:8080/. Port 8080 is used because when we run docker container, we expose port 8080

Re-dockerize Docker Image

If you want to re-dockerize docker image, you need to do below steps

  • Stop Docker Container that have the docker image
docker stop [Docker Container Name]

In my case

docker stop DockerUI
  • Then remove Docker Container by using below command
docker rm[Docker Container Name]

In my case

docker rm DockerUI
  • Then run container again by below command
docker run -d -p 8080:80 --name [Docker Container Name] [Docker Image Name]

In my case

docker run -d -p 8080:80 --name DockerUI dockerui

Thanks for reading

--

--