Home > database >  access to file from the project root inside dockerized solution
access to file from the project root inside dockerized solution

Time:06-02

Dockerfile

FROM public.ecr.aws/lambda/dotnet:6 AS base
FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim as build
WORKDIR /src
COPY ["AWSServerless.csproj", "AWSServerless/"]
RUN dotnet restore "AWSServerless/AWSServerless.csproj"

WORKDIR "/src/AWSServerless"
COPY . .
RUN dotnet build "AWSServerless.csproj" --configuration Release --output /app/build

FROM build AS publish   
RUN dotnet publish "AWSServerless.csproj" \
            --configuration Release \ 
            --runtime linux-x64 \
            --self-contained false \ 
            --output /app/publish \
            -p:PublishReadyToRun=true  

FROM base AS final
WORKDIR /var/task

CMD ["AWSServerless::AWSServerless.LambdaEntryPoint::FunctionHandlerAsync"]
COPY --from=publish /app/publish .

Project source structure

AWSServerless
   Controllers
      ValuesController.cs
   appsettings.json
   Dockerfile
   Startup.cs
   MyFile.mp4

from the controller I'm trying to access this MyFile.mp4 file

var image = Image.FromFile("/var/task/MyFile.mp4"); 

but I'm getting

System.IO.FileNotFoundException: /var/task/MyFile.mp4

I have tried with other paths

var image = Image.FromFile("/src/MyFile.mp4"); 
var image = Image.FromFile("/src/AWSServerless/MyFile.mp4"); 

None of this work (System.IO.FileNotFoundException). What I'm doing wrong here?

CodePudding user response:

I had the same problem, this is because this file is external and is not included inside the publish process, so from my case you can check if there is this file after you publish via connecting to the docker command: docker container exec -it container_Your_name ls /var/task

and if there is no this file, the problem is in your publish command.

I guess you need to include your external files inside your publish, for example:

In the properties of the file you can set Copy to output directory to Copy always or you can edit the solution file, expand the xml tag of the file needed and add <CopyToOutputDirectory>Always</CopyToOutputDirectory> as sub-tag.

Or you can do it manually in your XML:

<ItemGroup>
    <Content Include="AppData\**" CopyToPublishDirectory="PreserveNewest"/>
  </ItemGroup>

It helped me, otherwise you can try this approach: https://stackoverflow.com/a/55510740/17239546

  • Related