欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談docker的上下文和工作目錄

 更新時(shí)間:2025年09月30日 10:31:34   作者:小董亮出你的8塊腹肌吧!  
在編寫(xiě)Dockerfile時(shí)我們總會(huì)接觸到COPY上下文和工作目錄,有時(shí)候這些位置搞不清楚,本文就來(lái)介紹一下docker的上下文和工作目錄,感興趣的可以了解一下

寫(xiě)在前面

在編寫(xiě)Dockerfile時(shí)我們總會(huì)接觸到COPY上下文和工作目錄,有時(shí)候這些位置搞不清楚,總是讓我們陷入困境,本文就一起來(lái)看下這2個(gè)路徑。

1:COPY上下文

Dockerfile文件的COPY指令,拷貝的源文件就是基于上下文目錄來(lái)查找的,到底什么是上下文路徑,我們需要先來(lái)看下,而要解釋清楚什么是上下文路徑,必須先看下當(dāng)執(zhí)行docker build時(shí)的執(zhí)行流程,該流程如下:

1:將當(dāng)前所在的目錄打包為.tar包并發(fā)送到docker daemon
2:docker daemon將tar包解壓到一個(gè)臨時(shí)目錄,比如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 .

然后我們?cè)趆elloworld目錄執(zhí)行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:在上下文目錄里無(wú)法找到文件hello.txt

其中4找不到文件的原因是,因?yàn)槲募?code>/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 .

運(yùn)行:

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

此時(shí)找不到html/index.html,同樣的問(wèn)題,修改為docker/html/index.html,如下:

dongyunqi@dongyunqi-virtual-machine:~$ cat helloworld-app/Dockerfile
FROM busybox
COPY docker/hello.txt .
COPY docker/html/index.html .

運(yùn)行:

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

到這里上下文我們已經(jīng)清楚了,

2:工作目錄

工作目錄默認(rèn)就是根目錄,如下的Dockerfile驗(yàn)證:

dongyunqi@dongyunqi-virtual-machine:~$ cat howPwd.txt 
FROM busybox
RUN pwd

運(yùn)行:

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也輸出了當(dāng)前的工作目錄
 ---> Running in c1d493dc5ff6
Removing intermediate container c1d493dc5ff6
 ---> 4d065957678b
Step 3/3 : RUN pwd
 ---> Running in 7fc8f9b6b35a
/  # 這里我們輸出了當(dāng)前工作目錄是根目錄
Removing intermediate container 7fc8f9b6b35a
 ---> 578d72f80d16
Successfully built 578d72f80d16
Successfully tagged hello-app:2.0

當(dāng)然可以通過(guò)WORKDIR來(lái)修改工作目錄,如下:

dongyunqi@dongyunqi-virtual-machine:~$ cat howPwd.txt 
FROM busybox
WORKDIR /var
RUN pwd

運(yùn)行:

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也輸出了當(dāng)前的工作目錄是/var
 ---> Using cache
 ---> 89da7feb392e
Step 3/3 : RUN pwd
 ---> /var # 這里我們輸出了當(dāng)前工作目錄是/var
Successfully built 7975c01019bd
Successfully tagged hello-app:2.0

寫(xiě)在后面

到此這篇關(guān)于docker的上下文和工作目錄的文章就介紹到這了,更多相關(guān)docker的上下文和工作目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論