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, @"wwwroot\Db\product.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/wwwroot\Db\product.json'. File name: '/app/wwwroot\Db\product.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?
CodePudding user response:
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/wwwroot\Db\product.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
- public static string Combine(string path1, string path2, string path3)
- public static string Combine(string path1, string path2, string path3, string path4)
- public static string Combine(params string[] paths)
Your code: Path.Combine(_env.ContentRootPath, @"wwwroot\Db\product.json");
Would then become something like: Path.Combine(basePath, "wwwroot", "Db", "product.json");