Log In

How to Deploy an ASP.NET Core App with Docker

How to Deploy an ASP.NET Core App with Docker
02.05.2024
Reading time: 6 min
Hostman Team
Technical writer

After developing an application, you need to make it available to others. The app should be constantly running and not depend, for example, on the state of the developer's PC.

In this article, we will look at how to deploy an ASP.NET Core web application to a Docker containers and run it on a Hostman cloud server.

Prerequisites

For this guide, you will need:

  • An existing ASP.NET Core application to deploy to a Docker container or a template app for testing purposes. Further on, we will show how to create a template app.

  • A cloud server on Hostman with Docker installed. This will also be described in the guide. 

Containerization and Docker

What is containerization?

Containerization is a method used in software development in which services and their dependencies are packaged into special containers. These containers can be deployed on the server or locally, and we can be sure that we are testing and deploying the same application version.

Containers also isolate applications from each other while being much less resource-intensive than virtual machines.

Docker

Docker is an open-source project that allows you to automate software deployment with containers. The containers can run in different environments like Linux, Windows, or Mac. In general, Docker containers can be compared to virtual machines, but the containers share the kernel of the host's operating system (the server where we deploy the application), while virtual machines use their own OS kernel, so they require more resources.

Basic Docker terms:

  • Images are a package that contains all the dependencies and information needed to create a container. You can create and configure an image on one computer and then deploy it to another, and you will have the same environment.

  • A Dockerfile is a text file with instructions for building a Docker image.

  • A container is an instance of a specific Docker image created from a Dockerfile. The container is responsible for executing the application. It is the containers that we run on the server or locally.

  • Docker Registry is a repository of Docker images. It is very similar to GitHub, but it contains container images instead of code.

Docker has excellent documentation; we recommend checking it out for a deeper dive.

Developing an application

In this tutorial, we will deploy a template ASP.NET Core web API. To do this, create a new folder and run the command in the terminal:

dotnet new webapi

Image13

Now we have a template application. It can be launched by running the command:

dotnet run

Image8

By going to http://localhost:5167/swagger/index.html (the port may vary), we will see the generated Swagger documentation for our API and can try to execute the request:

Image1

Containerizing the application in Docker

So, the application works, now it needs to be containerized and deployed to the server. First, let's add a Dockerfile to describe what our container will consist of:

# Image for our app. To build the app, we use dotnet-sdk
# and call this image builder
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder
# Directory for our application inside the container
WORKDIR/Application
# Copy all files from the project to the container file system COPY. ./ # Run restore to download dependencies RUN dotnet restore # Publish the assembled dll to the "output" folder RUN dotnet publish -c Release -o output
# Build an image in which our application # will be launched. To launch the application, # aspnet is enough, no sdk FROM mcr.microsoft.com/dotnet/aspnet:7.0 WORKDIR/Application # Copy the application files from the previous image COPY --from=builder /Application/output . # Command that will launch the application ENTRYPOINT ["dotnet", "dotnet-app.dll"]

Let's run the command docker build -t dotnet-app . to build our application:

Image11

Using the -t dotnet-app parameter, we give the image a name, and using the . (the dot at the end is the current directory) we indicate the context in which Docker will search for and build the Dockerfile.

Using the docker images command, we can view a list of existing images and information about them:

Image9

Now we can start the container using the command:

docker run -p 5000:80 dotnet-app

With the -p 5000:80 parameter, we tell Docker to map port 5000 on the host to port 80 inside the container.

Image3

Our app is available at http://localhost:5000/WeatherForecast.

In the standard ASP.NET application template, Swagger is only available if the Development environment variable is set. This logic is described in the program.cs file:

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
     app.UseSwagger();
     app.UseSwaggerUI();
}

By default, the container is deployed in the Production environment. We can remove the if condition (app.Environment.IsDevelopment()) or add an environment variable to the Dockerfile:

ENV ASPNETCORE_ENVIRONMENT=Development
...

Now we need to rebuild the application:

docker build -t dotnet-app .

And run it again:

docker run -p 5000:80 dotnet-app

Swagger is now available:

Image10

Publishing Image to Docker Registry

So, we have a container image and want to use it for deployment on the server. To do this, we need to send this image to the server. Docker registry is suitable for these purposes. 

  1. Go to the https://hub.docker.com

  2. Log in and click on the "Create Repository" button

  3. Enter the repository name and save it.

  4. Now, go back to the terminal and run the command:

docker login

Image4

  1. To send a container image to the repository, first build the image, indicating your login in the name:

docker build -t [your login]/dotnet-app .
  1. And run the command:

docker push [your login]/dotnet-app:latest

Image6

Deploying the Application on the Server

First, let's rent a virtual server on Hostman. If you don't have an account yet, create it on hostman.com.

Next, log into your control panel and go to Cloud servers - Create.

You will need Docker installed on your server. You can install it manually or select a Docker image in the Marketplace tab. In this case, Hostman will install Docker automatically.

Image12

Next, select your server's parameters. For our template app, the minimal configuration is sufficient, but you may need more resources for running your actual application.

After the server is installed, you will find the access details on the Dashboard.

Image14

Connect to the server via SSH and run the docker login command to log in to the registry.

Next, run the command:

docker pull [your login]/dotnet-app

Image7

And launch our application:

docker run -p 5000:80 [your login]/dotnet-app -d

Image5

The -d option is used to run the container in the background.

Now, we can access our API at the IP address of the server:

Image2

Conclusion

We learned how to deploy an ASP.NET Core application to Docker, publish it to the Docker registry, and run it on a VPS. For a more complete dive, we recommend reading the Microsoft documentation.


Share