skip to Main Content

It is my first attempt to run a docker image in my asp.net core 5.0 application. I have added the following configuration in the docker file.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Shopping.Client.dll"]

I am trying to access the files that are in my wwwroot folder, using the following code:

private async Task<T> ReadAsync<T>(string filePath)
{

    using FileStream stream = File.OpenRead(filePath);
    return await JsonSerializer.DeserializeAsync<T>(stream);
}

public async Task<List<Product>> GetProducts()
{
    var filePath = System.IO.Path.Combine(_env.ContentRootPath, @"wwwrootDbproduct.json");
    return await ReadAsync<List<Product>>(filePath);
}

When I run the application and trying to access the files in wwwroot I am getting the error as:

An unhandled exception has occurred while executing the request.
System.IO.FileNotFoundException: Could not find file
‘/app/wwwrootDbproduct.json’. File name:
‘/app/wwwrootDbproduct.json’

Should I include anything special in the docker file to copy the wwwroot folder or do I need to add the file path considering the path in the docker image?

2

Answers


  1. It appears that you are using Linux containers and using Windows file path conventions. Path.Combine does support this, but it needs some help.

    /app/wwwrootDbproduct.json

    Try wwwroot/Db/product.json

    If this is your issue, you may wish to examine the overloads for Path.Combine
    as the Combine method has additional overloads that accept additional parameters

    1. public static string Combine(string path1, string path2, string path3)
    2. public static string Combine(string path1, string path2, string path3, string path4)
    3. public static string Combine(params string[] paths)

    Your code:
    Path.Combine(_env.ContentRootPath, @"wwwrootDbproduct.json");

    Would then become something like:
    Path.Combine(basePath, "wwwroot", "Db", "product.json");

    Login or Signup to reply.
  2. Please try by Try..Catch block.
    For example:

    public async Task<List<Product>> GetProducts()
    {
      try 
      {
          var filePath ="wwwroot//Db//product.json";
        return await ReadAsync<List<Product>>(filePath);
      }
      catch (Exception e)
      {
        //  Block of code to handle errors
      return List<Product>();
      }
    
    }
    

    and for the run container, you can use these commands

    Use volume in :
    You can see the volumes list in this path:(on the window)

    wsl.localhostdocker-desktop-datadatadockervolumes

    > docker run -v volumeName:/app/wwwroot/Db -p 7778:80 imagename 
    

    Or you can use the mount path:(not recommended)
    for example, you want to save in c:Db

    > docker run -v c:/db:/app/wwwroot/Db -p 7778:80 imagename 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search