Diving into the Future: Navigating .NET Core Deployment with Docker

Simplify versioning while mastering portability and scalability

Pratik Upadhyay
Simform Engineering
5 min readSep 1, 2023

--

In this blog, we will learn how to deploy a .NET core web application in a Docker container. But, first, let’s understand the basics:

What is Docker, and why is it needed?

Docker is an open-source container platform that simplifies building, deploying, and managing applications across multiple environments: development, testing, staging, and production Ion.

Docker allows a developer to package software applications into independent containers. You can run Docker-based applications on any platform that supports Docker. Developing applications in ASP.NET 6 — and then deploying them in Docker containers — can provide programmers with several benefits, including being able to work with multiple containers and easily share your applications between development and production environments.

Benefits of Docker:

  1. Portability: Containers are self-contained. Hence, they can run on any platform that supports Docker.
  2. Scalability: Additional “orchestration” utilization can spin up multiple container instances to support a greater load.
  3. Performance: Containers generally perform better than their VM counterparts.

What is containerization?

Container is a piece of software that contains the code and dependencies required to run an application within its own environment. As a result, containers are isolated from one another despite sharing the same host operating system.

They are portable and facilitate resource sharing among applications. Think of them as shipping containers for applications.

When you create a new application instance from the image, it gains access to the contained files. This isolation ensures that issues in one instance don’t affect others.

What is a Docker image?

A Docker image is your code’s execution blueprint containing dependencies and startup instructions.

General Overflow of Docker
General Overflow of Docker

How to deploy a .NET core web application in a Docker container

Step 1: Install Prerequisites

Download the Docker desktop client for your operating system from these links:

  1. Docker for Windows
  2. Docker for Linux

Please note that Docker desktop features vary depending on your OS and subscription plan. Here’s what it looks like when installed:

Step 2: Create the ASP.NET Core Application

1. Open the Visual Studio IDE.

2. Click on “Continue without code”

3. Navigate to File -> New -> Project

4. Select the “ASP.NET Core Web API” project template. Click Next to move on to the next screen.

5. Specify the project name and disk location. Check “Place solution and project in the same directory” for simplicity. Click Next to proceed.

6. Choose your target framework on the “Additional Information” screen (e.g., .NET 7.0).

Select the authentication type or “None” if not needed. Make sure to enable Docker.

7. Click “Create” to finish. Your new ASP.NET web application project will be created in your chosen location.

Success! Your project is ready.

Step 3: Open the Docker File

Double-click the Docker file to access auto-generated code from Visual Studio, with pre-configured steps for creating and running the Docker image in multiple stages.

Here’s how:

  1. Base: This is the starting point for your Docker image. Start with a minimal, lightweight image providing essential runtime tools.
  2. Build: This is where you perform any necessary application compilation, building, and packaging. Add your code and dependencies, and compile if needed (e.g., for .NET Core).
  3. Publish: Optimize your app for runtime by creating a deployment-ready version.
  4. Final Image: This is the image you will run as a container. Run this lightweight image with all the necessary files and dependencies.

FROM: Specifies the base image.

RUN: Executes commands during the image build to install software, update packages, etc.

COPY and ADD: Copies files from the host machine to the image.

ENV: Sets environment variables for the container.

EXPOSE: Documents which ports the container will listen on.

CMD and ENTRYPOINT: Specifies the command that runs when the container starts.

Here is how the Dockerfile looks in Visual Studio:

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Demo_Docker.csproj", "."]
RUN dotnet restore "./Demo_Docker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Demo_Docker.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Demo_Docker.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY - from=publish /app/publish .
ENTRYPOINT ["dotnet", "Demo_Docker.dll"]

Step 4: Deploy an ASP.NET App with Docker

  • Create a Docker file to deploy your ASP.NET app.
  • Use ‘docker build’ to generate a container image. Open PowerShell as administrator to do this.

Command: Docker build -t demo_docker .

Docker image created successfully in Docker

Step 5: Run the Docker Image

To execute the Docker image, run this command in PowerShell:

Command: docker run -d -p 8080:80 — name demo_docker_container demo_docker

Run docker image successfully
Application Started

GitHub: Docker Demo

Conclusion

In this blog, we saw how to deploy your.NET application in a docker container independent of the operating system.

Docker simplifies version control, ideal for microservices, and the future of .NET Core deployment via Docker promises to reshape our approach to application deployment.

Stay updated on the latest insights with the Simform Engineering blog.

For more updates, connect with us on Twitter and LinkedIn

--

--