Containerizing Dotnet App With Sql Server Using Docker Compose


running dotnet core app in docker with postgres

In this tutorial, we are going to containerize the .NET Web API application with docker. I am assuming you are familiar with docker. At least, you should have some understandings of how docker works. However, I have covered all the steps needed to create a docker container for your application, but I am not going to cover the theoretical concepts of docker.

Last updated on:

  • 19-june-2025

🔨Tools needed

  • Visual Studio Code (Free)
  • .Net 9.0 SDK (Free)
  • Docker desktop (Free)

🧑‍💻Tech used

  • .Net 9.0 Web APIs (controller APIs)
  • Ms SQL Server 2022 (within a container)
  • Docker compose

🍵Note: I am using windows 11 operating system. However, I also have tested it in the Linux mint xia and it is working fine.

Why to chose docker compose?

General workflow of creating images and container without docker compose:

Without docker compose

You have to create and run containers separately, which involves typing all the commands manually in the terminal or bash each time.

With docker compose

with docker compose

All the steps to build the images and create the containers are defined in a a single file called compose.yaml . You just need to fire a single command to create and run multiple containers.

Let’s start with pulling the Github repo

To save time, I have already created the .net application and you can pull the Github repo to get the application.

git clone https://github.com/rd003/DotnetDockerDemo.git

Open this application in the visual studio code and follow the steps.

Creating a Dockerfile

First and foremost, create a file named ‘Dockerfile’ in the root directory. Make sure, Dockerfile does not have any extension.

docker file

Add the following content in the docker file.

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY \*.sln .
COPY DotnetDockerDemo.Api/\*.csproj ./DotnetDockerDemo.Api/
RUN dotnet restore

# copy everything else and build app
COPY DotnetDockerDemo.Api/. ./DotnetDockerDemo.Api/
WORKDIR /source/DotnetDockerDemo.Api
RUN dotnet publish -c release -o /app

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app
COPY --from=build /app ./

ENTRYPOINT ["dotnet", "DotnetDockerDemo.Api.dll"]

This Dockerfile contains the instruction to create a docker image of our .net application. It is needed to build a .net application image, which will run in a container.

Creating a compose.yaml file

Next, create a file name compose.yaml in the root directory and paste the following content there.

services:
  server:
    container_name: people-apis
    build: .
    image: people-api:1.0.0
    ports:
      - 8080:8080
    depends_on:
      - "sql"
  sql:
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    container_name: sql_server
    ports:
      - 1433:1433
    environment:
      - ACCEPT_EULA=y
      - MSSQL_SA_PASSWORD=p@55w0rd
    volumes:
      - sql-server-data:/var/opt/mssql

volumes:
  sql-server-data:
    name: sql-data

In this file, we have defined two services.

  • First, it will build a docker image of .net application named people-api:1.0.0 and create a container for it, which will listen on the port 8080.
  • Second, it will pull the sql server image from the docker hub, create the container for it which will listen in the port 1433. The image will be pulled only once; if you already have a sql server image with similar tag, it won’t be pull again.

I have created volume in sql server section.

volumes:
  sql-server-data:
  name: sql-data

I have create a volume named sql-server-data and using this volume as follow

volumes:
  - sql-server-data: /var/opt/mssql

This line mounts sql-server-data volume to the path /var/opt/mssql . By creating volumes our data persists outside the container. If we remove the container or recreate it, our data (database, tables, procedures…everything) will persist.

Note: The dockerfile and compose.yaml file is created at June,19,2025. The content present in dockerfile and compose file is valid as of now, but may not be valid if you are reading this blog post in distant future.

Update the appsettings.json

"ConnectionStrings": {
 "default": "Server=sql,1433;Database=DotnetDockerDemo;User Id=sa;Password=myStrong(!)Password;encrypt=false"
 }

You may have noticed, I have defined ‘server ’ as ‘sql,1433’. Here’s what it means:

  • sql represents the name of service defined in compose.yaml. Look at the snippet below:
// compose.yaml

 sql:  <==== It is the name, I am refering in connection string
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    container_name: sql_server
  • 1433 is the port of sql server which is running in a container. We have defined this port in thecompose.yaml file.

Run docker compose

We need to run the following command

docker compose up -d

-d flag indicates that container is running in the detached mode. This command will execute the compose.yaml file and create the container for the .net application and sql server.

As a result of the command, you should see something like this in your terminal.

docker running

To verify if container is running or not, run the following command:

docker ps -a

As a result, you should see something like this in your terminal.

docker ps -a

However, you can also verify it in the docker desktop.

docker desktop

Testing the application

To test the application, open a web browser and type the url [http://localhost:8080/weatherforecast](http://localhost:8080/weatherforecast). As a result, you should see the following response in the browser.

response1

This indicates that our application is working perfectly.

Testing the endpoints using sql server

Type the url [http://localhost:8080/api/people/](http://localhost:8080/api/people/) in the browser and as a result you might receive the person array.

Removing composed containers

docker compose down

💻 Code with Dockerfile and compose.yaml

I have created a separate branch which contains the dockerfile and compose.yaml file. To get the source code with these file, you need to checkout the branch container .

Url: https://github.com/rd003/DotnetDockerDemo/tree/container

To get this branch along in your cloned project, you need to run git checkout container.

Originally published by me at medium.com