摘要
在认识Dockerfile的基础功能之后,即一个用基础镜像来构建新镜像的文本文件,就需要在实际工作中使用其灵活便利的操作来提升我们的工作效率了,这里演示在Tomcat里运行一个程序的过程,以此来感受Docker带给我们的便利。
通常以往的操作,或者目前大部分的操作依然还是这样:在一个服务器上安装 JDK 和 Tomcat,然后上传war包到Tomcat指定目录下,或者一个jar包,直接运行即可。这样可能一个服务器就只能安装一个JDK和一个Tomcat,当然多个也是可以的,只不过非常麻烦,这个Dockerfile就可以完美解决,并且互相毫不影响。当然这里就只是简单举个例子,实时上还有其他很多不同之处,都有非常优秀的一面。
编写Dockerfile
这里我们直接编写Dockerfile文件,首先要准备Jdk和Tomcat安装包
记住创建一个空白的目录,然后这个目录只存放我们需要的文件
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[root@hadoop101 ~]# cd /home/docker_test/docker_files/mytomcat
[root@hadoop101 mytomcat]# ll
total 155832
-r-------- 1 root root 11560971 Apr 21 23:49 apache-tomcat-9.0.62.tar.gz
-rw-r--r-- 1 root root 1099 Apr 22 00:29 Dockerfile
-r-------- 1 root root 148003999 Apr 21 23:47 jdk-8u331-linux-x64.tar.gz
-rw-r--r-- 1 root root 0 Apr 22 00:20 readme.txt
[root@hadoop101 mytomcat]#</pre>
这里我们把jdk和tomcat安装包放在这个目录下,然后加一个说明文档。
下面是Dockerfile文件具体内容:
<pre class="prettyprint hljs dockerfile" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">FROM centos
MAINTAINER zhangbaohpu
COPY readme.txt /usr/local/readme.txt
这里会自动解压
ADD jdk-8u331-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-9.0.62.tar.gz /usr/local/
ENV mypath /usr/local
WORKDIR $mypath
将原有的yum配置备份下
RUN tar cvf /etc/yum.repos.d.tar /etc/yum.repos.d
修改yum源配置,
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
删除掉多于的repo文件
RUN rm -rf /etc/yum.repos.d/CentOS-Linux-*
使yum配置更新
RUN yum clean all RUN yum makecache
下载需要的工具类
RUN yum -y install vim
设置环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_331
ENV CLASSPATH JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.62
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.62
ENV PATH JAVA_HOME/bin:CATALINA_HOME/bin
对外暴露端口
EXPOSE 8080
容器启动之后再启动tomcat,并打开日志
CMD /usr/local/apache-tomcat-9.0.62/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.62/logs/catalina.out</pre>
注:最新默认的Centos版本是8,自带的yum源已经失效,所以在这里我们顺便修改下yum源,然后再下载其他工具。
构建镜像
Dockerfile文件编写完成之后,就可以构建自己的镜像了,切记在Dockerfile所在目录下执行。
<pre class="prettyprint hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[root@hadoop101 mytomcat]# docker build -t mytomcat .
具体构建过程太长就不再展示了,只要最后有两个Successfully就表示构建完成。
[root@hadoop101 mytomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest b9cee7d729a7 10 minutes ago 697MB</pre>
运行镜像
有了我们自己自定义的镜像之后,就可以运行一个镜像实例,然后部署我们实际的应用程序
<pre class="prettyprint hljs gradle" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[root@hadoop101 mytomcat]# docker run -d -p 9090:8080 --name baotomcat -v /home/zhangbao/build/tomcat/test:/usr/local/apache-tomcat-9.0.62/webapps/test -v /home/zhangbao/build/tomcat/logs:/usr/local/apache-tomcat-9.0.62/logs mytomcat</pre>
访问 http://192.168.88.71:9090/ 就可以看到熟悉的页面了
命令解释:
-p 9090:8080 将容器内端口映射到宿主机端口
--name baotomcat 容器名称
-v /home/zhangbao/build/tomcat/test:/usr/local/apache-tomcat-9.0.62/webapps/test 将程序test根目录挂载到宿主机的指定目录下,前面为宿主机目录
-v /home/zhangbao/build/tomcat/logs:/usr/local/apache-tomcat-9.0.62/logs 挂载日志目录
最后一个是指定运行的镜像
部署项目
下面我们可以在宿主机发布程序了,因为部署程序test已经挂载到宿主机上了
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[root@hadoop101 mytomcat]# cd /home/zhangbao/build/tomcat/
[root@hadoop101 tomcat]# ll
total 0
drwxr-xr-x 2 root root 197 Apr 22 22:26 logs
drwxr-xr-x 2 root root 6 Apr 22 22:26 test
[root@hadoop101 tomcat]# cd test
[root@hadoop101 test]# ll
总用量 4
-rw-r--r-- 1 root root 266 4月 22 23:46 index.jsp
drwxr-xr-x 2 root root 21 4月 22 23:52 WEB-INF</pre>
index.jsp
<pre class="prettyprint hljs erb" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html></pre>
web.xml
<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app></pre>
网友评论