docker构建镜像——Dockerfile

from

RUN

CMD

EXPOSE

LABEL

ENV

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

entrypoint

实例

  • 只有cmd
  • 只有entrypoint
[root@hmm yum3]# docker build -t "entry-test-3" .
[+] Building 0.1s (11/11) FINISHED          docker:default
 => [internal] load build definition from Dockerfile  0.0s
 => => transferring dockerfile: 385B                  0.0s
 => [internal] load metadata for docker.io/library/c  0.0s
 => [internal] load .dockerignore                     0.0s
 => => transferring context: 2B                       0.0s
 => [1/5] FROM docker.io/library/centos:latest        0.0s
 => [internal] load build context                     0.0s
 => => transferring context: 28B                      0.0s
 => http://mirrors.aliyun.com/repo/Centos-8.repo      0.1s
 => CACHED [2/5] RUN rm -rf /etc/yum.repos.d/*.repo   0.0s
 => CACHED [3/5] ADD http://mirrors.aliyun.com/repo/  0.0s
 => CACHED [4/5] ADD test.py /root/                   0.0s
 => CACHED [5/5] RUN yum install -y python38          0.0s
 => exporting to image                                0.1s
 => => exporting layers                               0.0s
 => => writing image sha256:351421498ec6d823e1c82e8e  0.1s
 => => naming to docker.io/library/entry-test-3       0.0s
[root@hmm yum3]# docker run -ti entry-test-3 
/
['bin', 'dev', 'etc', 'home', 'lib', 'lib64', 'lost+found', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var', 'yun3-777', '.dockerenv']
[root@hmm yum3]# cat Dockerfile 
FROM centos
RUN rm -rf /etc/yum.repos.d/*.repo
ADD http://mirrors.aliyun.com/repo/Centos-8.repo /etc/yum.repos.d/
ADD test.py /root/
RUN yum install -y python38

# 启动容器默认执行的脚本或者命令
ENTRYPOINT ["/usr/bin/python3","/root/test.py"]

#CMD ["/bin/bash"] # 单独只有cmd指令:容器启动的时默认执行的命令

[root@hmm yum3]# cat test.py 
# -*- coding:utf-8 -*-
import os

#打印当前路径
print(os.getcwd())
os.mkdir('yun3-777')
print(os.listdir('.'))

让python程序接受打印命令行传参数

没修改了一个程序,需要重新构建一个镜像

[root@hmm yum3]# docker rmi -f  c065771342b6 #删除之前的镜像,更加清晰
Untagged: entry-test3:latest
Deleted: sha256:c065771342b650d7fb499b007086de7c7c1d2286283ff9f48351478fb3b6d809
[root@hmm yum3]# docker build -t 'entry-test3.1' .
[+] Building 22.0s (11/11) FINISHED         docker:default
 => [internal] load build definition from Dockerfile  0.0s
 => => transferring dockerfile: 385B                  0.0s
 => [internal] load metadata for docker.io/library/c  0.0s
 => [internal] load .dockerignore                     0.0s
 => => transferring context: 2B                       0.0s
 => [1/5] FROM docker.io/library/centos:latest        0.0s
 => [internal] load build context                     0.0s
 => => transferring context: 173B                     0.0s
 => http://mirrors.aliyun.com/repo/Centos-8.repo      0.0s
 => CACHED [2/5] RUN rm -rf /etc/yum.repos.d/*.repo   0.0s
 => CACHED [3/5] ADD http://mirrors.aliyun.com/repo/  0.0s
 => [4/5] ADD test.py /root/                          0.0s
 => [5/5] RUN yum install -y python38                21.6s
 => exporting to image                                0.4s
 => => exporting layers                               0.4s
 => => writing image sha256:7e6bc7bbfa684aedf1f5af1d  0.0s 
 => => naming to docker.io/library/entry-test3.1      0.0s 
[root@hmm yum3]# docker run -ti entry-test3.1 
/                                                          
['bin', 'dev', 'etc', 'home', 'lib', 'lib64', 'lost+found', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var', 'yun3-777', '.dockerenv']
['/root/test.py']
[root@hmm yum3]# docker run -ti entry-test3.1  hmmmmm
/
['bin', 'dev', 'etc', 'home', 'lib', 'lib64', 'lost+found', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var', 'yun3-777', '.dockerenv']
['/root/test.py', 'hmmmmm'] #传参数成功
  • 既有cmd又有entrypoint
    两个指令一起使用的时候,entrypoint指令作为可执行文件或者命令,cmd为entrypoint提供默认参数,docker run 指定命令可替换cmd默认参数
[root@hmm yum3]# docker build -t "entry-cmd-test" .
[+] Building 0.1s (11/11) FINISHED          docker:default
 => [internal] load build definition from Dockerfile  0.0s
 => => transferring dockerfile: 385B                  0.0s
 => [internal] load metadata for docker.io/library/c  0.0s
 => [internal] load .dockerignore                     0.0s
 => => transferring context: 2B                       0.0s
 => [1/5] FROM docker.io/library/centos:latest        0.0s
 => [internal] load build context                     0.0s
 => => transferring context: 29B                      0.0s
 => http://mirrors.aliyun.com/repo/Centos-8.repo      0.0s
 => CACHED [2/5] RUN rm -rf /etc/yum.repos.d/*.repo   0.0s
 => CACHED [3/5] ADD http://mirrors.aliyun.com/repo/  0.0s
 => CACHED [4/5] ADD test.py /root/                   0.0s
 => CACHED [5/5] RUN yum install -y python38          0.0s
 => exporting to image                                0.0s
 => => exporting layers                               0.0s
 => => writing image sha256:e2f1cde9ca8c1667c019eb4a  0.0s
 => => naming to docker.io/library/entry-cmd-test     0.0s
[root@hmm yum3]# docker run -ti entry-cmd-test
/
['bin', 'dev', 'etc', 'home', 'lib', 'lib64', 'lost+found', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var', 'yun3-777', '.dockerenv']
['/root/test.py', '/bin/bash'#此处为cmd传入的默认参数]
[root@hmm yum3]# docker run -ti entry-cmd-test yun3-hmmm
/
['bin', 'dev', 'etc', 'home', 'lib', 'lib64', 'lost+found', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var', 'yun3-777', '.dockerenv']
['/root/test.py', 'yun3-hmmm'#此处为python接收到传入的参数,覆盖率;了cmd]
[root@hmm yum3]# 

VOLUME(创建挂载点)

WORKDIR(配置工作目录)

[root@hmm yum3]# docker build -t "work-test" .
[+] Building 23.1s (12/12) FINISHED         docker:default
 => [internal] load build definition from Dockerfile  0.0s
 => => transferring dockerfile: 405B                  0.0s
 => [internal] load metadata for docker.io/library/c  0.0s
 => [internal] load .dockerignore                     0.0s
 => => transferring context: 2B                       0.0s
 => CACHED [1/6] FROM docker.io/library/centos:lates  0.0s
 => [internal] load build context                     0.0s
 => => transferring context: 29B                      0.0s
 => CACHED http://mirrors.aliyun.com/repo/Centos-8.r  0.1s
 => [2/6] WORKDIR /usr/local/                         0.0s
 => [3/6] RUN rm -rf /etc/yum.repos.d/*.repo          0.1s
 => [4/6] ADD http://mirrors.aliyun.com/repo/Centos-  0.0s
 => [5/6] ADD test.py /root/                          0.0s
 => [6/6] RUN yum install -y python38                22.6s
 => exporting to image                                0.4s
 => => exporting layers                               0.4s
 => => writing image sha256:b1064a2ef4c3a4c5fe8afe22  0.0s 
 => => naming to docker.io/library/work-test          0.0s 
[root@hmm yum3]# docker run  -ti --entrypoint '/bin/bash' work-test
# --entrypoint 覆盖配置文件中的entrypoint?还是指定entrypoint参数                                                   
[root@962f735c9bdc local]# pwd
/usr/local #工作目录切换成功
[root@962f735c9bdc local]# 

其他指令

- USER

  • ARG
  • SHEL

posted on   代码你敲我不敲  阅读(113)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具

导航

统计

返回顶端
点击右上角即可分享
微信分享提示