使用类库的基础镜像构建Dotnet API docker镜像

ipakzgxi  于 2023-04-11  发布在  Docker
关注(0)|答案(1)|浏览(125)

我正在尝试创建一个API微服务,它利用了一个共享的模型库。在创建镜像时,我需要在范围内包含这些类。然而,由于我的项目结构,这些模型并不总是在范围内。我决定使用这些通用模型创建一个基础镜像,并在这些镜像之上构建我的API。然而,我遇到了一个问题,我认为是一个结构问题,我只是似乎不能 Package 我的头周围。
当构建时,我收到下面提到的错误。如果我手动引用这些库的位置,我通常没有问题,但是我需要它们在一个可重用的映像中,以便于使用。
下面是两个dockerfile,以及我的项目是如何结构化的示例,以及构建时API期望从哪里导入类库。你知道我做错了什么吗?

API dockerfile

FROM base-image AS base
    WORKDIR /App
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
    WORKDIR /src
    
    
    COPY ["Audit.API/Audit.API.csproj", "Services/Audit/Audit.API/"]
    COPY --from=base /src/EventBus.Messages/ /src/Building-Blocks/EventBus.Messages/
    
    RUN dotnet restore "./Services/Audit/Audit.API/Audit.API.csproj" --source http://address
    COPY . .
    WORKDIR "/src/Services/Audit/Audit.API"
    RUN dotnet build "Audit.API.csproj" -c Release -o /App/build
    
    FROM build AS publish
    RUN dotnet publish "Audit.API.csproj" -c Release -o /App/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /App
    COPY --from=publish /App/publish .
    ENTRYPOINT ["dotnet", "Audit.API.dll"]

基础Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["Responses/Responses/Responses.csproj", "Responses/"]
COPY ["EventBus.Messages/EventBus.Messages.csproj", "EventBus.Messages/"]

错误

=> ERROR [build 8/8] RUN dotnet build "Audit.API.csproj" -c Release -o /App/build                                                                4.0s
------
 > [build 8/8] RUN dotnet build "Audit.API.csproj" -c Release -o /App/build:
#0 0.804 MSBuild version 17.3.2+561848881 for .NET
#0 1.258   Determining projects to restore...
#0 1.748   Restored /src/Building-Blocks/EventBus.Messages/EventBus.Messages.csproj (in 119 ms).
#0 1.922   Restored /src/Services/Audit/Audit.API/Audit.API.csproj (in 310 ms).
#0 3.443   EventBus.Messages -> /App/build/EventBus.Messages.dll
#0 3.904 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Services/Audit/Audit.API/Audit.API.csproj]
#0 3.912
#0 3.912 Build FAILED.
#0 3.912
#0 3.912 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Services/Audit/Audit.API/Audit.API.csproj]
#0 3.912     0 Warning(s)
#0 3.912     1 Error(s)
#0 3.912
#0 3.912 Time Elapsed 00:00:03.01
------
failed to solve: executor failed running [/bin/sh -c dotnet build "Audit.API.csproj" -c Release -o /App/build]: exit code: 1

AUDIT.API包引用

<ItemGroup>
    <ProjectReference Include="..\..\..\Building-Blocks\EventBus.Messages\EventBus.Messages.csproj" />
  </ItemGroup>

文件夹结构

Building-Blocks
 EventBus.Messages
Services
 Audit
  Audit.API
cbjzeqam

cbjzeqam1#

你忘了把源代码复制到基础映像上。

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["Responses/Responses/", "Responses/"]
COPY ["EventBus.Messages/", "EventBus.Messages/"]

我认为构建单个映像的问题是由于启动docker build命令的位置。
您可以从根文件夹开始执行docker build,并使用-f参数指定Dockerfile,在本例中,是您在Dockerfile上设置的指向根文件夹的路径。
您可以保存Dockerfile的任何路径,重要的是要记住,Dockefile中的路径是指您启动build命令的路径,或者您如何更改/指定命令的路径,通常是点(docker build -t xxx -f Dockerfile .)最后一个点是Dockerfile上引用的文件夹。
根路径上的Dockerfile。
积木
--EventBus.Messages
服务项目
--审计
----Audit.API
Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build

WORKDIR /src
# Copy the .csproj for restore
COPY ["Building-Blocks/Responses/Responses/Responses.csproj", "Building-Blocks/Responses/"]
COPY ["Building-Blocks/EventBus.Messages/EventBus.Messages.csproj", "Building-Blocks/EventBus.Messages/"]
COPY ["Services/Audit.API/Audit.API.csproj", "Services/Audit/Audit.API/"]
    
# Restore the main project 
RUN dotnet restore "./Services/Audit/Audit.API/Audit.API.csproj" --source http://address
    
COPY ["Building-Blocks/Responses/Responses/", "Building-Blocks/Responses/"]
COPY ["Building-Blocks/EventBus.Messages/", "Building-Blocks/EventBus.Messages/"]
COPY ["Services/Audit.API/", "Services/Audit/Audit.API/"]

WORKDIR "/src/Services/Audit/Audit.API"

# --no-restore, you already have a restore stage above
RUN dotnet build "Audit.API.csproj" -c Release --no-restore -o /App/build
    
# Get the runtime only you don't need the SDK to run the image
RUN dotnet publish "Audit.API.csproj" -c Release -o /App/publish /p:UseAppHost=false
    
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS pub
WORKDIR /App
COPY --from=build /App/publish .
ENTRYPOINT ["dotnet", "Audit.API.dll"]

将这些条目添加到.dockerignore文件中

**/bin
**/obj
**/.vscode
**/.vs

否则使用--no-restore的构建会失败,因为在COPY阶段,如果您在windows上开发并在linux映像上构建,它还复制了(可能)不同程序集的bin&obj文件夹。
然后,您可以从root docker build -t whatever -f Dockerfile .运行build命令

相关问题