如何基于Dockerfile构建tomcat镜像
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
- 编写一个dockerfile文件
- dockerbuild构建成一个镜像
- dockerrun运行镜像
- dockerpush发布镜像(DockerHub、阿里云镜像仓库)
#以contes为例查看dockerhub上contes的dockerfile FROMscratch ADDcentos-7-x86_64-docker.tar.xz/ LABEL\ org.label-schema.schema-version="1.0"\ org.label-schema.name="CentOSBaseImage"\ org.label-schema.vendor="CentOS"\ org.label-schema.license="GPLv2"\ org.label-schema.build-date="20200809"\ org.opencontainers.image.title="CentOSBaseImage"\ org.opencontainers.image.vendor="CentOS"\ org.opencontainers.image.licenses="GPL-2.0-only"\ org.opencontainers.image.created="2020-08-0900:00:00+01:00" CMD["/bin/bash"]
DockerFile构建过程
基础知识:
- 每个保留关键字(指令)都必须是大写字母
- 执行顺序从上到下顺序执行
- 表示注释
- 每个指令都会创建提交一个新的镜像层,并提交
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
Docker镜像逐渐成为企业交付的标准!
DockerFile:构建镜像,定义了一切的步骤,源代码;
DockerImages:通过DockerFile构建生成的一个镜像,这个镜像就是我们最终发布和运行的产品!
Docker容器:容器就是镜像运行起来提供服务!
DockerFile的指令
FROM #基础镜像 MAINTAINER #镜像是谁写的 姓名+邮箱 RUN #镜像构建的时候需要运行的命令 ADD #步骤 eg:tomcat镜像---》放入tomcat的压缩包!添加内容 WORKDIR #镜像的工作目录 VOLUME #挂载的目录位置 EXPOST #暴露端口配置 CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代 ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令 ONBUILD #当构建一个被继承的DockerFile,这个时候就会运行ONBUILD的指令,触发指令 COPY #类似ADD,将我们的文件拷贝到镜像中 ENV #构建的时候设置环境变量
实战测试
DockerHub中99%的镜像都是从FROMscratch这个基础镜像过来的,然后配置我们需要的软件和配置来进行构建
创建一个自己的centos
#1、编写DockerFile的文件 FROMcentos #作者信息 MAINTAINERyinxiaodong#配置环境变量 ENVMYPATH/user/local #指定镜像的工作目录 WORKDIR$MYPATH RUNyuminstall-yvim RUNyuminstall-ynet-tools #暴露80端口 EXPOSE80 CMDecho$MYPATH CMDecho"---------end--------" CMD/bin/bash #2、通过文件构建镜像 #命令dockerbuild-fdockerfile文件路径 -t镜像名:版本号. [root@localhostdockerfile]#dockerbuild-fmydockerfile-centos-tmycentos:0.1. SendingbuildcontexttoDockerdaemon2.048kB Step1/9:FROMcentos --->0d120b6ccaa8 Step2/9:MAINTAINERyinxiaodong --->Runninginf79b12ffb083 Removingintermediatecontainerf79b12ffb083 --->8ee53d3f7a65 Step3/9:ENVMYPATH/user/local --->Runningin659a2c96d5f7 Removingintermediatecontainer659a2c96d5f7 --->59ab131ef44c Step4/9:WORKDIR$MYPATH --->Runningin0b4b8f9c65bb Removingintermediatecontainer0b4b8f9c65bb --->408b06671488 Step5/9:RUNyuminstall-ynet-tools --->Runningin151f81148a87 CentOS-8-AppStream118kB/s|5.8MB00:50 CentOS-8-Base218kB/s|2.2MB00:10 CentOS-8-Extras1.8kB/s|7.3kB00:04 Dependenciesresolved. ================================================================================ PackageArchitectureVersionRepositorySize ================================================================================ Installing: net-toolsx86_642.0-0.51.20160912git.el8BaseOS323k TransactionSummary ================================================================================ Install1Package Totaldownloadsize:323k Installedsize:1.0M DownloadingPackages: net-tools-2.0-0.51.20160912git.el8.x86_64.rpm219kB/s|323kB00:01 -------------------------------------------------------------------------------- Total66kB/s|323kB00:04 warning:/var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm:HeaderV3RSA/SHA256Signature,keyID8483c65d:NOKEY CentOS-8-Base162kB/s|1.6kB00:00 ImportingGPGkey0x8483C65D: Userid:"CentOS(CentOSOfficialSigningKey) " Fingerprint:99DB70FAE1D7CE227FB6488205B555B38483C65D From:/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Keyimportedsuccessfully Runningtransactioncheck Transactionchecksucceeded. Runningtransactiontest Transactiontestsucceeded. Runningtransaction Preparing:1/1 Installing:net-tools-2.0-0.51.20160912git.el8.x86_641/1 Runningscriptlet:net-tools-2.0-0.51.20160912git.el8.x86_641/1 Verifying:net-tools-2.0-0.51.20160912git.el8.x86_641/1 Installed: net-tools-2.0-0.51.20160912git.el8.x86_64 Complete! Removingintermediatecontainer151f81148a87 --->c0a0546c9b2a Step6/9:EXPOSE80 --->Runningin42000936515d Removingintermediatecontainer42000936515d --->fe68114ecf3f Step7/9:CMDecho$MYPATH --->Runninginc393fc53a354 Removingintermediatecontainerc393fc53a354 --->a6924276bf90 Step8/9:CMDecho"---------end--------" --->Runningin5994de56f0a1 Removingintermediatecontainer5994de56f0a1 --->a8ba0ebb3770 Step9/9:CMD/bin/bash --->Runningind1fa2d436363 Removingintermediatecontainerd1fa2d436363 --->41bb76be4884 Successfullybuilt41bb76be4884 Successfullytaggedmycentos:0.1 [root@localhostdockerfile]#dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE mycentos0.141bb76be48847secondsago246MB centos1.0d840628c30a96hoursago215MB centoslatest0d120b6ccaa82daysago215MB #启动mycentos使用ifconfig命令查看ip信息 [root@localhostdockerfile]#dockerrun-itmycentos:0.1 [root@494c2bc72263local]#ifconfig eth0:flags=4163 mtu1500 inet172.17.0.3netmask255.255.0.0broadcast172.17.255.255 ether02:42:ac:11:00:03txqueuelen0(Ethernet) RXpackets6bytes516(516.0B) RXerrors0dropped0overruns0frame0 TXpackets0bytes0(0.0B) TXerrors0dropped0overruns0carrier0collisions0 lo:flags=73 mtu65536 inet127.0.0.1netmask255.0.0.0 looptxqueuelen1000(LocalLoopback) RXpackets0bytes0(0.0B) RXerrors0dropped0overruns0frame0 TXpackets0bytes0(0.0B) TXerrors0dropped0overruns0carrier0collisions0 #查看所在目录/user/local正是我们在dockerfile中指定的目录 [root@494c2bc72263local]#pwd /user/local
查看镜像的构建历史记录
#命令dockerhistory镜像id [root@localhost~]#dockerhistory41bb76be4884 IMAGECREATEDCREATEDBYSIZECOMMENT 41bb76be488416minutesago/bin/sh-c#(nop)CMD["/bin/sh""-c""/bin…0B a8ba0ebb377016minutesago/bin/sh-c#(nop)CMD["/bin/sh""-c""echo…0B a6924276bf9016minutesago/bin/sh-c#(nop)CMD["/bin/sh""-c""echo…0B fe68114ecf3f16minutesago/bin/sh-c#(nop)EXPOSE800B c0a0546c9b2a16minutesago/bin/sh-cyuminstall-ynet-tools31.3MB 408b0667148817minutesago/bin/sh-c#(nop)WORKDIR/user/local0B 59ab131ef44c17minutesago/bin/sh-c#(nop)ENVMYPATH=/user/local0B 8ee53d3f7a6517minutesago/bin/sh-c#(nop)MAINTAINERyinxiaodong2daysago/bin/sh-c#(nop)LABELorg.label-schema.sc…0B 2daysago/bin/sh-c#(nop)ADDfile:538afc0c5c964ce0d…215MB
我们平时拿到一个镜像,可以研究一下它是怎么做的---->dockerhistory镜像id
CMD和ENTRYPOINT的区别
CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代 ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令 #测试CMD #编写DockerFile的文件 [root@localhostdockerfile]#catdockerfile-cmd-test FROMcentos CMD["ls","-a"] #根据DockerFile构建镜像 [root@localhostdockerfile]#dockerbuild-fdockerfile-cmd-test-ttestcmd:0.1. SendingbuildcontexttoDockerdaemon3.072kB Step1/2:FROMcentos --->0d120b6ccaa8 Step2/2:CMD["ls","-a"] --->Runninginb3f8ba72222b Removingintermediatecontainerb3f8ba72222b --->561e47f88730 Successfullybuilt561e47f88730 Successfullytaggedtestcmd:0.1 #构建成功 #查看镜像 [root@localhostdockerfile]#dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE testcmd0.1561e47f887306secondsago215MB centoslatest0d120b6ccaa82daysago215MB #启动镜像发现ls-a命令生效 [root@localhostdockerfile]#dockerrun-ittestcmd:0.1 ..dockerenv devhomelib64mediaoptrootsbin sysusr ..bin etcliblost+foundmntprocrunsrv tmpvar #启动命令中追加一个-l,我们期望的是ls-a-l,但是报错,这里将ls-a替换成了-l,最终的命令是-l所以报错。 [root@localhostdockerfile]#dockerrun-it561e47f88730-l docker:Errorresponsefromdaemon:OCIruntimecreatefailed:container_linux.go:349:startingcontainerprocesscaused"exec:\"-l\":executablefilenotfoundin$PATH":unknown. #将启动命令替换成dockerrun-ittestcmd:0.1ls-al,成功打印详细信息; [root@localhostdockerfile]#dockerrun-ittestcmd:0.1ls-al total0 drwxr-xr-x.1rootroot6Aug1308:20. drwxr-xr-x.1rootroot6Aug1308:20.. -rwxr-xr-x.1rootroot0Aug1308:20.dockerenv lrwxrwxrwx.1rootroot7May112019bin->usr/bin drwxr-xr-x.5rootroot360Aug1308:20dev drwxr-xr-x.1rootroot66Aug1308:20etc drwxr-xr-x.2rootroot6May112019home lrwxrwxrwx.1rootroot7May112019lib->usr/lib lrwxrwxrwx.1rootroot9May112019lib64->usr/lib64 drwx------.2rootroot6Aug921:40lost+found drwxr-xr-x.2rootroot6May112019media drwxr-xr-x.2rootroot6May112019mnt drwxr-xr-x.2rootroot6May112019opt dr-xr-xr-x.123rootroot0Aug1308:20proc dr-xr-x---.2rootroot162Aug921:40root drwxr-xr-x.11rootroot163Aug921:40run lrwxrwxrwx.1rootroot8May112019sbin->usr/sbin drwxr-xr-x.2rootroot6May112019srv dr-xr-xr-x.13rootroot0Aug1109:58sys drwxrwxrwt.7rootroot145Aug921:40tmp drwxr-xr-x.12rootroot144Aug921:40usr drwxr-xr-x.20rootroot262Aug921:40var #测试二:现在我们把DockerFile中CMD替换成ENTRYPOINT后重新构建,运行看看结果 [root@localhostdockerfile]#catdockerfile-cmd-test FROMcentos ENTRYPOINT["ls","-a"] #重新构建镜像 [root@localhostdockerfile]#dockerbuild-fdockerfile-cmd-test-ttestcmd:0.2. SendingbuildcontexttoDockerdaemon3.072kB Step1/2:FROMcentos --->0d120b6ccaa8 Step2/2:ENTRYPOINT["ls","-a"] --->Runninginc634ca09fabe Removingintermediatecontainerc634ca09fabe --->52d295395f08 Successfullybuilt52d295395f08 Successfullytaggedtestcmd:0.2 #查看镜像 [root@localhostdockerfile]#dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE testcmd0.252d295395f087secondsago215MB testcmd0.1561e47f8873012minutesago215MB centoslatest0d120b6ccaa82daysago215MB #运行testcmd:0.2镜像并追加-l,发现打印出了详细信息 [root@localhostdockerfile]#dockerrun-ittestcmd:0.2-l total0 drwxr-xr-x.1rootroot6Aug1308:17. drwxr-xr-x.1rootroot6Aug1308:17.. -rwxr-xr-x.1rootroot0Aug1308:17.dockerenv lrwxrwxrwx.1rootroot7May112019bin->usr/bin drwxr-xr-x.5rootroot360Aug1308:17dev drwxr-xr-x.1rootroot66Aug1308:17etc drwxr-xr-x.2rootroot6May112019home lrwxrwxrwx.1rootroot7May112019lib->usr/lib lrwxrwxrwx.1rootroot9May112019lib64->usr/lib64 drwx------.2rootroot6Aug921:40lost+found drwxr-xr-x.2rootroot6May112019media drwxr-xr-x.2rootroot6May112019mnt drwxr-xr-x.2rootroot6May112019opt dr-xr-xr-x.121rootroot0Aug1308:17proc dr-xr-x---.2rootroot162Aug921:40root drwxr-xr-x.11rootroot163Aug921:40run lrwxrwxrwx.1rootroot8May112019sbin->usr/sbin drwxr-xr-x.2rootroot6May112019srv dr-xr-xr-x.13rootroot0Aug1109:58sys drwxrwxrwt.7rootroot145Aug921:40tmp drwxr-xr-x.12rootroot144Aug921:40usr drwxr-xr-x.20rootroot262Aug921:40var
DockerFile中很多命令都十分相似,我们需要了解他的区别,最好的学习就是对比进行测试查看效果!
实战:Tomcat镜像
1、准备镜像文件tomcat压缩包jdk压缩包
[root@localhosttomcat]#ls
apache-tomcat-8.5.43.tar.gzDockerfilejdk-8u211-linux-x64.tar.gzread.txt
2、编写dockerfile文件,官方命名Dockerfile,build会自动寻找这个文件,不需要-f指定!
#引入基础 FROMcentos #作者信息 MAINTAINERyinxiaodoong#copy文件read.txt到容器的/usr/local/read.txt目录下 COPYread.txt/usr/local/read.txt #添加jdktomcat--->自动解压 ADDjdk-8u211-linux-x64.tar.gz/usr/local/ ADDapache-tomcat-8.5.43.tar.gz/usr/local/ #安装vim命令 RUNyuminstall-yvim #配置MYPATH ENVMYPATH/usr/local/ #指定进入容器默认路径/user/local/ WORKDIR$MYPATH #配置jdk环境变量 ENVJAVA_HOME/usr/local/jdk1.8.0_211 ENVCLASSPATH$JAVA_HOME/lib/dt.jat:$JAVA_HOME/lib/tools.jar #配置tomcat环境变量 ENVCATALINA_HOME/usr/local/apache-tomcat-8.5.43 ENVCATALINA_BASH/usr/local/apache-tomcat-8.5.43 ENVPATH$PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin #暴露8080端口 EXPOSE8080 #/usr/local/tomcat/webapps# #启动容器时启动tomcat CMD/usr/local/apache-tomcat-8.5.43/bin/startup.sh&&tail-f/usr/local/apache-tomcat-8.5.43/logs/catalina.out #构建tomcat镜像 [root@localhosttomcat]#dockerbuild-tmytomcat [root@localhosttomcat]#dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE mytomcatlatesta9707559afa18secondsago693MB centoslatest0d120b6ccaa84daysago215MB [root@localhosttomcat]#dockerrun-d-p8081:8080--namemytomcat--01-v/home/yinxiaodong/build/tomcat/test:/usr/local/apache-tomcat-8.5.43/webapps/test-v/home/yinxiaodong/build/tomcat/logs:/usr/local/apache-tomcat-8.5.43/logsmytomcat
发布自己的镜像
发布到dockerHub
地址 https://hub.docker.com/注册自己的账号
确定自己的账号可以登录
在我们的服务器上提交
[root@localhostlogs]#dockerlogin--help Usage: dockerlogin[OPTIONS][SERVER] LogintoaDockerregistry. Ifnoserverisspecified,thedefaultisdefinedbythedaemon. Options: -p,--passwordstringPassword --password-stdinTakethepasswordfromstdin -u,--usernamestringUsername [root@localhostlogs]# [root@localhostlogs]#dockerlogin-uxxx-pxxx WARNING!Using--passwordviatheCLIisinsecure.Use--password-stdin. WARNING!Yourpasswordwillbestoredunencryptedin/root/.docker/config.json. Configureacredentialhelpertoremovethiswarning.See https://docs.docker.com/engine/reference/commandline/login/#credentials-store LoginSucceeded
登录完毕后就可以提交镜像了
[root@localhostlogs]#dockerpushxxx/mytomcat:1.0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。