Create a Containerized .NET Core Web Applications


In this article, you’ll learn how to Create a Containerized .NET Core Web Applications. In the previous article, we created some containers based on existing Docker images hosted on Docker Hub. But, if we wanted to create our own Docker images, That’s where a Dockerfile comes in. Please, read our previous article before proceeding to this article where we learn how to Build Docker Images by using Dockerfile.


A Dockerfile contains instructions for how to create a Docker image. So let’s look at a very basic Dockerfile for an ASP.NET Core application first. core Dockerfile

The first instruction in a Dockerfile is FROM. This specifies the base image that we’re running on top of. So, we want to use the Microsoft/dotnet image with the aspnetcore-runtime tag as our starting point. And this image has everything that’s needed to run ASP.NET Core applications.

docker dockerile WORKDIR

Next, we’re using the WORKDIR command, which sets the working directory for other instructions like RUN, COPY, or ENTRYPOINT. Our working directory is the app folder, and if this doesn’t exist, it will get created. core files in docker

Next, we can use the COPY instruction to copy files from our local machine into the image. so, here we’re assuming that we’ve already built and published our .NET Core application into a folder called out on our local development machine, and so we’ll copy the contents of that folder into our container’s working directory.

dockerfile ENTRYPOINT

Finally, the ENTRYPOINT instruction tells the container what to run when it starts up. In this case, it’s going to run the .NET application with samplewebapp.dll as the parameter.

So, we build a Docker image from this Dockerfile, we can use the docker build command if we’re in the same folder as the Dockerfile, and the -t argument is specifying that we’re giving our image a name, so we’re calling it Samplewebapp here.

docker build -t samplewebapp .

So, let’s see a demo in the below example. core file structure

we’re currently in a folder that contains that Dockerfile that we looked at, and this folder also contains a very simple ASP.NET Core test application.

here, we’re going to build that ASP.NET Core application with the dotnet publish command specifying a Release build and that the published binaries should be put into a folder called out, which is what our Dockerfile is expecting.

dotnet publish

Now, we’ve built our .NET application, let’s build our Docker image with docker build command.

docker build

So, that’s the name and optional tag which we haven’t used here that we’re giving to this Docker image, and the dot at the end means we’re running in the local folder, so it’s going to look for a Dockerfile in this folder.

Now, when we run this command, it will take a little while on the first run because we’re going to need to pull down the base image, which was the microsoft/dotnet image.

docker pull image

But, once that image is finished downloading, the rest of the steps will complete quite quickly.

image pull docker registry

So, now we’ve created our image. If we see the docker image list, we can see both the .NET image and our Samplewebapp image.

docker image list

Now, the size of Samplewebapp might look large, but remember, because of the layering of Docker images, it means that most of this space is simply the base image, so it’s actually only taking up an additional megabyte or so of space in addition to my base image.

Let’s run a container from this image with docker run in detached mode, and we’ll expose port 80 on the container as port 8080 locally. we’ll give a container name as a Myapp, and we’re running the Samplewebapp image that we have built. And so once this is running, because we exposed port 8080 locally,

container running

After that, we can visit localhost port 8080 in a web browser, and sure enough, we can see that our ASP.NET Core application is running.

ASP.NET Core application localhost

Now, this isn’t a very exciting app. we can run a .NET Core application inside a Linux container using Docker for Windows.

So, let me stop and delete my container now with docker rm -f to force it because it’s still running and the name of the container, which is myapp.

delete docker Container

Remember that this only deletes my container. The Samplewebapp image is still present for us to create other containers from it if we want to.

Thank you for reading this article, I hope you will understand how to Create a Containerized .NET Core Web Applications.
We’ll get into more details about this when we’ll learn how to Push Docker Image into Docker Hub in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *