COPY
格式
COPY [--chown=<用户>:<组>]<源>...<目的>
COPY [--chown=<用户>:<组>]["<源>"..."<目的>"]
案例
[root@hmm ~]# mkdir yum3-centos
[root@hmm ~]# ls
anaconda-ks.cfg docker-hello hello.c sizetest yum3-centos
Dockerfile hello hello-world.tar test1
[root@hmm ~]# cd yum3-centos/
[root@hmm yum3-centos]# ls
[root@hmm yum3-centos]# touch Dockerfile
[root@hmm yum3-centos]# touch nginx.repo
[root@hmm yum3-centos]# pwd
/root/yum3-centos
[root@hmm yum3-centos]# vim Dockerfile
-bash: vim: command not found
[root@hmm yum3-centos]# vi Dockerfile
[root@hmm yum3-centos]# vi nginx.repo
[root@hmm yum3-centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest 9363667f8aec 2 weeks ago 25.4MB
centos-with-nginx latest fe0f7fc1d27c 2 weeks ago 248MB
imglayers-test latest 3d2d641327e8 2 weeks ago 196MB
my-hello latest e8c9e00260a8 3 weeks ago 861kB
registry.cn-hangzhou.aliyuncs.com/hanmiao_03/hmmmm 24.03.26 e8c9e00260a8 3 weeks ago 861kB
127.0.0.1:5000/ubuntu latest ca2b0f26964c 4 weeks ago 77.9MB
ubuntu latest ca2b0f26964c 4 weeks ago 77.9MB
httpd latest ac45b24b92cc 2 months ago 167MB
hm/ubuntu hhh f9a80a55f492 10 months ago 63.2MB
ubuntu 18.04 f9a80a55f492 10 months ago 63.2MB
hello-world latest d2c94e258dcb 11 months ago 13.3kB
127.0.0.1:5000/centos v1 5d0da3dc9764 2 years ago 231MB
centos latest 5d0da3dc9764 2 years ago 231MB
ubuntu 14.04 13b66b487594 3 years ago 197MB
[root@hmm yum3-centos]# docker run centos-with-nginx
[root@hmm yum3-centos]# docker build -t 'yun3-copy1' .
[+] Building 0.2s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 99B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 140B 0.0s
=> [1/2] FROM docker.io/library/centos:latest 0.0s
=> [2/2] COPY ./nginx.repo /etc/yum.repos.d/ 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:592cacbb3f593d986c578fdb9e29ea0e806b057b 0.0s
=> => naming to docker.io/library/yun3-copy1 0.0s
[root@hmm yum3-centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yun3-copy1 latest 592cacbb3f59 5 seconds ago 231MB
registry latest 9363667f8aec 2 weeks ago 25.4MB
centos-with-nginx latest fe0f7fc1d27c 2 weeks ago 248MB
imglayers-test latest 3d2d641327e8 2 weeks ago 196MB
my-hello latest e8c9e00260a8 3 weeks ago 861kB
registry.cn-hangzhou.aliyuncs.com/hanmiao_03/hmmmm 24.03.26 e8c9e00260a8 3 weeks ago 861kB
127.0.0.1:5000/ubuntu latest ca2b0f26964c 4 weeks ago 77.9MB
ubuntu latest ca2b0f26964c 4 weeks ago 77.9MB
httpd latest ac45b24b92cc 2 months ago 167MB
hm/ubuntu hhh f9a80a55f492 10 months ago 63.2MB
ubuntu 18.04 f9a80a55f492 10 months ago 63.2MB
hello-world latest d2c94e258dcb 11 months ago 13.3kB
127.0.0.1:5000/centos v1 5d0da3dc9764 2 years ago 231MB
centos latest 5d0da3dc9764 2 years ago 231MB
ubuntu 14.04 13b66b487594 3 years ago 197MB
[root@hmm yum3-centos]# docker run -ti yun3-copy1
[root@5e60d54ffd46 /]# cd /etc/yum.repos.d/
[root@5e60d54ffd46 yum.repos.d]# ls
CentOS-Linux-AppStream.repo CentOS-Linux-HighAvailability.repo
CentOS-Linux-BaseOS.repo CentOS-Linux-Media.repo
CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Plus.repo
CentOS-Linux-Debuginfo.repo CentOS-Linux-PowerTools.repo
CentOS-Linux-Devel.repo CentOS-Linux-Sources.repo
CentOS-Linux-Extras.repo nginx.repo
CentOS-Linux-FastTrack.repo
[root@5e60d54ffd46 yum.repos.d]# cat nginx.repo
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch
gpgcheck=0
enabled=1
源路径必须位于构建上下文中
使用copy的时候超出当前构建范围,不管是使用相对路径还是使用绝对路径,构建镜像的时候都会报错
- 使用相对路径构建
[root@hmm yum3-centos]# docker build -t yum3-copy2 .
[+] Building 0.0s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 135B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/centos:latest 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 31B 0.0s
=> CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/ 0.0s
=> ERROR [3/3] COPY ../../home/hmm/hello.txt /root 0.0s
------
> [3/3] COPY ../../home/hmm/hello.txt /root:
------
Dockerfile:3
--------------------
1 | FROM centos
2 | COPY ./nginx.repo /etc/yum.repos.d/
3 | >>> COPY ../../home/hmm/hello.txt /root #报错
4 | CMD /bin/bash
5 |
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 77c8e775-3806-4851-a513-b1983d25edd4::03l8kntfbdcyvyt4tw33xeuv5: "/home/hmm/hello.txt": not found
[root@hmm yum3-centos]
#不管源文件是用的绝对路径还是相对路径,只要超出了当前路径那么就会失败,报错
- 使用绝对路径
[+] Building 0.0s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 139B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/centos:latest 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 31B 0.0s
=> CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/ 0.0s
=> ERROR [3/3] COPY /root/yum3-centos/nginx.repo /root 0.0s
------
> [3/3] COPY /root/yum3-centos/nginx.repo /root:
------
Dockerfile:3
--------------------
1 | FROM centos
2 | COPY ./nginx.repo /etc/yum.repos.d/
3 | >>> COPY /root/yum3-centos/nginx.repo /root #报错
4 | CMD /bin/bash
5 |
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 77c8e775-3806-4851-a513-b1983d25edd4::pu9wv1zzwb7w9tuck529visuq: "/root/yum3-centos/nginx.repo": not found
[root@hmm yum3-centos]#
如果源是目录,则复制目录的整个内容,包括文件系统元数据。
[root@hmm yum3-centos]# vi Dockerfile
[root@hmm yum3-centos]# docker build -t yum3-copy3 .
[+] Building 0.3s (8/8) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 130B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/centos:latest 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 962.10kB 0.1s
=> CACHED [2/3] COPY ./nginx.repo /etc/yum.repos.d/ 0.0s
=> [3/3] COPY ./lunisolar-main/ /home/ 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:84ae402461bb0b18255a4fbe8dfd918f35e26072 0.0s
=> => naming to docker.io/library/yum3-copy3 0.0s
[root@hmm yum3-centos]# docker run yum3-copy3
[root@hmm yum3-centos]# docker run -ti yum3-copy3
[root@153b880fd808 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@153b880fd808 /]# cd /home/
[root@153b880fd808 home]# ls
CHANGELOG.md babel.config.js package.json tsconfig.json
LICENSE changelog-config.js pnpm-lock.yaml typings
README.md docs rollup.config.js
__tests__ jest.config.ts src
[root@153b880fd808 home]#
多个源
#如果直接指定多个源,或者源中使用了通配符,则目的路径必须是目录,并且必须以斜杠(/)结尾
[root@hmm yum3-centos]# ls
Dockerfile lunisolar-main nginx.repo
[root@hmm yum3-centos]# vim Dockerfile
[root@hmm yum3-centos]# cat Dockerfile
FROM centos
COPY ./luni* nginx.repo /home/
CMD /bin/bash
[root@hmm yum3-centos]# docker build -t "yun3-copy5" .
[+] Building 0.1s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 94B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 10.89kB 0.0s
=> CACHED [1/2] FROM docker.io/library/centos:latest 0.0s
=> [2/2] COPY ./luni* nginx.repo /home/ 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d4277a7b443afe5bc46923ee7beb05eef0364295 0.0s
=> => naming to docker.io/library/yun3-copy5 0.0s
[root@hmm yum3-centos]# docker run -ti yun3-copy5
[root@34cfdea289ae /]# cd /home/
[root@34cfdea289ae home]# ls
CHANGELOG.md babel.config.js nginx.repo src
LICENSE changelog-config.js package.json tsconfig.json
README.md docs pnpm-lock.yaml typings
__tests__ jest.config.ts rollup.config.js
[root@34cfdea289ae home]#
-
如果目的路径不以斜杠结尾,则它将被视为常规文件,源内容将被写入目录路径(简单来说就是会重命名)
-
如果目的路径不存在,则会与其路径中所有缺少的目录一起被创建。(简单来说就是,会自动创建路径)
ADD
实例
[root@hmm yum3-centos]# docker build -t 'yun3-add' .
[+] Building 0.2s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 189B 0.0s
=> [internal] load metadata for docker.io/library/cento 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/4] FROM docker.io/library/centos:latest 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 413.14kB 0.0s
=> http://mirrors.aliyun.com/repo/Centos-8.repo 0.1s
=> [2/4] ADD nginx.repo /etc/yum.repos.d/ 0.0s
=> [3/4] ADD http://mirrors.aliyun.com/repo/Centos-8.re 0.0s
=> [4/4] ADD lunisolar-main.zip /home/yun3/ 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:eb713753ec15edfd95fe4feba80d 0.0s
=> => naming to docker.io/library/yun3-add 0.0s
[root@hmm yum3-centos]# docker run -ti yun3-add
[root@ec222ba8717e /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@ec222ba8717e /]# cd root/
[root@ec222ba8717e ~]# ls
Centos-8.repo anaconda-post.log
anaconda-ks.cfg original-ks.cfg
[root@ec222ba8717e ~]# cd /home/
[root@ec222ba8717e home]# ls
yun3
[root@ec222ba8717e home]# cd yun3/
[root@ec222ba8717e yun3]# ls
lunisolar-main.zip
[root@ec222ba8717e yun3]# cd /etc/yum.repos.d/
[root@ec222ba8717e yum.repos.d]# ls
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
nginx.repo
标签:centos,root,repo,yum3,0.0,镜像,docker,Dockerfile
From: https://www.cnblogs.com/humlogs/p/18111153