Project dependency not found when running DockerFi

2019-08-27 18:42发布

问题:

This is the Dockerfile generated by VS2017. I changed a little bit for using it on Azure DevOps

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["WebApi.csproj", "WebApi/"]
COPY ["./MyProject.Common/MyProject.Common.csproj", "MyProj.Common/"]
RUN dotnet restore "MyProject.WebApi/MyProject.WebApi.csproj"
COPY . .
WORKDIR "/src/MyProject.WebApi"
RUN dotnet build "MyProject.WebApi.csproj" -c Release -o /app

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

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

Solution structure

MyProject.sln
   -MyProject.Common
     ...
   -MyProject.WebApi
       ...
       Dockerfile

I have created a Build Pipeline under Azure DevOps to run Docker Build with these steps :

  1. Get Sources Step from Azure Repos Git
  2. Agent Job (Hosted Ubuntu 1604)
  3. Command Line script docker build -t WebApi .

I have this error

2019-02-02T18:14:33.4984638Z  ---> 9af3faec3d9e
2019-02-02T18:14:33.4985440Z Step 7/17 : COPY ["./MyProject.Common/MyProject.Common.csproj", "MyProject.Common/"]
2019-02-02T18:14:33.4999594Z COPY failed: stat /var/lib/docker/tmp/docker-builder671248463/MyProject.Common/MyProject.Common.csproj: no such file or directory
2019-02-02T18:14:33.5327830Z ##[error]Bash exited with code '1'.
2019-02-02T18:14:33.5705235Z ##[section]Finishing: Command Line Script

Attached Screenshot with the working directory used

I don't understand if I have to change something inside Dockerfile or into Console Script step on DevOps

回答1:

This is just a hunch, but considering your Dockerfile is located under MyProject.WebApi and you want to copy files from MyProject.Common which is on the same level, then you might need to specify a different context root directory when running docker build:

docker build -t WebApi -f Dockerfile ../

When Docker builds an image it collects a context - a list of files which are accessible during build and can be copied into image.

When you run docker build -t WebApi . it runs inside MyProject.WebApi directory and all files in the directory . (unless you have .dockerignore file), which is MyProject.WebApi in this case, are included into context. But MyProject.Common is not part of the context and thus you can't copy anything from it.

Hope this helps

EDIT: Perhaps you don't need not specify Working Directory (shown in the screenshot), then the command would change into:

docker build -t WebApi -f MyProject.WebApi/Dockerfile .

In this case Docker will use Dockerfile located inside MyProject.WebApi and include all files belonging to the solution into the context.

You can also read about context in the Extended description for the docker build command in the official documentation.