Dockerfile in subdirectory causes Forbidden path outside the build context exception

  docker, dockerfile

I’m trying to put a dockerfile in a subdirectory of my main startup project AppMain. AppMain has a dependency project called AppDependency. When the dockerfile is in the root directory of AppMain, all works correctly, but when it’s nested in a subdirectory of the AppMain, it fails with

4>Step 5/20 : COPY ["AppMain/AppMain.csproj", "AppMain/"]
4>COPY failed: stat /var/lib/docker/tmp/docker-builder453314675/AppMain/AppMain.csproj: no such file or directory

So that makes sense to me since it’s nested in a subdirectory, so I simply added a "../" in front of the COPY commands but that results in the following context exception:

4>COPY failed: Forbidden path outside the build context: ../AppMain/AppMain.csproj ()

Alright so that makes sense as well because the docker documentation states that the context starts where the dockerfile is located, but this leads me to ask the real question.

  1. If it cannot copy outside of the where the dockerfile is located and it works when its located in the root of AppMain, then why doesn’t it fail when copying the AppReference project which obviously is above where the docker file is located and outside the context since it would have to go up one directory and then down to the AppReference project?
  2. Is there a way to achieve having a dockerfile nested in a subdirectory?

The entire docker file is listed below.

FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["AppMain/AppMain.csproj", "AppMain/"]
COPY ["AppReference/AppReference.csproj", "AppReference/"]
RUN dotnet restore "AppMain/AppMain.csproj"
COPY . .
WORKDIR "/src/AppMain"
RUN dotnet build "AppMain.csproj" -c Release -o /app

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

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

Source: Dockerfile Questions

LEAVE A COMMENT