React and .NET Core 6.0 Sample Project with Docker - Part 3
Tags: docker, kubernetes, beginners
Published: 2022-10-25 10:22:46

In this article we are going to Dockerize ASP.NET Core with SQL Server into micro-services.

Please refer to my previous article to create ASP.NET Core Web API.

[React and .NET Core 6.0 Sample Project with Docker -Part 1](https://dev.to/jayavardhan5555/react-and-net-core-60-sample-project-with-docker-part-1-562i)

The sections of this post will be as follows:

Create the Docker file
Writing docker compose
Configure Web API to use SQL Server
Build and Run Docker Compose
Test the Web API End Points

To follow along with this tutorial, you need to have:

- [Docker Desktop ](https://www.docker.com/products/docker-desktop/)

- A Windows, Linux, or Mac machine

If you are ready, let’s get started.

Before this make sure that docker engine is up and running. Open a terminal and run.

`docker info`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ucgugs3nthjm3wdsk9l.png)

1) Create the Docker file

We need to create a docker file within our root directory to build and run the Web API as a image.

```c#
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MoviesAPI/MoviesAPI.csproj", "MoviesAPI/"]
RUN dotnet restore "MoviesAPI/MoviesAPI.csproj"
COPY . .
WORKDIR "/src/MoviesAPI"
RUN dotnet build "MoviesAPI.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MoviesAPI.dll"]
```
This file is responsible for instructing Docker how we want to build our Web API image. Once we build this file it will fetch the .NET Core image and add the all required dependencies and copy it to app/publish folder and start the application at MoviesAPI.dll 

2) Writing docker compose 

We will add docker compose file by selecting container orchestrator support for the application. Please follow the below steps to add docker compose file.

Application => Right Click => Add => Select container orchestrator support.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc6rh4or4i16fqt8vus2.jpg)

select Docker Compose option and Click on Ok.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byk27kdd5d865qb9su7d.png)

Select Docker support options as Linux and click on Ok.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m42m6ju8y6ur60ayin54.png)

Add the following code to docker compose and override file to download the sql server docker image and run the movies db on port 1433.

docker-compose.yml

```c#
version: '3.4'

services:
  moviesapi:
    image: moviesapi
    build:
      context: .
      dockerfile: MoviesAPI/Dockerfile

  db:
    image: "mcr.microsoft.com/mssql/server:2017-latest"
```
docker-compose.override.yml

```yaml
version: '3.4'

services:
  moviesapi:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "80"

  db:
    container_name: Movies
    environment:
        SA_PASSWORD: "Password123"
        ACCEPT_EULA: "Y"
    restart: always
    ports:
        - "1433:1433"
```
3) Configure Web API to use SQL Server

Update the appsettings.json file.

```c#
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MovieConnection": "Server=Movies;Database=Movies;User Id= sa;Password=Password123;"
  }
}
```
Add the following logic for EF Core migration. after builder.Build() in Program.cs

```c#
if(app.Environment.IsDevelopment())
{
    using(var scope = app.Services.CreateScope())
    {
        var movieContext = scope.ServiceProvider.GetRequiredService<MovieContext>();
        movieContext.Database.EnsureCreated();
        
    }
}

```
4) Build and Run Docker Compose

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04p0nmqqz28qxhhlthgy.png)

Now go to Docker desktop and click on containers. Here we can see docker container up and running. 

Movies Web API running on 56711 port and Movies db is running on 1433 port.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrrr0kp9xdl9so52oo71.png)

5) Test the Web API End Points

We will test POST and GET requests to verify the functionality like we tested in part 1.

Go to Postman and create a new http request.

POST Method:

> http://localhost:port/api/movies/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvtk2nii24g1dm4bq4ta.png)

GET Method:

> http://localhost:port/api/movies/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7g5x0dn0cusoyjk76pyu.png)

Conclusion:

If we install SQL Server on our local machines and then try to run the application will take a lot of time and disk space. If we the containerized databases it will be easy to start the database and also we can delete with in seconds.

Thanks for reading!

Please leave a comment if you fine this helpful. Any feedback or suggestions are welcome