Home > Net >  How to dockerize .NET6 AWS Lambda project with dependent class library projects
How to dockerize .NET6 AWS Lambda project with dependent class library projects

Time:05-25

I have four projects in one solution:

  • App.Data - class library
  • App.Core - class library
  • App.LambdaOne - AWS Lambda project
  • App.LambdaTwo - AWS Lambda project

The solution structure looks like this:

Solution:

  • App.Data
  • App.Core
  • App.LambdaOne
    • Dockerfile
  • App.LambdaTwo
    • Dockerfile

I am trying to create two docker images for App.LambdaOne and App.LambdaTwo, so I can deploy them separately.

So far, I have got a simple lambda project without any dependent project works using the Dockfile below.

Docker command in App.LambdaSimple's directory

docker build -t LambdaSimpleImage .

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 ["App.LambdaSimple.csproj", "App.LambdaSimple/"]
RUN dotnet restore "App.LambdaSimple.csproj"

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

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

FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
CMD ["App.LambdaSimple::App.LambdaSimple.Function::FunctionHandler"]

But the tricky part is that App.LambdaOnedepend on App.Data and App.Core. I tried to modify the working sample to deploy App.LambdaOne, but no luck so far. By running the same Docker command. Errors occurred, I have added those to the comments

FROM public.ecr.aws/lambda/dotnet:6 AS base

FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim as build
WORKDIR /src
COPY ["App.AWSLambdaOne.csproj", "App.AWSLambdaOne/"]
#Error: cannot find App.Data.csproj
COPY ["App.Data.csproj", "App.Data/"]
RUN dotnet restore "App.LambdaOne.csproj"

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

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

FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
CMD ["App.AWSLambdaOne::App.AWSLambdaOne.Function::FunctionHandler"]

I can understand why the error occurred, which is due to the Docker Context being under App.AWSLambdaOne. But cannot find any solution to fix it.

A partial solution I found is to have the Dockerfile at the Solution level, so Docker context includes all projects when building it. However, it does not fit my purpose since I want to build two images for different projects.

I have been searching for a clue in the last two days, it is driving me crazy. May I ask if it is possible to achieve what I am after with the existing project structure? If not can anyone please point me to the right direction?

THANKS A LOT in advance!

CodePudding user response:

You can't access host files outside the build context, so you must move the build context up a directory to be able to access the code for the other projects. So to start, let's change the build command to

docker build -t LambdaSimpleImage ..

Now we need to adjust the Dockerfile so it fits the new context

FROM public.ecr.aws/lambda/dotnet:6 AS base

FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim as build
WORKDIR /src
COPY ["App.LambdaSimple/App.LambdaSimple.csproj", "App.LambdaSimple/"]
COPY ["App.Data/App.Data.csproj", "App.Data/"]
COPY ["App.Core/App.Core.csproj", "App.Core/"]

RUN dotnet restore "App.LambdaSimple/App.LambdaSimple.csproj"

COPY . .
RUN dotnet build "App.LambdaSimple/App.LambdaSimple.csproj" --configuration Release --output /app/build

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

FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
CMD ["App.LambdaSimple::App.LambdaSimple.Function::FunctionHandler"]
  • Related