本文档介绍了两种制作Docker镜像的方法。使用的系统是CentOS7DockerImage两种制作方法方法一:dockercommit#将容器当前状态保存到镜像中,然后生成对应的镜像方法二:dockerbuild#使用Dockerfile自动创建镜像方法一:dockercommit使用安装的apache工具创建容器镜像[root@Docker~]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEcentoslatest470671670cac4monthsago237MB[root@Docker~]#dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES[root@Docker~]#dockerrun-itcentos:latest/bin/bash[root@1b96e68a3cce/]#[root@1b96e68a3cce/]#yum-yinstallhttpd#in在容器中安装apache包[root@1b96e68a3cce/]#exitto查看镜像列表[root@Docker~]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEcentoslatest470671670cac4个月前237MB根据容器时制作之前状态的镜像:安装apache工具创建centos镜像语法:dockercommit"containerID"or"image_name"查看容器ID[root@Docker~]#dockerps-aCONTAINERIDIMAGECOMMANDCREATED状态端口名称1b96e68a3ccecentos:latest"/bin/bash"3分钟前退出(0)2分钟前awesome_hypatia607752360adfcentos:latest"/bin/bash-c'while…"18分钟前退出(137)12分钟agobrave_fermi0a297ff99af8centos:latest"/bin/bash"22分钟前退出(1)20分钟前ecstatic_yonathefb4af688330centos:latest"/bin/bash"24分钟前退出(0)23分钟前epic_mcclintock[root@Docker~]#docker提交1b96e68a3:apachesha256:b8822ec8a7bbb325793e7908e355180BE82A49481CFF197661FB5D0DA5872E88[root@docker?]itcentos:apache/bin/bash[root@e4c295d27581/]#rpm-qahttpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64看httpd软件名解释基于容器创建成功的方法关于apache镜像二:创建基于centos的httpdweb服务器镜像通过:dockerbuild使用dockerbuild创建镜像时,需要使用Dockerfile文件自动创建image镜像。Dockerfile有点像编译源码时./configure后生成的Makefile。Docker~]#mkdir/docker-build[root@Docker~]#cd/docker-build[root@Dockerdocker-build]#touchDockerfile[root@Dockerdocker-build]#lsDockerfile注意:make需要Makefile自动编译自动创建docker镜像时,需要Dockerfile2,编辑DockerfileDockerfile用于创建自定义镜像,包括用户指定的软件依赖等。[root@Dockerdocker-build]#vimDockerfileFROMcentos:latestMAINTAINERRUNyum-yinstallhttpdADDstart.sh/usr/local/bin/start.shADDindex.html/var/www/html/index.htmlCMDechohelloworld注释FROMcentos:latest#哪个镜像来自MAINTAINER#MAINTAINER镜像创建者RUNyum-yinstallhttpd#RUNinstallsoftwarewithADDstart.sh/usr/local/bin/start.shADDindex.html/var/www/html/index.htmlADD将文件复制到新生成图片的文件系统对应的路径下。复制到新镜像的所有文件和文件夹权限为0755,uid和gid为0CMDechohelloworld#container启动时执行命令或启动服务,但是一个Dockerfile中只能有一个CMD命令,如果有多个一个,另一个执行最后一个CMD3.创建start.sh脚本启动httpd服务和apache默认主页index.html文件[root@Dockerdocker-build]#echo"#!/bin/bash">>start.sh[root@Dockerdocker-build]#echo"/usr/sbin/httpd-DFOREGROUND">>start.sh注:/usr/sbin/httpd-DFOREGROUND相当于执行systemctlstarthttpd[root@Dockerdocker-build]#chmoda+xstart.shcreatesindex.html[root@Dockerdocker-build]#echo"dockerimagebuildtestfromjaking">index.html4.使用命令build创建新镜像语法:dockerbuild-t父镜像名称:镜像tagDockerfile文件路径-t:表示tag,镜像名称示例:使用命令dockerbuild新建一个镜像,命名为centos:httpd[root@Dockerdocker-build]#lsDockerfileindex.htmlstart.sh[root@Dockerdocker-build]#dockerbuild-tcentos:httpd./注意:./表示当前目录,你的当前目录应该包含DockerfileSendingbuildcontexttoDockerdaemon4.096kBStep1/5:FROMcentos:latest--->470671670cacStep2/5:MAINTAINER--->Ru--->5b9af0184bcfStep3/5:RUNyum-yinstallhttpd--->在8f5c114649edCentOS-8-AppStream228KB/s中运行|7.0MB00:31CentOS-8-Base547KB/s|2.2MB00:04CentOS-8-附加功能431B/s|5.9kB00:14Dependenciesresolved.PackageArchVersionRepoSizeInstall:httpdx86_642.4.37-16.module_el8.1.0+256+ae790463AppStream1.7MInstallationdependencies:aprx86_641.6.3-9.el8AppStream121x.6_6.4-util.1-6.el8AppStream105kcentos-logos-httpdnoarch80.5-2.el8AppStream24k...删除中间容器...完成!删除中间容器8f5c114649ed--->040b5f229962Step4/5:添加start.sh/usr/local/bin/start.sh--->11a106005031步骤5/5:添加index.html/var/www/html/index.html--->85b4a3657cedSuccessfullybuild85b4a3657cedSuccessfullytaggedcentos:httpd查看[root@dockerdocker-build]#dockerimagesRepository标签图像ID创建的sizecentoshttpd85b4a3657ced45秒前280mbcentosapacheapacheapacheb8822ec8a7bb882ec8a7b20分钟前280mbcentos280mbcentos最新4706706706670670cactand镜像新生成的镜像[root@Dockerdocker-build]#dockerrun-itcentos:httpd/bin/bash[root@1188a43a4585/]#lsbinetcliblost+foundmntprocrunsrvtmpvardevhomelib64mediaoptrootsbinsysusr[root@1188a43a4585/]#rpm-qahttpdhttpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64[root@1188a43a4585/]#exitexit[root@Dockerdocker-build]#Summary以上就是Docker镜像制作方法的介绍,希望对大家有所帮助
