This documentation explains how to build a project of .NET 6.0 on an Ubuntu machine using Docker and Nginx as the reverse proxy.
First and foremost, create a folder for your project and move into it using:
mkdir dotnet-docker-setup && cd dotnet-docker-setup
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
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 application
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
# Copy everything to the container
COPY . .
# Restore dependencies and build the application
RUN dotnet restore
RUN dotnet publish -c Release -o /app/out
# Stage 2: Run the applicationFROM
mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
# Copy the published output from the build stage
COPY --from=build /app/out .
# Configure the port for your app
EXPOSE 5000
# Application Startup
ENTRYPOINT [ "dotnet", "MyWebApp.dll" ]
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-network
networks:
app-network:
driver: bridge
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;
}
}
Build and start your Docker containers using Docker Compose:
docker-compose up --build -d
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.