Docker RUN PWD位于Windows的根目录而不是当前工作目录

时间:2019-06-13 16:19:10

标签: windows docker dockerfile

出于某种奇怪的原因,我当前的工作目录是/。它应该是dockerfile的当前目录,但不是。有人看过吗?我在Windows上同时使用PowerShell和Cmder并获得相同的结果。

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2

#WORKDIR /bin
RUN pwd
RUN ls -la

#COPY . app/

RUN dotnet --version
ENTRYPOINT ["dotnet", "app/simple-dotnet-api.dll"]

输出:

λ docker build --no-cache -t simpledotnetapi .
Sending build context to Docker daemon  252.4kB
Step 1/6 : FROM mcr.microsoft.com/dotnet/core/sdk:2.2
 ---> 08663b8eaa01
Step 2/6 : RUN ls -la
 ---> Running in ac2e849a757e
total 72
drwxr-xr-x   1 root root 4096 Jun 13 16:17 .
drwxr-xr-x   1 root root 4096 Jun 13 16:17 ..
-rwxr-xr-x   1 root root    0 Jun 13 16:17 .dockerenv
drwxr-xr-x   1 root root 4096 Jun 11 00:30 bin
drwxr-xr-x   2 root root 4096 Mar 28 09:12 boot
drwxr-xr-x   5 root root  340 Jun 13 16:17 dev
drwxr-xr-x   1 root root 4096 Jun 13 16:17 etc
drwxr-xr-x   2 root root 4096 Mar 28 09:12 home
drwxr-xr-x   1 root root 4096 Jun 10 00:00 lib
drwxr-xr-x   2 root root 4096 Jun 10 00:00 lib64
drwxr-xr-x   2 root root 4096 Jun 10 00:00 media
drwxr-xr-x   2 root root 4096 Jun 10 00:00 mnt
drwxr-xr-x   2 root root 4096 Jun 10 00:00 opt
dr-xr-xr-x 144 root root    0 Jun 13 16:17 proc
drwx------   1 root root 4096 Jun 11 08:07 root
drwxr-xr-x   3 root root 4096 Jun 10 00:00 run
drwxr-xr-x   1 root root 4096 Jun 11 00:30 sbin
drwxr-xr-x   2 root root 4096 Jun 10 00:00 srv
dr-xr-xr-x  13 root root    0 Jun 13 15:46 sys
drwxrwxrwt   1 root root 4096 Jun 11 00:30 tmp
drwxr-xr-x   1 root root 4096 Jun 10 00:00 usr
drwxr-xr-x   1 root root 4096 Jun 10 00:00 var
Removing intermediate container ac2e849a757e
 ---> 50062f08e279
Step 3/6 : RUN pwd
 ---> Running in f79599959874
/
Removing intermediate container f79599959874
 ---> 8af3de2e5e58
Step 4/6 : COPY . app/
 ---> 32d77a7cf452
Step 5/6 : RUN dotnet --version
 ---> Running in f1f83285eff2
2.2.300
Removing intermediate container f1f83285eff2
 ---> f0bb4891b002
Step 6/6 : ENTRYPOINT ["dotnet", "app/simple-dotnet-api.dll"]
 ---> Running in 3978dee3fdcf
Removing intermediate container 3978dee3fdcf
 ---> af95f20afbbe
Successfully built af95f20afbbe
Successfully tagged simpledotnetapi:latest

1 个答案:

答案 0 :(得分:1)

在Dockerfile中使用RUN pwd将打印映像的当前工作目录,而不是Dockerfile所在的目录。

运行docker build时会传递一个构建上下文。在使用docker build --no-cache -t simpledotnetapi .的情况下,.(当前目录)是构建的上下文。 COPY /source /destination将在构建上下文的根目录中查找source,然后尝试将其复制到容器内的/destination

使用类推,运行docker build类似于使用远程Shell在远程计算机上运行一系列指令,但是共享目录为构建上下文。