π Containerizing a .NET 8 Application with SQL Server using Docker

Hey there! I'm currently working as an Associate DevOps Engineer, and I'm diving into popular DevOps tools like Azure Devops,Linux, Docker, Kubernetes,Terraform and Ansible. I'm also on the learning track with AWS certifications to amp up my cloud game. If you're into tech collaborations and exploring new horizons, let's connect!
GitHub Repository: https://github.com/musayyab-ali/GradLink_App
In this blog, I will walk you through how I containerized a .NET 8 application that connects to a SQL Server (MSSQL) database. The project was cloned from the above GitHub repository and customized to run smoothly inside Docker containers using a persistent volume for the database.
π οΈ Prerequisites
Docker installed on your machine
π§± Step 1: Set Up MSSQL Database in Docker
We start by creating a SQL Server container using the following command:
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=hassan@123" \
-p 1433:1433 --name sql1 --hostname sql1 \
-v sql1data:/var/opt/mssql \
--restart unless-stopped \
-d mcr.microsoft.com/mssql/server:2022-latest
π What this does:
Sets up SQL Server with a persistent volume
sql1dataAutomatically restarts unless manually stopped
π§βπ» Step 2: Restore SQL Server Database from Backup
If you have an existing database backup file (e.g., newGradLink.bak), you can restore it to your SQL Server container using the following commands:
Check Backup Files
Usesqlcmdto verify the contents of the backup file:sqlcmd -S localhost -U sa -P 'hassan@123' -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/newGradLink.bak'"Restore Database
After verifying, restore the database using this command:sqlcmd -S localhost -U sa -P 'hassan@123' -Q " RESTORE DATABASE gradlink FROM DISK = '/var/opt/mssql/backup/newGradLink.bak' WITH MOVE 'GradLink' TO '/var/opt/mssql/data/gradlink.mdf', MOVE 'GradLink_log' TO '/var/opt/mssql/data/gradlink_log.ldf', REPLACE;"
π Note: Make sure the paths (e.g.,
/var/opt/mssql/backup/) in the SQL Server container are valid and the backup file is placed in the correct directory.
βοΈ Step 3: Update Connection String in appsettings.json
Before building the .NET container, make sure your GradLink/ projectβs appsettings.json contains the correct connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=xxxxxxxx; Initial Catalog=GradLink; User ID=sa; Password=hassan@123; MultipleActiveResultSets=True;TrustServerCertificate=True; Integrated Security=False;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
π Replace
172.21.114.18with your Docker bridge IP or the IP of the SQL Server container if youβre in a custom network.
π¦ Step 4: Create the Dockerfile
Here's the Dockerfile used to build the .NET app container:
# Use .NET 8 SDK for build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
# Copy csproj files and restore as distinct layers
COPY GradLink/GradLink.csproj GradLink/
COPY GradLink.Model/GradLink.Model.csproj GradLink.Model/
COPY GradLink.Repository/GradLink.Repository.csproj GradLink.Repository/
RUN dotnet restore GradLink/GradLink.csproj
# Copy everything else and build
COPY . .
RUN dotnet publish GradLink/GradLink.csproj -c Release -o out
# Runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "GradLink.dll"]
π οΈ Step 5: Build and Run the .NET Container
β Build the Docker Image
cdocker build -t gradlink-app .
π Run the Container and Link to SQL Server
docker run -d -p 5000:8080 --name gradlink-app --link sql1 gradlink-app
Application will be available at
http://localhost:8080
π Persistent Volume for SQL Server
The -v sql1data:/var/opt/mssql volume ensures that your database data persists even if the container restarts or is recreated.
β Final Checklist
β MSSQL container up and running βοΈ
π Conclusion
Youβve successfully containerized a .NET 8 web application with a linked SQL Server instance. This setup is great for local development, testing, or preparing your app for cloud/container orchestration platforms like Kubernetes.
π Important Notes:
1. π Use a Custom Docker Network (Recommended)
To ensure both containers can talk to each other by name:
bashCopyEdit# Create a custom network
docker network create gradlink-network
# Run SQL Server container on this network
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=hassan@123" \
-p 1433:1433 --name sql1 --hostname sql1 \
-v sql1data:/var/opt/mssql \
--network gradlink-network \
--restart unless-stopped \
-d mcr.microsoft.com/mssql/server:2022-latest
# Run your .NET app container on the same network
docker run -d -p 8080:80 --name gradlink-app \
--network gradlink-network \
gradlink-app
This setup allows your .NET app to reach SQL Server via sql1 instead of needing an IP address.



