在Azure DevOps上运行DockerFile build命令时找不到项目依赖项 [英] Project dependency not found when running DockerFile build command on Azure DevOps
问题描述
这是VS2017生成的Dockerfile. 我对在Azure DevOps上使用它进行了一些更改
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"]
解决方案结构
MyProject.sln
-MyProject.Common
...
-MyProject.WebApi
...
Dockerfile
我已经在Azure DevOps下创建了一个构建管道,以通过以下步骤运行Docker Build:
I have created a Build Pipeline under Azure DevOps to run Docker Build with these steps :
- 从Azure Repos Git获取源代码步骤
- 代理作业(托管Ubuntu 1604)
- 命令行脚本
docker build -t WebApi .
我有这个错误
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
我不知道我是否需要在Dockerfile内或在DevOps上的控制台脚本步骤中更改某些内容
I don't understand if I have to change something inside Dockerfile or into Console Script step on DevOps
推荐答案
这只是预感,但是考虑到您的Dockerfile
位于MyProject.WebApi
下,并且您想从同一目录下的MyProject.Common
复制文件级别,则在运行docker build
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 ../
当Docker构建映像时,它会收集一个上下文-生成期间可以访问并可以复制到映像中的文件列表.
When Docker builds an image it collects a context - a list of files which are accessible during build and can be copied into image.
当您运行docker build -t WebApi .
时,它将在MyProject.WebApi
目录中运行,并且目录.
中的所有文件(除非您具有.dockerignore
文件)(在这种情况下为MyProject.WebApi
)都包含在上下文中.但是MyProject.Common
不是上下文的一部分,因此您无法从中复制任何内容.
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.
希望这会有所帮助
也许您不需要指定Working Directory
(如屏幕截图所示),那么命令将变为:
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 .
在这种情况下,Docker将使用位于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.
您还可以在 查看全文