更新:
这是代码仓库:
https://github.com/franva/dockerapi/tree/master/dockerapi

我是Docker的新手,我使用Visual Studio 2017添加了Docker Support,因此基本上它只是在项目文件夹下创建一个Dockerfile。
我跑:

docker build -t dockerapi:init .

E:\temp\playground\dockerapi (master -> origin)
λ docker build -t dockerapi:imp .

Sending build context to Docker daemon  25.09kB
Step 1/17 : FROM microsoft/dotnet:2.2-aspnetcore-runtime
AS base  ---> c56aab97bc42
Step 2/17 : WORKDIR /app  ---> Using cache  --->8f9c09673b05
Step 3/17 : EXPOSE 80  ---> Using cache  --->7b773fae0164
Step 4/17 : EXPOSE 443  ---> Using cache  --->a5c32bea1008
Step 5/17 : FROM microsoft/dotnet:2.2-sdk AS build  --->155911c343f3
Step 6/17 : WORKDIR "/src"  ---> Using cache  --->b5357cf7b8ef
Step 7/17 : COPY ["dockerapi/dockerapi.csproj","dockerapi/"]  ---> Using cache  ---> 354963ea2dcd
Step 8/17 : RUN dotnet restore "dockerapi/dockerapi.csproj"  ---> Using cache  --->7ef0324fd068
Step 9/17 : COPY . .  ---> 93ef0a22b520
Step 10/17 : WORKDIR /src/dockerapi  ---> Running in 2179c7f48179
Removing intermediate container 2179c7f48179  ---> ede8a86afa07

Step 11/17 : RUN dotnet build "dockerapi.csproj" -c Release -o /app  ---> Running in a4d4864c8208

Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rightsreserved.

  Restore completed in 532.79 ms for /src/dockerapp/dockerapp.csproj.
  Restore completed in 874.46 ms for /src/dockerapi/dockerapi.csproj.

dockerapp -> /app/dockerapp.dll
dockerapi -> /app/dockerapi.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:06.46 Removing intermediate container a4d4864c8208 ---> 84f9f429528d
Step 12/17 : FROM build AS publish  ---> 84f9f429528d
Step 13/17 : RUN dotnet publish "dockerapi.csproj" -c
Release -o /app  ---> Running in beed612f6c1f

Microsoft (R) Build
Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C)
Microsoft Corporation. All rights reserved.

  Restore completed in 80.99 ms for /src/dockerapp/dockerapp.csproj.

  Restore completed in 104.18 ms for /src/dockerapi/dockerapi.csproj.

dockerapp -> /src/dockerapp/bin/Release/netcoreapp2.2/dockerapp.dll
dockerapi -> /src/dockerapi/bin/Release/netcoreapp2.2/dockerapi.dll
dockerapi -> /app/ Removing intermediate container beed612f6c1f  ---> 2969bcee4855
Step 14/17 : FROM base AS final  ---> a5c32bea1008
Step 15/17 : WORKDIR /app  ---> Using cache  ---> be6b73f67179
Step 16/17 : COPY --from=publish /app .  ---> Using cache  ---> f4512e5248b0
Step 17/17 : ENTRYPOINT ["dotnet", "dockerapi.dll"]  ---> Using cache  --->b60b91d0d89f

Successfully built b60b91d0d89f Successfully tagged dockerapi:imp

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
我将Dockerfile移到了项目文件夹上方1级的解决方案文件夹中,然后docker build正确运行了。
但是然后我仔细看了看,发现它没有创建 src 文件夹
我首先获得了最新的图像并运行该图像:

然后我找到了新的容器

然后我摔进了容器
E:\temp\playground\dockerapi (master -> origin)
λ docker exec -it f191ed6a110b bash
root@f191ed6a110b:/app# ls
appsettings.Development.json  dockerapi.pdb                     dockerapp.dll                     web.config
appsettings.json              dockerapi.runtimeconfig.dev.json  dockerapp.pdb
dockerapi.deps.json           dockerapi.runtimeconfig.json      dockerapp.runtimeconfig.dev.json
dockerapi.dll                 dockerapp.deps.json               dockerapp.runtimeconfig.json
这是代码。
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app <--- this line does create app folder
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR "/src" <--- this line doesn't create src folder
COPY ["dockerapi/dockerapi.csproj", "dockerapi/"]
RUN dotnet restore "dockerapi/dockerapi.csproj"
COPY . .
WORKDIR "/src/dockerapi"
RUN dotnet build "dockerapi.csproj" -c Release -o /app

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

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

最佳答案

这是完全正常的。
您的dockerfile使用multistage build。您应该阅读有关它的文档。基本上,您有一个阶段(基础)是要发布的镜像,而另一个阶段(构建)是要构建和发布应用程序的镜像。
在构建阶段所做的所有操作均不会发布,除非您将其明确复制到最终镜像中。
您的dockerfile指定您将源代码复制到构建阶段的/ src目录中。然后,您仍在构建阶段构建并发布它。完成后,将结果从构建阶段复制到基础阶段。这就是COPY --from=publish /app .指令的作用。
它允许您发布较浅的图像。注意,基本镜像为microsoft/dotnet:2.2-aspnetcore-runtime,而构建镜像则基于较重的microsoft/dotnet:2.2-sdk

原始答案
问题不是图像中不存在/src/src/dockerapi目录。这是在主机上找不到的dockerapi/dockerapi.csproj文件。
这仅仅是一个相对路径错误。您正在从C:\temp\playground\dockerapp\dockerapi目录内部执行docker build命令。这意味着,构建上下文是dockerapi目录。
当您执行此行COPY ["dockerapi/dockerapi.csproj", "dockerapi/"]时, docker 正在搜索不存在的C:\temp\playground\dockerapp\dockerapi\dockerapi\dockerapi.csproj(注意\dockerapi\dockerapi)文件。
通过执行dockerapp文件夹中的build命令并将路径传递到Dockerfile,尝试更改Docker构建上下文。

C:\temp\playground\dockerapp> docker build -f .\dockerapi\Dockerfile .
请参阅Docker文档以获取更多信息https://docs.docker.com/engine/reference/builder/#usage

关于.net - Docker WORKDIR不会创建文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56806676/

10-11 01:48