我们想要将一个war包打包到docker的镜像里面,思路和过程都非常简单
1、项目代码打war包
2、编写一个dockerfile文件
(1)以tomcat镜像为基础
(2)复制tomcat的配置文件到镜像中(这里是为了修改tomcat的端口)
(3)复制war包到镜像中tomcat的工作目录
3、执行docker打包命令
编写dockerfile
1、在war包的同级目录下准备server.xml ,这里我是为了改项目的端口,从镜像里面拿出tomcat的server.xml修改的,如果使用默认的端口,这步可以跳过
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!-- APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<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">
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<!-- 这里原本是8080 ,我改成了28480
-->
<Connector port="28480" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
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>
2、准备Dockerfile文件
touch Dockerfile
vim Dockerfile
#基于tomcat镜像
FROM tomcat
#维护人
MAINTAINER chow
#更新日期
ENV REFRESHED_AT 2021-07-18
#将server.xml替换tomcat镜像中的server.xml(这里是为了修改tomcat的端口),如果使用默认端口可删掉
ADD server.xml /usr/local/tomcat/conf
#将war包放到镜像的tomcat中
ADD my-war.war /usr/local/tomcat/webapps/ROOT.war
#公开端口 这里我tomcat的端口为28480,默认为8080
EXPOSE 28480
#设置启动命令
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
运行docker打包命令
docker build -t test:1.0.0 .
注意:
1、test是镜像名
2、1.0.0是版本名
3、不要忘了后面那个 . 指当前目录的意思
运行
docker images
可以看到我们刚才打包的镜像
测试
用我们刚才打包的镜像运行试试
docker run -id --name=test -p 28480:28480 test:1.0.0
查看容器,加上-a可以看到所有的容器,不加-a 只能看到运行中的容器
docker ps -a
查看容器输出日志
docker logs 容器id
查看容器输出日志跟随日志
docker logs -f 容器id