二、DockerFile
1. 什么是dockerfile?
dockerfile是用来构建docker镜像的文件,就是一个命令参数脚本
构建步骤:
- 编写一个dockerfile文件
- docker build构建成为一个镜像
- docker run运行镜像
- docker push发布镜像(DockerHub、阿里云镜像仓库)
查看官方是如何制作镜像文件的:https://hub.docker.com/
当点击daockerfile的链接跳转到GitHub上时,可以看到官方dockerfile的源码
2. DockerFile构建过程
基础知识:
- 每个关键字(指令)都必须是大写字母
- 指令是从上到下顺序执行的
- “#” 表示注释
- 每一个指令都会创建一个新的镜像层,并提交
dockerfile是面向开发的,以后要发布项目、做镜像时就需要编写dockerfile文件
步骤:开发、部署、运维
- DockerFile:构建文件,定义了一切的步骤
- DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品
- DockerContainer:容器就是镜像运行起来提供服务的(使用镜像)
3. DockerFile的指令
FROM // 基础镜像,一切从这里开始构建 MAINTAINER // 镜像是谁写的,姓名+邮箱 RUN // 镜像构建时需要运行的命令 ADD // 步骤:其它镜像,其它镜像的压缩包就是添加的内容 WORKDIR // 镜像的工作目录(可以自己指定) VOLUME // 挂载的目录 EXPOSE // 暴露端口配置 CMD // 指定这个容器启动时要运行的命令,只有最后一个会生效,可被替代 ENTRYPOINT // 指定这个容器启动时要运行的命令,可以追加命令 ONBUILD // 当构建一个被继承的DockerFile,这个时候就会运行ONBUILD的命令 COPY // 类似ADD命令,将文件拷贝到镜像中 ENV // 构建的时候设置环境变量
4. 实战测试
Centos8.0官方的dockerfile文件
Docker Hub中绝大部分镜像都是从scratch镜像中过来的,然后配置需要的软件和配置来进行构建
# 跳转到home目录下 [root@VM-16-16-centos ~]# cd /home # 新建dockerfile的目录 [root@VM-16-16-centos home]# mkdir dockerfile # 跳转到dockerfile目录中 [root@VM-16-16-centos home]# cd dockerfile # 新建并编辑dockerfile文件 [root@VM-16-16-centos dockerfile]# vim mydockerfile ------------------- dockerfile文件内容 ------------------- FROM centos MAINTAINER shijimodejiagoushi<1072876976@qq.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 90 CMD echo $MYPATH CMD echo "---------- end ----------" CMD /bin/bash ------------------- dockerfile文件内容 -------------------
接下来对通过dockerfile构建成功的镜像进行查看与测试
[root@VM-16-16-centos dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 1.0 b87f31e57281 13 minutes ago 340MB # 这里就是我们通过dockerfile构建的镜像 centos 1.0 30a7c5c2798f 2 days ago 231MB halohub/halo 1.4.13 07dafc8f2711 8 days ago 325MB tomcat latest b0e0b0a92cf9 3 weeks ago 680MB centos latest 5d0da3dc9764 2 months ago 231MB mysql 8.0.13 102816b1ee7d 2 years ago 486MB # 进入我们构建的镜像进行测试 [root@VM-16-16-centos dockerfile]# docker run -it mycentos:1.0 # 查看当前镜像的工作路径 [root@df7b4314eb48 local]# pwd /usr/local
三、综合实战:制作Tomcat镜像
1. 准备压缩包
制作tomcat镜像需要使用到Tomcat的压缩包以及JDK的压缩包
下载链接:
- Tomcat8.5.7:https://tomcat.apache.org/download-80.cgi
JDK8:https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html
2. 编写Dockerfile文件
官方命名为Dockerfile
,build时就会自动寻找这个文件,就不需要 -f
来进行指定了
在dockerfile文件中,使用ADD命令时添加的是压缩包的话,会自动进行解压
[root@VM-16-16-centos tomcat-dockerfile]# touch readme.txt [root@VM-16-16-centos tomcat-dockerfile]# vim Dockerfile [root@VM-16-16-centos tomcat-dockerfile]# cat Dockerfile FROM centos MAINTAINER shijimodejiagoushi<1072876976@qq.com> COPY readme.txt /usr/local/readme.txt ADD jdk-8u202-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-8.5.72.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.72 ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.72 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-8.5.72/bin/startup.sh && tail -F /url/local/apache-tomcat-8.5.72/bin/logs/catalina.out
3. 构建镜像
因为此时的dockerfile文件的名称为Dockerfile,与官方的名称相同,因此直接构建生成镜像即可,无需指定dockerfile的路径
[root@VM-16-16-centos tomcat-dockerfile]# docker build -t diytomcat . Sending build context to Docker daemon 157.4MB Step 1/15 : FROM centos ---> 5d0da3dc9764 Step 2/15 : MAINTAINER shijimodejiagoushi<1072876976@qq.com> ---> Using cache ---> 4102f7944f69 Step 3/15 : COPY readme.txt /usr/local/readme.txt ---> 72faff07d65d Step 4/15 : ADD jdk-8u311-linux-x64.tar.gz /usr/local/ ---> 469af64c2dff Step 5/15 : ADD apache-tomcat-8.5.72.tar.gz /usr/local/ ---> d8772f248742 Step 6/15 : RUN yum -y install vim ---> Running in fde58a7e7aea CentOS Linux 8 - AppStream 16 MB/s | 9.6 MB 00:00 CentOS Linux 8 - BaseOS 4.7 MB/s | 8.5 MB 00:01 CentOS Linux 8 - Extras 24 kB/s | 10 kB 00:00 Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k which x86_64 2.21-12.el8 baseos 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: (1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 4.2 MB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 12 MB/s | 48 kB 00:00 (3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 48 MB/s | 1.4 MB 00:00 (4/5): vim-common-8.0.1763-15.el8.x86_64.rpm 90 MB/s | 6.3 MB 00:00 (5/5): which-2.21-12.el8.x86_64.rpm 211 kB/s | 49 kB 00:00 -------------------------------------------------------------------------------- Total 5.3 MB/s | 7.8 MB 00:01 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64 vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch which-2.21-12.el8.x86_64 Complete! Removing intermediate container fde58a7e7aea ---> 25325ca2577f Step 7/15 : ENV MYPATH /usr/local ---> Running in a9c36972ce5a Removing intermediate container a9c36972ce5a ---> 63bb0ba56832 Step 8/15 : WORKDIR $MYPATH ---> Running in d09e1861e9ab Removing intermediate container d09e1861e9ab ---> 06619c0bcd1e Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8 ---> Running in 5ef54db767c4 Removing intermediate container 5ef54db767c4 ---> 71dea4ae1e23 Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ---> Running in 015882797b85 Removing intermediate container 015882797b85 ---> cf09007b5767 Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.72 ---> Running in 1109e02165b4 Removing intermediate container 1109e02165b4 ---> 77bf619c4bec Step 12/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.72 ---> Running in 90cd718a8a5c Removing intermediate container 90cd718a8a5c ---> ca611958f271 Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Running in f07f036febf0 Removing intermediate container f07f036febf0 ---> b83a4ed77e8d Step 14/15 : EXPOSE 8080 ---> Running in 3c1de3e2b0e8 Removing intermediate container 3c1de3e2b0e8 ---> 4df529a4e623 Step 15/15 : CMD CATALINA_HOME /usr/local/apache-tomcat-8.5.72/bin/startup.sh && tail --F /url/local/apache-tomcat-8.5.72/bin/logs/catalina.out ---> Running in 8206acc4eb6c Removing intermediate container 8206acc4eb6c ---> 59ed85310171 Successfully built 59ed85310171 Successfully tagged diytomcat:latest
此时可以看到镜像中已经存在刚才由dockerfile构建出的镜像,接下来启动生成的镜像容器并且将容器的工作目录和日志目录挂载到主机上
[root@VM-16-16-centos home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest 59ed85310171 46 minutes ago 686MB mycentos 1.0 b87f31e57281 3 hours ago 340MB centos 1.0 30a7c5c2798f 2 days ago 231MB halohub/halo 1.4.13 07dafc8f2711 8 days ago 325MB tomcat latest b0e0b0a92cf9 3 weeks ago 680MB centos latest 5d0da3dc9764 2 months ago 231MB mysql 8.0.13 102816b1ee7d 2 years ago 486MB [root@VM-16-16-centos home]# docker run -d -p 8080:8080 --name diytomcat -v /home/tomcat-dockerfile/build/tomcat/test:/usr/local/apache-tomcat-8.5.72/webapps/test -v /home/tomcat-dockerfile/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-8.5.72/logs diytomcat 3cb91b448a6d0b836086f15bb763d2bc00944b4f8d9db6c7426d6b62fd4c439f
接下来通过docker exec -it 容器Id
进入到容器的内部
在浏览器上输入ip地址:端口号
查看是否可以访问到tomcat主页
访问成功!
由于我们做了数据卷的挂载,因此我们可以在本地直接编写项目,接下来在本地进行修改文件,查看数据卷的挂载是否成功。
- 在主机的test目录下新建WEB-INF目录
新建web.xml文件
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> </web-app>
新建index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="utf-8"> <title>Hello,世纪末的架构师</title> </head> <body> <h2>Hello World!</h2> </body> </html>
总结
以上内容就是Docker基础入Dockerfile和容器数据卷的学习,通过docker基础的学习,越发感觉到docker的精妙,而docker命令更是基础中的基础,与Linux命令相似。在学习docker的过程中,对Linux系统的命令也做了一定的回顾与熟练。接下来继续学习docker的进阶知识,docker网络以及之后的docker-compose等内容。
人生如骑自行车,方向掌握在自己手中,用力蹬才能前进,一路上不管逆风还是顺风,全凭自己掌握。你的未来取决于你的现在,有多少付出就有多少收获!