In the video above, walk you through how to containerize a .NET app and SQL Server database using Docker – from start to finish.
We cover Dockerfiles, Docker Compose, Docker Hub, and the core containerization concepts that every .NET developer should understand.
In this tutorial, you’ll learn:
- What Docker, Containers, Images, and Dockerfiles actually are
- Why containers make .NET development faster and more consistent
- How to build and run your own .NET container image
- How to connect it to a SQL Server container
- How to use Docker Compose to run them both with a single command
- How to persist your SQL data using volumes
- How to keep your passwords secure with environment variables
By the end, you’ll have a fully containerized .NET app that runs identically on any machine – Windows, macOS, or Linux.
Dockerfile
Below is the Dockerfile created in the video.
This file defines each step required to build, publish, and run your .NET app inside a container.
It uses a multi-stage build to keep the final image small, clean, and production-ready.
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["BookMvcApp.csproj", "./"]
RUN dotnet restore "BookMvcApp.csproj"
COPY . .
RUN dotnet build "./BookMvcApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BookMvcApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BookMvcApp.dll"]How it works
- Base stage – Defines the runtime environment (ASP.NET 9 image).
- Build stage – Uses the SDK to compile the app and restore dependencies.
- Publish stage – Packages the app into a ready-to-run format.
- Final stage – Produces the minimal runtime image with only published output.
This pattern ensures smaller, faster, and more secure images.
Docker Compose File
Next, we create a docker-compose.yml file to run both the web app and SQL Server containers together.
version: '3.9'
services:
web:
build: .
ports:
- "8080:8080"
depends_on:
- db
environment:
- ConnectionStrings__DefaultConnection=Server=db;Database=BookDb;User Id=sa;Password=${DB_PASSWORD};TrustServerCertificate=True;
db:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=${DB_PASSWORD}
ports:
- "1433:1433"
volumes:
- mssql-data:/var/opt/mssql
volumes:
mssql-data:Key points
- The
webservice builds your .NET app using the Dockerfile. - The
dbservice pulls Microsoft’s official SQL Server 2022 image from Docker Hub. ${DB_PASSWORD}is read from your local.envfile, keeping credentials out of version control.- The named volume
mssql-datapersists your database data even after containers are stopped.
With this setup, you can spin up both containers – app and database – in one command:
docker compose up --buildFinal Thoughts
This simple setup gives you a portable, reproducible, and production-ready development environment for any .NET application.
You can now:
- Onboard new developers in minutes
- Run your app identically across all machines
- Prepare for easy cloud deployments

No responses yet