📦 Docker Setup for .NET 8 Web API Project
This document describes how to create a Docker image and run a container for a .NET 8 Web API using a multi-stage Dockerfile.
Docker file in visual studio proj
Docker Image
Docker Container
You can have as many container you want for an image
🛠 Dockerfile Breakdown
# Stage 1: Build Environment
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
# 👉 Using .NET SDK 8.0 image for build
WORKDIR /App
# 👉 Set working directory inside container to /App
COPY . ./
# 👉 Copy all files from the current directory to /App inside container
RUN dotnet restore
# 👉 Restore all NuGet packages based on the .csproj file
RUN dotnet publish -c Release -o out
# 👉 Build and publish the app to the /App/out folder in Release mode
# Stage 2: Runtime Image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
# 👉 Use a lighter ASP.NET runtime image for running the app
WORKDIR /App
# 👉 Set working directory again to /App in the runtime image
COPY --from=build-env /App/out .
# 👉 Copy published output from the build stage
EXPOSE 8085
# 👉 Expose port 8085 in the container (used by the app)
ENTRYPOINT ["dotnet", "DemoAPi.dll"]
# 👉 Start the app using dotnet CLI
🐳 Docker Commands
🔧 Build the Docker Image
docker build -t demoimage .
-t demoimage: Tags the image with name demoimage.
.: Refers to the current directory where Dockerfile exists.
🚀 Run the Docker Container
docker run -d --name demo-c -p 8080:8085 demoimage
-d: Detached mode (runs in background).
--name demo-c: Container name is demo-c.
-p 8080:8085: Maps port 8080 on host to port 8085 inside container.
demoimage: Name of the image to use for the container.
🌐 Test the API
Open your browser or use a tool like Postman to test:
http://localhost:8085/WeatherForecast/
📝 Summary
Multi-stage Dockerfile ensures smaller final image.
App is built and published in one stage, and only output is copied to runtime image.
Use port mapping to expose your app externally.
You can create multiple containers from the same image anytime.
---Rough note in hindi
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
# Bhai dotnet sdk 8.0 le aa
WORKDIR /App
# saare cheeze app folder main daalo
# Copy everything to app folder
COPY . ./
# Restore as distinct layers//Aab proj file se sab dll restore kar with exact version
RUN dotnet restore
# Build and publish a release---App folder ke andar outfolder hain usme dal to release code
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
# sdk bana ke
WORKDIR /App
# app folder main saara code alread hain upar command ne kiya tha
COPY --from=build-env /App/out .
# ek port expose karna hoga jaise agar local host call hoga tab kaun se port pe call jayega woh
EXPOSE 8085
# JAB BHI LOCAL HOST SE CALL KAROGE 8085 PORT PE TAB YE APPLICATION RUN HOGA
# first line wale build env se sab kuch app out main past e hoga
ENTRYPOINT ["dotnet", "DemoAPi.dll"]
# Project name
#docker build -t demoimage .
#eska matlab . represt in current directory docker file.
#run this in docker termial it will create image
# now you have created an image for a project . Aab chao to 1000000 container bana do es image ke. Jitne chahe utne
# Now so as to create container we need to run docker run -d --name demo-c -p 8080:8085 demo-image
# here you are saying docker please run dettach mode container name (any) which port you want run(localmhost)
#and also docker host configured in project and also name of the image you created above
# Now container in ready.
#So as to test run in terminal start "http://localhost:8080/WeatherForecast/"
Top comments (0)