DevOps

Set up .NET 6.0 Project on Ubuntu with Docker


Introduction

This documentation explains how to build a project of .NET 6.0 on an Ubuntu machine using Docker and Nginx as the reverse proxy. 

Step 1: Set Up the Project Directory

First and foremost, create a folder for your project and move into it using:

mkdir dotnet-docker-setup && cd dotnet-docker-setup

 

Step 2: Create Your .NET Project

If you do not have a .NET project, you can create a new one with the .NET CLI SDK.

dotnet new web -n MyWebApp

Then move into the project folder:

cd MyWebApp

Step 3: Create a Dockerfile

Navigate to the root of the project (where the .csproj file is located), and create a file named Dockerfile:

nano Dockerfile

 

Add the following content to your Dockerfile:

# Stage 1: Build the applicationFROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /app # Copy everything to the containerCOPY . . # Restore dependencies and build the applicationRUN dotnet restoreRUN dotnet publish -c Release -o /app/out # Stage 2: Run the applicationFROMmcr.microsoft.com/dotnet/aspnet:6.0 AS runtime WORKDIR /app # Copy the published output from the build stageCOPY --from=build /app/out . # Configure the port for your appEXPOSE 5000 # Application StartupENTRYPOINT [ "dotnet", "MyWebApp.dll" ]

 

Step 4: Create a Docker Compose File

Move to the root directory (dotnet-docker-setup) and create a docker-compose.yml file:

nano docker-compose.yml

 

Add the following content:

version: '3.8'services: webapp: build: context: ./MyWebApp dockerfile: Dockerfile ports: - "5000:5000" networks: - app-network nginx: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf depends_on: - webapp networks: - app-networknetworks: app-network: driver: bridge

 

Step 5: Create an Nginx Configuration File

Create a custom Nginx configuration file in the root directory:

nano nginx.conf

 

Add the following content:

server { listen 80; server_name your-domain.com www.your-domain.com; location / { proxy_pass http://webapp:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }}

 

Step 6: Build and Run the Containers

Build and start your Docker containers using Docker Compose:

docker-compose up --build -d

 

Step 7: Verify the Application

Once the containers are running, open your browser and navigate to:

http://<your-server-ip>

 

You should see your .NET application running.

 

Ready to transform your business with our technology solutions? Contact Us  today to Leverage Our DevOps Expertise. 

0

Devops

Related Center Of Excellence