# 🚀 Containerizing a .NET 8 Application with SQL Server using Docker

> **GitHub Repository:** [https://github.com/musayyab-ali/GradLink\_App](https://github.com/musayyab-ali/GradLink_App)

[In this blog, I will walk you through how](https://github.com/musayyab-ali/GradLink_App) I containerized a .NET [8 application that connects to a SQL Server](https://github.com/musayyab-ali/GradLink_App) (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
    
* .NET 8 [SDK (for local development and testing)](https://github.com/musayyab-ali/GradLink_App)
    

---

## [🧱 Step 1: Set Up MSSQL Database in Docker](https://github.com/musayyab-ali/GradLink_App)

[We start by c](https://github.com/musayyab-ali/GradLink_App)reating [a SQL Server container using the following command:](https://github.com/musayyab-ali/GradLink_App)

```plaintext
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 volum[e `sql1data`](https://github.com/musayyab-ali/GradLink_App)
    
* [Maps port 1433 to your local system for access](https://github.com/musayyab-ali/GradLink_App)
    
* [Au](https://github.com/musayyab-ali/GradLink_App)tomatically [restarts unless manually stopped](https://github.com/musayyab-ali/GradLink_App)
    

## 🧑‍💻 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:
    
    ```plaintext
    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:
    
    ```plaintext
    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](https://github.com/musayyab-ali/GradLink_App) 3: Up[date Connection String in `appsettings.json`](https://github.com/musayyab-ali/GradLink_App)

Before [building the .NET container, make sure your](https://github.com/musayyab-ali/GradLink_App) `GradLink/` pro[ject’s `appsettings.json` contains the correct](https://github.com/musayyab-ali/GradLink_App) connection string:

```plaintext
{
  "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 cus](https://github.com/musayyab-ali/GradLink_App)tom network.

---

## 📦 Step 4: Create the Dockerfile

Here's the `Dockerfile` used to [build the .NET app container:](https://github.com/musayyab-ali/GradLink_App)

```plaintext
# 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](https://github.com/musayyab-ali/GradLink_App)

```plaintext
cdocker build -t gradlink-app .
```

### [🚀 Run the Container and Link to SQL](https://github.com/musayyab-ali/GradLink_App) Server

```plaintext
 docker run -d -p 5000:8080 --name gradlink-app --link sql1 gradlink-app
```

* Application will be available at [`http://localhost:8080`](http://localhost:8080)
    
* SQL Se[rver is accessible inside the container as `s`](https://github.com/musayyab-ali/GradLink_App)`ql1`
    

---

## 🔄 Per[sistent Volume for SQL Server](https://github.com/musayyab-ali/GradLink_App)

[The `-v sql1da`](https://github.com/musayyab-ali/GradLink_App)`ta:/var/opt/ms`[`sql` volume ensures that your database data persists even if the container restart](https://github.com/musayyab-ali/GradLink_App)s or is recreated.

---

## ✅ Final Checklist

* ✅ MSSQL container up and running ✔️
    
* ✅ `ap`[`psettings.json` updated ✔️](https://github.com/musayyab-ali/GradLink_App)
    
* [✅ .NET app container built and running ✔️](https://github.com/musayyab-ali/GradLink_App)
    
* [✅ Persistent volume for DB ✔️](https://github.com/musayyab-ali/GradLink_App)
    

---

## [🏁 Conclusion](https://github.com/musayyab-ali/GradLink_App)

[You’ve success](https://github.com/musayyab-ali/GradLink_App)f[ully containerized a .NET 8 web application with a linked SQL Server instance. This setup is](https://github.com/musayyab-ali/GradLink_App) 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:

```plaintext
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.
