简体   繁体   English

如何将 .NET6 AWS Lambda 项目与依赖的 class 库项目对接

[英]How to dockerize .NET6 AWS Lambda project with dependent class library projects

I have four projects in one solution:我在一个解决方案中有四个项目:

  • App.Data - class library App.Data - class 图书馆
  • App.Core - class library App.Core - class库
  • App.LambdaOne - AWS Lambda project App.LambdaOne - AWS Lambda 项目
  • App.LambdaTwo - AWS Lambda project App.LambdaTwo - AWS Lambda 项目

The solution structure looks like this:解决方案结构如下所示:

Solution:解决方案:

  • App.Data应用程序数据
  • App.Core应用核心
  • App.LambdaOne App.LambdaOne
    • Dockerfile Dockerfile
  • App.LambdaTwo应用程序LambdaTwo
    • Dockerfile Dockerfile

I am trying to create two docker images for App.LambdaOne and App.LambdaTwo , so I can deploy them separately.我正在尝试为App.LambdaOneApp.LambdaTwo创建两个 docker 图像,因此我可以单独部署它们。

So far, I have got a simple lambda project without any dependent project works using the Dockfile below.到目前为止,我有一个简单的 lambda 项目,没有任何依赖项目使用下面的 Dockfile 工作。

Docker command in App.LambdaSimple 's directory App.LambdaSimple目录中的 Docker 命令

docker build -t LambdaSimpleImage .

Dockerfile 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.LambdaOne depend on App.Data and App.Core .但棘手的部分是App.LambdaOne依赖于App.DataApp.Core I tried to modify the working sample to deploy App.LambdaOne , but no luck so far.我试图修改工作示例以部署App.LambdaOne ,但到目前为止运气不好。 By running the same Docker command.通过运行相同的 Docker 命令。 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.我能理解为什么会报错,这是因为Docker Context在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.我发现的一个部分解决方案是在解决方案级别拥有 Dockerfile,因此 Docker 上下文在构建它时包括所有项目。 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!非常感谢!

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现在我们需要调整 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/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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM