Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’4 min read
πŸš€ Containerizing a .NET 8 Application with SQL Server using Docker
M

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


🧱 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:

πŸ§‘β€πŸ’» 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:

  1. Check Backup Files
    Use sqlcmd to 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'"
    
  2. 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.18 with 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 .
 docker run -d -p 5000:8080 --name gradlink-app --link sql1 gradlink-app

πŸ”„ 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


🏁 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:

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.

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.