欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Docker&Kubernetes❀DockerFile保留关键字基本使用方法与相关解释

时间:2023-07-28
文章目录

1、基础概念2、构建过程

2.1 语法介绍2.2 DockerFile的执行流程 3、保留字指令4、案例配置

4.1 案例一:构建自定义系统4.2 案例二:区分CMD与ENTRYPOINT指令 1、基础概念


DockerFile 是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本;我们在https://hub.docker.com/爬取的镜像其实本身也是由DockerFile文件构建,如下所示;

搜索引擎内搜查centos,选择某一个镜像,点击;

查找到对应的镜像版本与其DockerFile链接;

DockerFile链接为Github网站内代码;

相关代码如下:

FROM scratch#镜像本源;ADD centos-8-x86_64.tar.xz /LABEL org.label-schema.schema-version="1.0" org.label-schema.name="CentOS base Image"org.label-schema.vendor="CentOS" org.label-schema.license="GPLv2" org.labelschema.build-date="20201204"CMD ["/bin/bash"]

2、构建过程

DockerFile构建镜像的过程如下:

编写DockerFile文件docker builddocker run 2.1 语法介绍

每条保留字指令都必须为大写字母且后面要跟随至少一个参数;指令按照从上到下的顺序执行;#代表注释;每条指令都会创建一个新的镜像层,并对镜像进行提交; 2.2 DockerFile的执行流程

Docker从基础镜像运行一个容器;执行一条指令并对容器做出修改;执行类似Docker commit的操作提交一个新的镜像层;Docker再基于刚提交的镜像运行一个新容器;执行DockerFile中的下一条指令直到所有指令都执行完成; 3、保留字指令


FROM:基础镜像,指定当前镜像是基于哪个镜像的;MAINTAINER:指定镜像维护者的姓名和邮箱地址;RUN:指定容器构建时需要运行的命令;EXPOSE:指定当前容器对外暴露的端口信息;WORKDIR:指定在创建容器后终端默认登录的工作目录;ENV:指定构建镜像过程中的环境变量;ADD:将宿主机目录下文件复制进镜像且ADD命令会自动处理URL和解压tar类型压缩包;COPY:类似于ADD,复制文件和目录到镜像中,但是不做处理;

CPOY src destCOPY [“src”,“dest”] VOLUME:容器数据卷,用于数据保持和持久化;CMD:指定容器启动时要运行的命令,Docker中可以存在多个CMD命令,但是只有最后一个生效,- CMD会被Docker run之后的参数替换;ENTRYPOINT:指定容器启动时要运行的命令,ENTRYPOINT和CMD一样,但是不会被Docker run的参数替换,进行追加执行;ONBUILD:当构建一个被继承的DockerFile时运行指令,父镜像在被子镜像继承后父镜像的onbuild被触发; 4、案例配置


4.1 案例一:构建自定义系统

首先使用默认centos镜像创建一个容器作为对比容器,名称设置为test_a,在该容器内执行vim与ifconfig命令无法执行,因为精简版的centos没有此功能;

[root@Docker ~]# docker run -it --name test_a centos[root@efb048390050 /]# pwd /[root@efb048390050 /]# vim /tmp/test.txtbash: vim: command not found[root@efb048390050 /]# ifconfigbash: ifconfig: command not found

构建DockerFile;

[root@Docker ~]# vim /myDocker/DockerFile_test01 FROM centosMAINTAINER zxc<497427805@qq.com>ENV mypath /tmp#环境变量设置为/tmp;WORKDIR $mypath#修改容器默认登录目录;RUN yum install -y vim#安装vim功能;RUN yum install -y net-tools#安装网络功能;EXPOSE 80#暴露80端口;CMD /bin/bash

使用DockerFile构建镜像;

