netcore项目下添加的Dockerfile编译,找不到依赖项目的路径

2019-11-19 13:24发布

问题:

Dockerfile如下:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
//这一行copy是没有问题的,由于执行docker build的目录就是在项目根目录下
COPY ["Api.Admin.csproj", "Api.Admin/"]

//下面的4个类库项目,是api.admin项目依赖的项目(因为没有依赖nuget包,直接从本地项目引用)
COPY ["../lib-common/Api.Common/Api.Common.csproj", "../lib-common/Api.Common/"]
COPY ["../lib-common/Api.Common1/Api.Common1.csproj", "../lib-common/Api.Common1/"]
COPY ["../lib-common/Api.Common2/Api.Common2.csproj", "../lib-common/Api.Common2/"]
COPY ["../lib-common/Api.Common3/Api.Common3.csproj", "../lib-common3/Api.Common3/"]

RUN dotnet restore "Api.Admin/Api.Admin.csproj"
COPY . .
WORKDIR "/src/Api.Admin"
RUN dotnet build "Api.Admin.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Api.Admin.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Admin.dll"]

cmd定位到api.admin项目目录下,执行docker build 命令,会提示定位到
COPY ["../lib-common/Api.Common/Api.Common.csproj", "../lib-common/Api.Common/"]
这一行错误,COPY failed: Forbidden path outside the build context:

回答1:

直接copy 所有的项目吧简单粗暴

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
COPY ./ /src

WORKDIR "/src/WebNotebook.Vue"

FROM build AS publish
RUN dotnet publish "WebNotebook.Vue.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebNotebook.Vue.dll"]

这是我的源码示例
https://github.com/zhaozhengyan/WebNotebook



回答2:

将 Dockerfile 放到上级目录