docker的上下文和工作目录

写在前面

在编写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 构建上下文

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值