我正在尝试创建一个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
1条答案
按热度按时间cbjzeqam1#
你忘了把源代码复制到基础映像上。
我认为构建单个映像的问题是由于启动
docker build
命令的位置。您可以从根文件夹开始执行
docker build
,并使用-f
参数指定Dockerfile,在本例中,是您在Dockerfile上设置的指向根文件夹的路径。您可以保存Dockerfile的任何路径,重要的是要记住,Dockefile中的路径是指您启动build命令的路径,或者您如何更改/指定命令的路径,通常是点(docker build -t xxx -f Dockerfile .)最后一个点是Dockerfile上引用的文件夹。
根路径上的Dockerfile。
积木
--EventBus.Messages
服务项目
--审计
----Audit.API
Dockerfile
将这些条目添加到
.dockerignore
文件中否则使用
--no-restore
的构建会失败,因为在COPY
阶段,如果您在windows上开发并在linux映像上构建,它还复制了(可能)不同程序集的bin&obj
文件夹。然后,您可以从root
docker build -t whatever -f Dockerfile .
运行build命令