Building a .NET API with SQL Server Backend on Docker: A Step-by-Step Guide
In today's software development landscape, containerization has become a cornerstone of building scalable and portable applications. Docker, a popular containerization platform, enables developers to encapsulate their applications and dependencies into portable containers. This guide will walk you through creating a .NET API with a SQL Server backend, all running smoothly inside Docker containers.
Why Use Docker for .NET API and SQL Server?
- Consistency Across Environments: Docker ensures that your application behaves the same way in development, testing, and production.
- Isolation: Containers isolate your application from the underlying system, reducing conflicts between dependencies.
- Scalability: Easily scale services up or down based on demand.
- Simplified Deployment: Docker makes deployment simpler by packaging everything into a single unit.
Prerequisites
Before we dive in, make sure you have the following tools installed on your machine:
- Docker: Download Docker Desktop for your operating system.
- .NET SDK: Download .NET SDK for your development needs.
- SQL Server Docker Image: We’ll use Microsoft’s official SQL Server Docker image.
Step-by-Step Guide
1. Set up SQL Server in docker.
- Run docker command to create a network: We willl be using this network for our sql server and apis.
- Run docker command to create and start the sql server container:
- Connect to the SQL Server instance: You should now be able to connect to
.\Express, 1439
with User Name assa
and password asMyPass@word
from your SQL Enterprise Manager.
- Create a database kaDb:
1. Create a .NET API Project
-
Open a Terminal and navigate to the directory where you want to create your project.
-
Create a new .NET Web API project using the following command:
This will create a new directory called
UsersAPI
with a basic Web API template. -
Navigate to the project directory:
-
Run the project locally to ensure it's working
Visit https://localhost:5001/api/WeatherForecast to see the default API response.
-
Create Dockerfile:
- Lets connect to the database:
Add connection strings to your appsettings.json file: Since this api will be running inside docker, within the same network
mynetwork
, we refer the sql server assqlserver
- Install Entity Framework Core for database access:
- Create a DbContext to interact with the database. Create a new file
KaDbContext.cs
:
- Update
Program.cs
to use the DbContext:
- Create a new UsersController:
- Build docker image:
- Run docker image:
- Navigate to: http:/localhost:9091/swagger
- Execute API: http:/localhost:9091/Users
Conclusion
By using Docker to containerize your .NET API and SQL Server, you've created a robust, portable, and consistent development environment. Docker not only simplifies the deployment process but also ensures that your application behaves consistently across different environments. This setup is just the beginning—Docker provides a powerful platform for scaling, managing, and deploying applications efficiently.