1

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?

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • did you connect to your docker container and check if file really exists? for example using `docker container exec -it container_Your ls /var/task` – Mykyta Halchenko Jun 01 '22 at 14:41
  • hm, interesting. When I list containers /var/task dir there is not MyFile.mp4. Dll and json files only. – user1765862 Jun 01 '22 at 14:56

1 Answers1

0

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

Mykyta Halchenko
  • 720
  • 1
  • 3
  • 21