什么叫建立镜像

什么叫建立镜像,第1张

如何基于Dockerfile构建tomcat镜像

本文主要介绍如何基于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"]

    docker文件构建过程

    基础知识:

  • 每个保留的关键字(指令)必须是大写字母。
  • 执行顺序是从上到下执行的。
  • 表示意见
  • 每个指令创建并提交一个新的镜像层,并提交它。
  • 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%的映像都是从从头开始的基础上镜像的,然后我们配置构建它所需的软件和配置。

    创建您自己的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的区别

    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 ..dockerenvdevhomelib64mediaoptrootsbinsysusr ..binetcliblost+foundmntprocrunsrvtmpvar #启动命令中追加一个-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<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

    这就是本文的全部内容。希望对大家的学习有帮助,支持我们。

    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://outofmemory.cn/zz/774363.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-05-03
    下一篇 2022-05-03

    发表评论

    登录后才能评论

    评论列表(0条)

    保存