镜像提交时会同时提交数据卷数据么
这个问题困惑了我一段时间,不多说,直接试试:
基于centos镜像运行一个容器,并指定路径挂载数据卷
[root@192 /]# docker run -it -v /home/centos/test:/usr/var centos [root@03a5686af4fa /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@03a5686af4fa /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@03a5686af4fa /]# cd usr [root@03a5686af4fa usr]# ls bin games include lib lib64 libexec local sbin share src tmp var [root@03a5686af4fa usr]# cd var [root@03a5686af4fa var]# ls a.java
宿主机增加文件
[root@192 ~]# cd /home [root@192 home]# ls centos dockerfiles mysql test.java tml-1 tml-not-add.java tomcat volume_centos [root@192 home]# cd centos/ [root@192 centos]# ls test [root@192 centos]# cd test [root@192 test]# touch a.java [root@192 test]#
依据当前镜像和容器提交一个新镜像
[root@03a5686af4fa var]# docker commit -m "volume是否会被带着提交" 03a5686af4fa volume-centos:1.0 bash: docker: command not found [root@03a5686af4fa var]# exit exit [root@192 /]# docker commit -m "volume是否会被带着提交" 03a5686af4fa volume-centos:1.0 sha256:caf2866a962e5a332c93e28fec00dd5b67e578267af70d909c8002ee10f17b9f [root@192 /]# ls backup bin boot dev etc home jdk-8u171-linux-x64.tar.gz kafka lib lib64 media mnt opt proc root run sbin soft srv sys tmp usr var [root@192 /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE volume-centos 1.0 caf2866a962e 15 seconds ago 231MB docker-image-volume 1.0 3d736800dd60 3 hours ago 204MB tianmaolin/tml-mydockerfile-tomcat 1.0 94dbe2d0cb91 4 hours ago 799MB tml-mydockerfile-tomcat 1.0 94dbe2d0cb91 4 hours ago 799MB tml-dockerfile-tomcat 1.0 2a1bbcae34bf 5 hours ago 799MB tml-entrypoint-test 0.1 79cf0d9a1240 6 hours ago 204MB tml-cmd-test 0.1 2b54bee0bae6 6 hours ago 204MB my-centos-image 0.1 e4e087ce52a3 10 hours ago 580MB <none> <none> 55ac5aa9c06f 10 hours ago 231MB mytomcat 1.0 251e3ac9aff1 7 days ago 684MB nginx latest 605c77e624dd 8 weeks ago 141MB tomcat latest fb5657adc892 2 months ago 680MB mysql 5.7 c20987f18b13 2 months ago 448MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB centos 7 eeb6ee3f44bd 5 months ago 204MB centos latest 5d0da3dc9764 5 months ago 231MB portainer/portainer latest 580c0e4e98b0 11 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 23 months ago 791MB elasticsearch latest 5acf0e8da90b 3 years ago 486MB [root@192 /]# docker history volume-centos Error response from daemon: No such image: volume-centos:latest [root@192 /]# docker history volume-centos:1.0 IMAGE CREATED CREATED BY SIZE COMMENT caf2866a962e 44 seconds ago /bin/bash 112B volume是否会被带着提交 5d0da3dc9764 5 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 5 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
依据新镜像运行容器并查看容器数据卷目录
[root@192 /]# docker run -it volume-centos:1.0 /bin/bash [root@06b20f3507cc /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@06b20f3507cc /]# cd usr [root@06b20f3507cc usr]# ls bin games include lib lib64 libexec local sbin share src tmp var [root@06b20f3507cc usr]# cd var [root@06b20f3507cc var]# ls
可以看到并没有a.java
文件,但是var文件夹创建上了,同时查看下该容器元数据
"Mounts": [],
由此可见volume数据卷是独立于联合文件系统的。
依据dockerfile制作tomcat镜像并发布
按照如下步骤进行tomcat镜像制作及发布:
1 准备本地的压缩包文件
使用appNode将两个tomcat镜像构建用到的压缩包上传到/home/tomcat
目录下
目录如下:
[root@192 ~]# cd /home [root@192 home]# ls dockerfiles mysql test.java tml-1 tml-not-add.java tomcat volume_centos [root@192 home]# cd tomcat [root@192 tomcat]# ls apache-tomcat-8.5.55.tar.gz jdk-8u321-linux-x64.tar.gz [root@192 tomcat]# touch readme.txt [root@192 tomcat]# ls apache-tomcat-8.5.55.tar.gz jdk-8u321-linux-x64.tar.gz readme.txt [root@192 tomcat]#
2 创建一个dockerfile文件
[root@192 tomcat]# vim Dockerfile [root@192 tomcat]#
3 编写dockerfile文件
编写dockerfile文件,文件名使用官方命名:Dockerfile ,build的时候会默认寻找当前目录下的文件,不需要使用-f参数指定
FROM centos:7 MAINTAINER tml<22222222@qq.com> COPY readme.txt /usr/local/readme.txt ADD jdk-8u321-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-8.5.55.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_321 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.55 ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.55 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-8.5.55/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.55/bin/logs/catalina.out
4 使用该Dockerfile构建镜像
执行命令构建镜像:docker build -t tml-mydockerfile-tomcat:1.0 .
,执行过程如下:
[root@192 tomcat]# docker build -t tml-mydockerfile-tomcat:1.0 . Sending build context to Docker daemon 157.2MB Step 1/15 : FROM centos:7 ---> eeb6ee3f44bd Step 2/15 : MAINTAINER tml<22222222@qq.com> ---> Running in c0cf98ad1656 Removing intermediate container c0cf98ad1656 ---> 37e75c1c3fbf Step 3/15 : COPY readme.txt /usr/local/readme.txt COPY failed: file not found in build context or excluded by .dockerignore: stat readme.txt: file does not exist [root@192 tomcat]# cd /home [root@192 home]# ls dockerfiles mysql test.java tml-1 tml-not-add.java tomcat volume_centos [root@192 home]# cd tomcat [root@192 tomcat]# touch readme.txt [root@192 tomcat]# docker build -t tml-mydockerfile-tomcat:1.0 . Sending build context to Docker daemon 157.2MB Step 1/15 : FROM centos:7 ---> eeb6ee3f44bd Step 2/15 : MAINTAINER tml<22222222@qq.com> ---> Using cache ---> 37e75c1c3fbf Step 3/15 : COPY readme.txt /usr/local/readme.txt ---> c00a06e0b624 Step 4/15 : ADD jdk-8u321-linux-x64.tar.gz /usr/local/ ---> 7843775f3843 Step 5/15 : ADD apache-tomcat-8.5.55.tar.gz /usr/local/ ---> ca57ff38958c Step 6/15 : RUN yum -y install vim ----------------------------------中间过程省略-------------------------------------------------- Step 7/15 : ENV MYPATH /usr/local ---> Running in 15432f278bd4 Removing intermediate container 15432f278bd4 ---> 90ce935c42b5 Step 8/15 : WORKDIR $MYPATH ---> Running in c218f55f9975 Removing intermediate container c218f55f9975 ---> 0248a9e1fccd Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_321 ---> Running in 38c4fb4b8ec0 Removing intermediate container 38c4fb4b8ec0 ---> aa704765f66f Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ---> Running in 047dc22054b3 Removing intermediate container 047dc22054b3 ---> 45de2f0792a6 Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.55 ---> Running in 4be3a29dae94 Removing intermediate container 4be3a29dae94 ---> cb0a033be5cf Step 12/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.55 ---> Running in a16bb8c663e7 Removing intermediate container a16bb8c663e7 ---> 66bb078fc3e1 Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Running in e86d6426d09f Removing intermediate container e86d6426d09f ---> 69cf63889a88 Step 14/15 : EXPOSE 8080 ---> Running in 0ccebe86d126 Removing intermediate container 0ccebe86d126 ---> d211c04efccb Step 15/15 : CMD /usr/local/apache-tomcat-8.5.55/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.55/bin/logs/catalina.out ---> Running in 2e305079d51f Removing intermediate container 2e305079d51f ---> 94dbe2d0cb91 Successfully built 94dbe2d0cb91 Successfully tagged tml-mydockerfile-tomcat:1.0 [root@192 tomcat]#
5 启动生成的镜像,构建Tomcat容器
这里设置了数据卷,宿主机的/home/tomcat/test
对应该容器的/usr/local/apache-tomcat-8.5.55/webapps/test
。这样关于test项目的修复只需要在宿主机上修改就可以了,不需要进入到容器中修改。
[root@192 test]# docker run -d -p 6680:8080 --name tml-mydockerfile-tomcat-contanier -v /home/tomcat/test:/usr/local/apache-tomcat-8.5.55/webapps/test tml-mydockerfile-tomcat:1.0 08e1e1d3388ed9b9932f727868ffe6d8c027d4b4bbc92042667474e7e6f94ff4 [root@192 test]#
在/home/tomcat/test
的目录下,新建index.html
测试Tomcat是否能正常使用。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>tml,hello</title> </head> <body> <h1>tml 这是你自制的tomcat镜像,访问一下</h1> <p>恭喜你访问成功</p> </body> </html>
可以看到容器内部地址:
[root@192 test]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 08e1e1d3388e tml-mydockerfile-tomcat:1.0 "/bin/sh -c '/usr/lo…" 33 seconds ago Up 31 seconds 0.0.0.0:6680->8080/tcp, :::6680->8080/tcp tml-mydockerfile-tomcat-contanier b4c122ea9c7e nginx "/docker-entrypoint.…" 19 hours ago Up 19 hours 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx-ro 0b24ae53e7fc nginx "/docker-entrypoint.…" 19 hours ago Up 19 hours 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx-tml-juming a872a0802014 nginx "/docker-entrypoint.…" 19 hours ago Up 19 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx-tml-volume 33fe5e65f769 mysql:5.7 "docker-entrypoint.s…" 24 hours ago Up 24 hours 33060/tcp, 0.0.0.0:6603->3306/tcp, :::6603->3306/tcp mysql-5.7-tml 78664208a245 portainer/portainer "/portainer" 8 days ago Up 7 days 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp thirsty_gauss 2f59536a92da tomcat "catalina.sh run" 8 days ago Up 7 days 0.0.0.0:3335->8080/tcp, :::3335->8080/tcp tomcat-tml [root@192 ~]# docker exec -it 08e1e1d3388e /bin/bash [root@f2dd05532e5f local]# ls readme.txt [root@f2dd05532e5f local]# cd .. [root@f2dd05532e5f user]# ls local [root@f2dd05532e5f user]# cd .. [root@f2dd05532e5f /]# ls anaconda-post.log dev home lib64 mnt proc run srv tmp usr bin etc lib media opt root sbin sys user var [root@f2dd05532e5f /]# cd /usr [root@f2dd05532e5f usr]# ls bin etc games include lib lib64 libexec local sbin share src tmp [root@f2dd05532e5f usr]# cd local [root@f2dd05532e5f local]# ls apache-tomcat-8.5.55 etc include lib libexec share bin games jdk1.8.0_321 lib64 sbin src [root@f2dd05532e5f local]# cd apache-tomcat-8.5.55/ [root@f2dd05532e5f apache-tomcat-8.5.55]# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs webapps CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib temp work [root@f2dd05532e5f apache-tomcat-8.5.55]# cd webapps/ [root@f2dd05532e5f webapps]# ls ROOT docs examples host-manager manager test [root@f2dd05532e5f webapps]# cd test/ [root@f2dd05532e5f test]# ls index.html [root@f2dd05532e5f test]#
6 容器访问测试
访问下容器看看效果,页面请求:
7 发布镜像到DockerHub
登录DockerHub官网进行注册,因为之前我已经注册过了,所以这里直接登录:
[root@192 ~]# docker login -u tianmaolin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@192 ~]#
登录后直接进行推送即可,使用docker push命令推送镜像到DockerHub上的仓库
[root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tml-mydockerfile-tomcat 1.0 94dbe2d0cb91 15 minutes ago 799MB tml-dockerfile-tomcat 1.0 2a1bbcae34bf About an hour ago 799MB tml-entrypoint-test 0.1 79cf0d9a1240 2 hours ago 204MB tml-cmd-test 0.1 2b54bee0bae6 2 hours ago 204MB my-centos-image 0.1 e4e087ce52a3 6 hours ago 580MB <none> <none> 55ac5aa9c06f 6 hours ago 231MB mytomcat 1.0 251e3ac9aff1 7 days ago 684MB nginx latest 605c77e624dd 8 weeks ago 141MB tomcat latest fb5657adc892 2 months ago 680MB mysql 5.7 c20987f18b13 2 months ago 448MB mysql latest 3218b38490ce 2 months ago 516MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB centos 7 eeb6ee3f44bd 5 months ago 204MB centos latest 5d0da3dc9764 5 months ago 231MB portainer/portainer latest 580c0e4e98b0 11 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 23 months ago 791MB elasticsearch latest 5acf0e8da90b 3 years ago 486MB [root@192 ~]# docker tag 94dbe2d0cb91 tianmaolin/tml-mydockerfile-tomcat:1.0 [root@192 ~]# docker push tianmaolin/tml-mydockerfile-tomcat:1.0 The push refers to repository [docker.io/tianmaolin/tml-mydockerfile-tomcat] ac01855d7d2a: Pushed c7e4d1ddfc26: Pushed 38ed04c6f8af: Pushed 6b9f15b17790: Pushed 174f56854903: Mounted from library/centos 1.0: digest: sha256:d829761d327aea380c934da8c58873843cfe0e1872d5cdb958afc2557b268db0 size: 1373 [root@192 ~]#
因为push的时候,镜像名前面需要加上用户名(tianmaolin,如果用户名不是当前登录用户则会拒绝push请求),所以需要使用命令docker tag 镜像名 新的镜像名复制出一份镜像重新打个Tag,访问dockerhub可以看到镜像已经推送完成了:
可以看到该镜像的layer:
FROM的基础镜像其实是引用了三层layer,所以镜像从另一种意义上看是一组指令集合。
总结一下
梳理下各个概念,其实Dockerfile就是一个镜像编排蓝图,把一个镜像Dockerimage的整体结构编排出来,编排出来后可以基于该镜像进行容器运行,容器里的数据目录可以映射挂载到宿主机的磁盘地址。镜像为一个只读文件系统,容器为基于镜像的读写层,通过commit命令可以将镜像和基于该镜像的容器二次制作一个新镜像,自己编写制作的镜像也可以推送到DockerHub供别人下载使用。