I'm trying to create a Docker image but it's not working. I'm trying to run this Docker command from the CLI and I'm getting the following error:
(from the <repo root>/src/Services/Accounts/Accounts.Api)
> docker build .
Error message:
Step 7/18 : COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY failed: stat /var/lib/docker/tmp/docker-builder937056687/src/Services/Accounts/Accounts.Api/Accounts.Api.csproj: no such file or directory
and here's my Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
RUN ls -al
COPY ["src/Services/Accounts/Accounts.Api/Accounts.Api.csproj", "src/Services/Accounts/Accounts.Api/"]
COPY ["src/Services/Accounts/Accounts.Api/NuGet.config", "src/Services/Accounts/Accounts.Api/"]
RUN dotnet restore "src/Services/Accounts/Accounts.Api/Accounts.Api.csproj"
COPY . .
WORKDIR "/src/src/Services/Accounts/Accounts.Api"
RUN dotnet build "Accounts.Api.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Accounts.Api.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Hornet.Accounts.Api.dll"]
I've also tried running this from the repo root directory:
> docker build .\src\Services\Accounts\Accounts.Api
Why is it trying to find my files in some /var/lib/docker/tmp/blah
folder?
Further info:
答案 0 :(得分:1)
我认为我们可以通过澄清如何使用构建上下文以及如何指定Dockerfile的位置来解决此问题。
Docker构建命令用法如下:
docker build [OPTIONS] PATH
您的构建上下文是您使用PATH
定义的位置,Docker可以将构建上下文中的文件用于构建。 (您不能在构建上下文之外使用文件进行构建。)
在COPY
语句的Dockerfile中,您要指定源文件相对于存储库根目录的位置。这意味着
您应该使用<root of your repo>
PATH
从.
运行构建命令,如下所示:
docker build .
您尚未在问题中指定Dockerfile的路径,但在我看来,它位于您的存储库的子文件夹中。为了使您的构建工作生效,请从<root of your repo>
发出docker build命令,如下所示:
docker build -f <path to Dockerfile> .
您的Dockerfile必须在构建上下文中。
我认为正确获取构建上下文和Dockerfile的位置将修复您的构建。
还请记住使用-t
标志来标记您的图像。
为什么要在/ var / lib / docker / tmp / blah文件夹中找到我的文件?
您可能已经知道,Docker在用于Windows的Docker应用程序的内部使用Linux VM来开发基于Linux的容器。此位置仅指VM中的位置。