Getting Started with Docker-Compose for ASP.NET Core and MSSQL

Introduction

In today's software development landscape, containerization has become an essential practice for managing and deploying applications efficiently. Docker, with its ability to package applications and their dependencies into isolated containers, simplifies the development, testing, and deployment process. When working with ASP.NET Core and MSSQL, Docker Compose further streamlines these tasks by defining multi-container applications. In this article, we'll walk through setting up a Docker Compose environment for an ASP.NET Core application and an MSSQL database.

Prerequisites

Before we begin, ensure you have the following installed on your development machine:

Step 1. Create an ASP.NET Core Application

First, let's create a new ASP.NET Core application. Open your terminal and run the following commands:

dotnet new webapi -o AspNetCoreDocker
cd AspNetCoreDocker

This will create a new ASP.NET Core Web API project in the AspNetCoreDocker directory.

Step 2. Add a Dockerfile

Next, add a Dockerfile to define how the ASP.NET Core application should be built and run inside a container. Create a file named Dockerfile in the project root and add the following content:

# Use the official ASP.NET Core runtime as a parent image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

# Use the SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["AspNetCoreDocker.csproj", "."]
RUN dotnet restore "AspNetCoreDocker.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "AspNetCoreDocker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "AspNetCoreDocker.csproj" -c Release -o /app/publish

# Copy the build output to the runtime image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AspNetCoreDocker.dll"]

Step 3. Add a Docker Compose File

Now, let's create a Docker Compose file to define the multi-container application. Create a file named docker-compose.yml in the project root and add the following content:

version: '3.4'

services:
  web:
    image: aspnetcoredocker
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5000:80"
    depends_on:
      - db

  db:
    image: mcr.microsoft.com/mssql/server:2019-latest
    environment:
      SA_PASSWORD: "Your_password123"
      ACCEPT_EULA: "Y"
    ports:
      - "1433:1433"

Step 4. Configure the ASP.NET Core Application to Use MSSQL

Update the appsettings.json file in the ASP.NET Core project to configure the connection string for the MSSQL database:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=db;Database=master;User=sa;Password=Your_password123;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Next, update the Startup.cs file to use the connection string:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

Step 5. Build and Run the Application with Docker Compose

With everything set up, it's time to build and run the application using Docker Compose. In the terminal, run the following command:

docker-compose up --build

Docker Compose will build the ASP.NET Core application image, pull the MSSQL image, and start both containers. The ASP.NET Core application will be accessible at http://localhost:5000, and the MSSQL database will be running on localhost:1433.

Step 6. Verify the Setup

To verify that the setup is working correctly, you can create a simple controller in the ASP.NET Core application that connects to the MSSQL database and performs basic operations. For example, you can create a WeatherForecastController that retrieves data from the database.

Conclusion

Docker Compose makes it easy to manage multi-container applications, and with the steps outlined in this article, you can set up a robust development environment for your ASP.NET Core application and MSSQL database. By containerizing your application, you ensure consistency across different environments and streamline the deployment process. Happy coding!