介绍
介绍
大家根福哥学会了使用Dockerfile创建Docker镜像的技巧了,那么我们现在搭建服务器环境就不需要再去下载软件的源代码了,也不需要编译安装了,更加不用操心软件和操作系统的各种兼容问题了。
今天福哥带着大家来安装tomcat的环境,基于微服务思维一个Docker里面只安装最基本的软件即可,Tomcat是非常流行的免费Java解决方案,福哥最后选择的是就tomcat:9.0-jdk16-openjdk-buster这个镜像作为基础镜像了。
镜像
tag
Tomcat 9.0的tag有很多,福哥选择的是tomcat:9.0-jdk16-openjdk-buster版本,这个版本是基于debian操作系统的。
结构
照例我们先把镜像拉取下来,然后启动一个容器,看看里面都有什么,弄明白了自己才好捣鼓啊!
拉取镜像
docker pull tomcat:9.0-jdk16-openjdk-buster
启动临时容器
docker run -tid --name tomcat9.0 -h tomcat9.0 tomcat:9.0-jdk16-openjdk-buster
切入临时容器
docker exec -ti tomcat9.0 /bin/bash
查看结构
java结构。
root@tomcat9:/usr/local/tomcat# find / -iname "*java*" /usr/local/tomcat/webapps.dist/examples/WEB-INF/jsp/applet/Clock2.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/examples/ExampleTagBase.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/examples/LogTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/examples/FooTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/examples/FooTagExtraInfo.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/examples/ValuesTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/colors/ColorGameBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/el/Functions.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/ValuesBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/TileSimpleTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/ShuffleSimpleTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/EchoAttributesTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/HelloWorldSimpleTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/FindBookSimpleTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/simpletag/RepeatSimpleTag.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/BookBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/jsp2/examples/FooBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/trailers/ResponseTrailers.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/RequestHeaderExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/SessionExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/HelloWorldExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/checkbox/CheckTest.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/http2/SimpleImagePush.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/dates/JspCalendar.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/RequestInfoExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/RequestParamExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/filters/ExampleFilter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/nonblocking/ByteCounter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/nonblocking/NumberWriter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/sessions/DummyCart.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/listeners/SessionListener.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/listeners/ContextListener.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/error/Smart.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/cal/Entries.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/cal/TableBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/cal/Entry.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/cal/JspCalendar.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/num/NumberGuessBean.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/util/HTMLFilter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/util/CookieFilter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/compressionFilters/CompressionFilter.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/compressionFilters/CompressionServletResponseWrapper.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/compressionFilters/CompressionFilterTestServlet.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/compressionFilters/CompressionResponseStream.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/validators/DebugValidator.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/CookieExample.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/Stockticker.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/Async3.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/Async0.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/Async1.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/Async2.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/AsyncStockServlet.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/async/AsyncStockContextListener.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/ExamplesConfig.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/DrawMessage.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/DrawboardEndpoint.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/wsmessages/AbstractWebsocketMessage.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/wsmessages/BinaryWebsocketMessage.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/wsmessages/StringWebsocketMessage.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/wsmessages/CloseWebsocketMessage.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/Client.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/DrawboardContextListener.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/drawboard/Room.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/snake/SnakeTimer.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/snake/Direction.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/snake/Snake.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/snake/Location.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/echo/EchoStreamAnnotation.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/echo/EchoAnnotation.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java /usr/local/tomcat/webapps.dist/examples/WEB-INF/classes/ServletToJsp.java /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/jspattribute/FooBean.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/jspattribute/ShuffleSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/jspattribute/TileSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/jspattribute/HelloWorldSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/misc/EchoAttributesTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/el/ValuesBean.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/el/ValuesTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/el/Functions.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/simpletag/FindBookSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/simpletag/BookBean.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/simpletag/RepeatSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/simpletag/Functions.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsp2/simpletag/HelloWorldSimpleTag.java.html /usr/local/tomcat/webapps.dist/examples/jsp/jsptoserv/ServletToJsp.java.html /usr/local/tomcat/webapps.dist/examples/jsp/plugin/applet/Clock2.java /usr/local/tomcat/webapps.dist/examples/jsp/cal/Entry.java.html /usr/local/tomcat/webapps.dist/examples/jsp/cal/TableBean.java.html /usr/local/tomcat/webapps.dist/examples/jsp/cal/Entries.java.html /usr/local/tomcat/webapps.dist/examples/jsp/cal/JspCalendar.java.html /usr/local/tomcat/webapps.dist/docs/appdev/sample/src/mypackage/Hello.java /usr/local/openjdk-16/jmods/java.sql.rowset.jmod /usr/local/openjdk-16/jmods/java.xml.crypto.jmod /usr/local/openjdk-16/jmods/java.net.http.jmod /usr/local/openjdk-16/jmods/java.datatransfer.jmod /usr/local/openjdk-16/jmods/java.security.jgss.jmod /usr/local/openjdk-16/jmods/java.smartcardio.jmod /usr/local/openjdk-16/jmods/java.base.jmod /usr/local/openjdk-16/jmods/java.management.rmi.jmod /usr/local/openjdk-16/jmods/java.instrument.jmod /usr/local/openjdk-16/jmods/java.scripting.jmod /usr/local/openjdk-16/jmods/java.se.jmod /usr/local/openjdk-16/jmods/java.prefs.jmod /usr/local/openjdk-16/jmods/java.naming.jmod /usr/local/openjdk-16/jmods/java.xml.jmod /usr/local/openjdk-16/jmods/java.rmi.jmod /usr/local/openjdk-16/jmods/java.logging.jmod /usr/local/openjdk-16/jmods/java.security.sasl.jmod /usr/local/openjdk-16/jmods/jdk.javadoc.jmod /usr/local/openjdk-16/jmods/java.sql.jmod /usr/local/openjdk-16/jmods/java.compiler.jmod /usr/local/openjdk-16/jmods/java.transaction.xa.jmod /usr/local/openjdk-16/jmods/java.management.jmod /usr/local/openjdk-16/jmods/java.desktop.jmod /usr/local/openjdk-16/bin/javac /usr/local/openjdk-16/bin/javap /usr/local/openjdk-16/bin/java /usr/local/openjdk-16/bin/javadoc /usr/local/openjdk-16/lib/libjavajpeg.so /usr/local/openjdk-16/lib/libjava.so /usr/local/openjdk-16/conf/security/java.policy /usr/local/openjdk-16/conf/security/java.security /usr/local/openjdk-16/legal/java.security.sasl /usr/local/openjdk-16/legal/java.security.jgss /usr/local/openjdk-16/legal/java.sql /usr/local/openjdk-16/legal/java.xml.crypto /usr/local/openjdk-16/legal/java.se /usr/local/openjdk-16/legal/java.instrument /usr/local/openjdk-16/legal/java.xml /usr/local/openjdk-16/legal/java.naming /usr/local/openjdk-16/legal/java.transaction.xa /usr/local/openjdk-16/legal/java.management /usr/local/openjdk-16/legal/java.net.http /usr/local/openjdk-16/legal/java.smartcardio /usr/local/openjdk-16/legal/java.datatransfer /usr/local/openjdk-16/legal/jdk.javadoc /usr/local/openjdk-16/legal/java.scripting /usr/local/openjdk-16/legal/java.logging /usr/local/openjdk-16/legal/java.base /usr/local/openjdk-16/legal/java.compiler /usr/local/openjdk-16/legal/java.rmi /usr/local/openjdk-16/legal/java.sql.rowset /usr/local/openjdk-16/legal/java.management.rmi /usr/local/openjdk-16/legal/java.prefs /usr/local/openjdk-16/legal/java.desktop find: ‘/proc/1/map_files’: Operation not permitted find: ‘/proc/46/map_files’: Operation not permitted find: ‘/proc/52/map_files’: Operation not permitted
tomcat结构。
root@tomcat9:/usr/local/tomcat# find / -iname "*tomcat*" /usr/local/tomcat /usr/local/tomcat/conf/tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xsd /usr/local/tomcat/bin/tomcat-juli.jar /usr/local/tomcat/lib/tomcat-dbcp.jar /usr/local/tomcat/lib/tomcat-jni.jar /usr/local/tomcat/lib/tomcat-i18n-de.jar /usr/local/tomcat/lib/tomcat-i18n-zh-CN.jar /usr/local/tomcat/lib/tomcat-i18n-pt-BR.jar /usr/local/tomcat/lib/tomcat-i18n-ko.jar /usr/local/tomcat/lib/tomcat-i18n-es.jar /usr/local/tomcat/lib/tomcat-api.jar /usr/local/tomcat/lib/tomcat-jdbc.jar /usr/local/tomcat/lib/tomcat-util-scan.jar /usr/local/tomcat/lib/tomcat-i18n-ru.jar /usr/local/tomcat/lib/tomcat-i18n-fr.jar /usr/local/tomcat/lib/tomcat-coyote.jar /usr/local/tomcat/lib/tomcat-i18n-cs.jar /usr/local/tomcat/lib/tomcat-websocket.jar /usr/local/tomcat/lib/tomcat-util.jar /usr/local/tomcat/lib/tomcat-i18n-ja.jar /usr/local/tomcat/webapps.dist/ROOT/tomcat.svg /usr/local/tomcat/webapps.dist/ROOT/tomcat.css /usr/local/tomcat/webapps.dist/docs/appdev/sample/web/images/tomcat.gif /usr/local/tomcat/webapps.dist/docs/images/tomcat.gif /usr/local/tomcat/webapps.dist/docs/images/tomcat.png /usr/local/tomcat/webapps.dist/host-manager/images/tomcat.svg /usr/local/tomcat/webapps.dist/manager/images/tomcat.svg find: ‘/proc/1/map_files’: Operation not permitted find: ‘/proc/46/map_files’: Operation not permitted find: ‘/proc/51/map_files’: Operation not permitted
tomcat配置文件。
/usr/local/tomcat/conf/server.xml
tomcat应用跟目录。
/usr/local/tomcat/webapps
默认情况下webapps里面是空的,包括manager之类的默认工具应用都在webapps.dist下面。
root@tomcat9:/usr/local/tomcat# ls /usr/local/tomcat/webapps.dist/ ROOT docs examples host-manager manager
使用curl测试一下,看到的是404错误页面。
将webapps.dist下面的manager放到webapps下面。
mv /usr/local/tomcat/webapps.dist/manager/ /usr/local/tomcat/webapps/
然后重新启动容器。
[root@dev php7.4]# docker stop tomcat9.0 tomcat9.0 [root@dev php7.4]# docker start tomcat9.0 tomcat9.0
再使用curl测试一下,可以看到认证页面了。
停止临时容器
docker stop tomcat9.0
删除临时容器
docker rm tomcat9.0
Dockerfile
最后福哥把前面的设置命令整理到一起写成Dockerfile,这样大家就可以通过Dockerfile安装环境了。
server.xml
福哥把默认的server.xml配置文件里面的注释都去掉了,没有修改里面默认参数,只是把应用根目录从webapps改成/tongfu.net/web了。
<?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="/tongfu.net/web" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server>
Dockerfile
创建Dockerfile,将server.xml直接覆盖默认配置文件/usr/local/tomcat/conf/server.xml。
FROM tomcat:9.0-jdk16-openjdk-buster MAINTAINER Andy Bogate MAINTAINER tongfu@tongfu.net MAINTAINER https://tongfu.net/dockerfile MAINTAINER 2021/5/24 MAINTAINER v1.0.0 EXPOSE 8080 # config ADD server.xml /usr/local/tomcat/conf/server.xml
总结
今天福哥带着大家使用Dockerfile搭建了Tomcat微服务环境了,可以发现使用Dockerfile方式搭建环境我们真的只需要关心我们需要关心的部分,繁琐的编译参数、依赖库、环境参数等等一系列的问题基础镜像都给我们解决好了。
下一课,福哥会带着搭建学习使用Dockerfile搭建Nginx+Java环境,敬请期待~~