React and .NET Core 6.0 Sample Project with Docker - Part 3

jayavardhan5555

Jayavardhan Reddy J

Posted on October 25, 2022

React and .NET Core 6.0 Sample Project with Docker - Part 3

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

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:

If you are ready, let鈥檚 get started.

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

docker info

Image description

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.



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"]


Enter fullscreen mode Exit fullscreen mode

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

select Docker Compose option and Click on Ok.

Image description

Select Docker support options as Linux and click on Ok.

Image description

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



version: '3.4'

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

  db:
    image: "mcr.microsoft.com/mssql/server:2017-latest"


Enter fullscreen mode Exit fullscreen mode

docker-compose.override.yml



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"


Enter fullscreen mode Exit fullscreen mode

3) Configure Web API to use SQL Server

Update the appsettings.json file.



{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MovieConnection": "Server=Movies;Database=Movies;User Id= sa;Password=Password123;"
  }
}


Enter fullscreen mode Exit fullscreen mode

Add the following logic for EF Core migration. after builder.Build() in Program.cs



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

    }
}



Enter fullscreen mode Exit fullscreen mode

4) Build and Run Docker Compose

Image description

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

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

GET Method:

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

Image description

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

馃挅 馃挭 馃檯 馃毄
jayavardhan5555
Jayavardhan Reddy J

Posted on October 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related