[root@Docker ~]# docker build -f /myDocker/DockerFile_test01 -t mycentos:1.3 .Sending build context to Docker daemon 12.19MBStep 1/8 : FROM centos ---> 300e315adb2fStep 2/8 : MAINTAINER zxc<497427805@qq.com> ---> Running in 9334196fed84Removing intermediate container 9334196fed84 ---> 3faa116ae7eeStep 3/8 : ENV mypath /tmp ---> Running in 63dbd47292ceRemoving intermediate container 63dbd47292ce ---> def729e0a375Step 4/8 : WORKDIR $mypath ---> Running in 5e2756007a93Removing intermediate container 5e2756007a93 ---> 422d53f849bdStep 5/8 : RUN yum install -y vimComplete!Removing intermediate container 0448e9cb3db8 ---> d761096df2ebStep 6/8 : RUN yum install -y net-toolsComplete!Removing intermediate container 948beabdd09e ---> 4d7ccf0941d4Step 7/8 : EXPOSE 80 ---> Running in f5870f895b8fRemoving intermediate container f5870f895b8f ---> 78d5fca3e1ddStep 8/8 : CMD /bin/bash ---> Running in a991211763ccRemoving intermediate container a991211763cc ---> 37fa47ba5db8Successfully built 37fa47ba5db8Successfully tagged mycentos:1.3[root@Docker ~]# docker images mycentosREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 300e315adb2f 3 months ago 209MBmycentos 1.3 37fa47ba5db8 2 minutes ago 291MB

使用新镜像创建容器,名称为test_b,并验证容器默认登录目录、vim与ifconfig是否可以执行、端口80是否打开;

[root@Docker ~]# docker run -it --name test_b mycentos:1.3 [root@3663701a12aa tmp]# pwd/tmp[root@3663701a12aa tmp]# vim /tmp/test.txt[root@3663701a12aa tmp]# ifconfigeth0: flags=4163 mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 16 bytes 2092 (2.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@3663701a12aa tmp]# [root@Docker ~]# [root@Docker ~]# docker psConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3663701a12aa mycentos:1.3 "/bin/sh -c /bin/bash" 24 seconds ago Up 22 seconds 80/tcp test_befb048390050 centos "/bin/bash" 8 minutes ago Up 8 minutes test_a

4.2 案例二:区分CMD与ENTRYPOINT指令

[root@Docker ~]# vim /myDocker/DockerFile_test02 FROM centosRUN yum install -y curlCMD curl -k https://www.baidu.com[root@Docker ~]# docker build -f /myDocker/DockerFile_test02 -t mycentos02:1.3 .Sending build context to Docker daemon 12.19MBStep 1/3 : FROM centos ---> 300e315adb2fStep 2/3 : RUN yum install -y curlComplete!Removing intermediate container 9d958771849f ---> 2c4160ab22b9Step 3/3 : CMD curl -s http://ip.cn ---> Running in 6419f0667627Removing intermediate container 6419f0667627 ---> ed0db2759ed1Successfully built ed0db2759ed1Successfully tagged mycentos02:1.3[root@Docker ~]# docker images mycentos02REPOSITORY TAG IMAGE ID CREATED SIZEmycentos02 1.3 ed0db2759ed1 about a minute ago 243MB[root@Docker ~]# docker run -it --name test_c mycentos02:1.3~百度一下,你就知道~[root@Docker ~]# curl -k https://www.baidu.com -iHTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Length: 2443Content-Type: text/htmlDate: Thu, 11 Mar 2021 07:26:27 GMTEtag: "588603eb-98b"Last-Modified: Mon, 23 Jan 2017 13:23:55 GMTPragma: no-cacheServer: bfe/1.0.8.18Set-cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

注意点:

[root@Docker ~]# docker run -it --name test_c mycentos02:1.3 -idocker: Error response from daemon: Conflict、The container name "/test_c" is already in use by container "fdcf5c5e28df2f7d1dfde3c92903d808fc2b568966162d5594db1c7d1c6bf62a"、You have to remove (or rename) that container to be able to reuse that name.See 'docker run --help'.

此处产生报错原因:Docker中可以存在多个CMD命令,但是只有最后一个生效,CMD会被Docker run之后的参数替换,此处加入参数 -i,实际上是执行了代码命令 CMD -i,因此输出报错;

[root@Docker ~]# vim /myDocker/DockerFile_test02 FROM centosRUN yum install -y curlENTRYPOINT curl -k https://www.baidu.com[root@Docker ~]# docker build -f /myDocker/DockerFile_test03 -t mycentos03:1.3 .[root@Docker ~]# docker run -it --name test_d mycentos03:1.3~百度一下,你就知道~[root@Docker ~]# docker run -it --name test_d mycentos02:1.3 -iHTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Length: 2443Content-Type: text/htmlDate: Fri, 12 Mar 2021 07:50:23 GMTEtag: "588603eb-98b"Last-Modified: Mon, 23 Jan 2017 13:23:55 GMTPragma: no-cacheServer: bfe/1.0.8.18Set-cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/~百度一下,你就知道~

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。