浅谈docker的上下文和工作目录
作者:小董亮出你的8块腹肌吧!
写在前面
在编写Dockerfile时我们总会接触到COPY上下文和工作目录,有时候这些位置
搞不清楚,总是让我们陷入困境,本文就一起来看下这2个路径
。
1:COPY上下文
Dockerfile文件的COPY指令,拷贝的源文件就是基于上下文目录来查找的,到底什么是上下文路径,我们需要先来看下,而要解释清楚什么是上下文路径,必须先看下当执行docker build时的执行流程,该流程如下:
1:将当前所在的目录打包为.tar包并发送到docker daemon
2:docker daemon将tar包解压到一个临时目录,比如docker daemon的/var/lib/tmp目录
这里tar压缩文件的最终解压目录就是我们上下文了,比如我们有如下的目录:
dongyunqi@dongyunqi-virtual-machine:~$ tree helloworld-app/ helloworld-app/ ├── docker │ ├── hello.txt │ └── html │ └── index.html └── Dockerfile 2 directories, 3 files
Dockerfile如下:
dongyunqi@dongyunqi-virtual-machine:~$ cat helloworld-app/Dockerfile FROM busybox COPY hello.txt . COPY html/index.html .
然后我们在helloworld目录执行docker build,如下:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t hello-app:2.0 helloworld-app/ Sending build context to Docker daemon 5.12kB # tar包成功到docker daemon,后面就是docker daemon的工作了 Step 1/3 : FROM busybox ---> 827365c7baf1 Step 2/3 : COPY hello.txt . COPY failed: stat /var/lib/docker/tmp/docker-builder375982663/hello.txt: no such file or directory
从COPY failed: stat /var/lib/docker/tmp/docker-builder375982663/hello.txt: no such file or directory
我们可以得到如下的信息:
1:docker客户端打包生成的tar包名字叫做`docker-builder375982663.tar`。
2:docker daemon解压tar包的目录是/var/lib/docker/tmp
3:这里的上下文目录是/var/lib/docker/tmp/docker-builder375982663
4:在上下文目录里无法找到文件hello.txt
其中4找不到文件的原因是,因为文件在/var/lib/docker/tmp/docker-builder375982663/docker/hello.txt
,因此我们只需要修改Dockerfiie将COPY hello.txt .
修改为COPY docker/hello.txt .
,如下:
dongyunqi@dongyunqi-virtual-machine:~$ cat helloworld-app/Dockerfile FROM busybox COPY docker/hello.txt . # 修改这一行 COPY html/index.html .
运行:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t hello-app:2.0 helloworld-app/ Sending build context to Docker daemon 6.144kB Step 1/3 : FROM busybox ---> 827365c7baf1 Step 2/3 : COPY docker/hello.txt . ---> 83989744c05c Step 3/3 : COPY html/index.html . COPY failed: file not found in build context or excluded by .dockerignore: stat html/index.html: file does not exist
此时找不到html/index.html
,同样的问题,修改为docker/html/index.html
,如下:
dongyunqi@dongyunqi-virtual-machine:~$ cat helloworld-app/Dockerfile FROM busybox COPY docker/hello.txt . COPY docker/html/index.html .
运行:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t hello-app:2.0 helloworld-app/ Sending build context to Docker daemon 7.168kB Step 1/3 : FROM busybox ---> 827365c7baf1 Step 2/3 : COPY docker/hello.txt . ---> Using cache ---> 83989744c05c Step 3/3 : COPY docker/html/index.html . ---> Using cache ---> 10fa588c5565 Successfully built 10fa588c5565 Successfully tagged hello-app:2.0
到这里上下文我们已经清楚了,
2:工作目录
工作目录默认就是根目录,如下的Dockerfile验证:
dongyunqi@dongyunqi-virtual-machine:~$ cat howPwd.txt FROM busybox RUN pwd
运行:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t hello-app:2.0 -f howPwd.txt helloworld-app/ Sending build context to Docker daemon 8.751kB Step 1/3 : FROM busybox ---> 827365c7baf1 Step 2/3 : WORKDIR / # 这里docker daemon也输出了当前的工作目录 ---> Running in c1d493dc5ff6 Removing intermediate container c1d493dc5ff6 ---> 4d065957678b Step 3/3 : RUN pwd ---> Running in 7fc8f9b6b35a / # 这里我们输出了当前工作目录是根目录 Removing intermediate container 7fc8f9b6b35a ---> 578d72f80d16 Successfully built 578d72f80d16 Successfully tagged hello-app:2.0
当然可以通过WORKDIR来修改工作目录,如下:
dongyunqi@dongyunqi-virtual-machine:~$ cat howPwd.txt FROM busybox WORKDIR /var RUN pwd
运行:
dongyunqi@dongyunqi-virtual-machine:~$ docker build -t hello-app:2.0 -f howPwd.txt helloworld-app/ Sending build context to Docker daemon 8.751kB Step 1/3 : FROM busybox ---> 827365c7baf1 Step 2/3 : WORKDIR /var # 这里docker daemon也输出了当前的工作目录是/var ---> Using cache ---> 89da7feb392e Step 3/3 : RUN pwd ---> /var # 这里我们输出了当前工作目录是/var Successfully built 7975c01019bd Successfully tagged hello-app:2.0
写在后面
到此这篇关于docker的上下文和工作目录的文章就介绍到这了,更多相关docker的上下文和工作目录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!