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
It appears that you are using Linux containers and using Windows file path conventions. Path.Combine does support this, but it needs some help.
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
Your code:
Path.Combine(_env.ContentRootPath, @"wwwrootDbproduct.json");
Would then become something like:
Path.Combine(basePath, "wwwroot", "Db", "product.json");
Please try by Try..Catch block.
For example:
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
Or you can use the mount path:(not recommended)
for example, you want to save in c:Db