本文主要介绍如何基于Dockerfile构建tomcat镜像。通过示例代码非常详细的介绍,对大家的学习或者工作都有一定的参考价值。有需要的朋友可以参考一下。
Dockerfile是一个用来构建docker镜像的文件!命令脚本!
施工步骤:
docker文件构建过程
基础知识:
Dockerfile是面向开发的。如果以后要发布项目并镜像,需要写dockerfile,非常简单!
Docker形象逐渐成为企业投放的标配!
DockerFile:建立镜像,定义一切的步骤,源代码;
dockerimages:dockerfile构造生成的一个图像,这个图像是我们发布并运行的最终产品!
Docker容器:容器是运行来提供服务的一面镜子!
DockerFile命令
实践测试
DockerHub中99%的映像都是从从头开始的基础上镜像的,然后我们配置构建它所需的软件和配置。
创建您自己的centos
#1、编写DockerFile的文件 FROMcentos #作者信息 MAINTAINERyinxiaodong<m15010969094@163.com> #配置环境变量 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<m15010969094@163.com> --->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)<security@centos.org>" 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<UP,BROADCAST,RUNNING,MULTICAST>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<UP,LOOPBACK,RUNNING>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)MAINTAINERyinxiaodong<m1…0B 0d120b6ccaa82daysago/bin/sh-c#(nop)CMD["/bin/bash"]0B <missing>2daysago/bin/sh-c#(nop)LABELorg.label-schema.sc…0B <missing>2daysago/bin/sh-c#(nop)ADDfile:538afc0c5c964ce0d…215MB我们通常会得到一个镜像,所以我们可以研究它是如何制作的->Dockhistory镜像id
cmd和ENTRYPOINT的区别
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<m15010969094@163.com> #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
这就是本文的全部内容。希望对大家的学习有帮助,支持我们。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)