Docker Tomcat远程部署到容器
一:创建一个开启远程部署的tomcat容器
tomcat角色配置
1.tomcat开启远程部署,修改conf/tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?> <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0"> <role rolename="admin-gui"/> <role rolename="admin-script"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <user username="hongdada" password="hongdada" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/> </tomcat-users>
tomcat管理权限过滤配置
编辑${TOMCAT_HOME}/webapps/manager/META-INF/context.xml:
原始文件只支持本地管理:
<?xml version="1.0" encoding="UTF-8"?> <Context antiResourceLocking="false" privileged="true" docBase="${catalina.home}/webapps/manager" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" /> <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context>
Dockerfile:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
FROM openjdk:8-jre-alpine MAINTAINER hongdada "hongdaqi159505@gmail.com" ENV CATALINA_HOME /usr/local/tomcat ENV PATH $CATALINA_HOME/bin:$PATH RUN mkdir -p "$CATALINA_HOME" WORKDIR $CATALINA_HOME # let "Tomcat Native" live somewhere isolated ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR # see https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/KEYS # see also "update.sh" (https://github.com/docker-library/tomcat/blob/master/update.sh) ENV GPG_KEYS 05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23 ENV TOMCAT_MAJOR 8 ENV TOMCAT_VERSION 8.5.32 ENV TOMCAT_SHA512 fc010f4643cb9996cad3812594190564d0a30be717f659110211414faf8063c61fad1f18134154084ad3ddfbbbdb352fa6686a28fbb6402d3207d4e0a88fa9ce ENV TOMCAT_TGZ_URLS \ # https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394 https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz \ # if the version is outdated, we might have to pull from the dist/archive :/ https://www-us.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz \ https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz \ https://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz ENV TOMCAT_ASC_URLS \ https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc \ # not all the mirrors actually carry the .asc files :'( https://www-us.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc \ https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc \ https://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc RUN set -eux; \ \ apk add --no-cache --virtual .fetch-deps \ gnupg \ \ ca-certificates \ openssl \ ; \ \ export GNUPGHOME="$(mktemp -d)"; \ for key in $GPG_KEYS; do \ gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ done; \ \ success=; \ for url in $TOMCAT_TGZ_URLS; do \ if wget -O tomcat.tar.gz "$url"; then \ success=1; \ break; \ fi; \ done; \ [ -n "$success" ]; \ \ echo "$TOMCAT_SHA512 *tomcat.tar.gz" | sha512sum -c -; \ \ success=; \ for url in $TOMCAT_ASC_URLS; do \ if wget -O tomcat.tar.gz.asc "$url"; then \ success=1; \ break; \ fi; \ done; \ [ -n "$success" ]; \ \ gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz; \ tar -xvf tomcat.tar.gz --strip-components=1; \ rm bin/*.bat; \ rm tomcat.tar.gz*; \ rm -rf "$GNUPGHOME"; \ \ nativeBuildDir="$(mktemp -d)"; \ tar -xvf bin/tomcat-native.tar.gz -C "$nativeBuildDir" --strip-components=1; \ apk add --no-cache --virtual .native-build-deps \ apr-dev \ coreutils \ dpkg-dev dpkg \ gcc \ libc-dev \ make \ "openjdk${JAVA_VERSION%%[-~bu]*}"="$JAVA_ALPINE_VERSION" \ openssl-dev \ ; \ ( \ export CATALINA_HOME="$PWD"; \ cd "$nativeBuildDir/native"; \ gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ ./configure \ --build="$gnuArch" \ --libdir="$TOMCAT_NATIVE_LIBDIR" \ --prefix="$CATALINA_HOME" \ --with-apr="$(which apr-1-config)" \ --with-java-home="$(docker-java-home)" \ --with-ssl=yes; \ make -j "$(nproc)"; \ make install; \ ); \ rm -rf "$nativeBuildDir"; \ rm bin/tomcat-native.tar.gz; \ \ runDeps="$( \ scanelf --needed --nobanner --format '%n#p' --recursive "$TOMCAT_NATIVE_LIBDIR" \ | tr ',' '\n' \ | sort -u \ | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ )"; \ apk add --virtual .tomcat-native-rundeps $runDeps; \ apk del .fetch-deps .native-build-deps; \ \ # sh removes env vars it doesn't support (ones with periods) # https://github.com/docker-library/tomcat/issues/77 apk add --no-cache bash; \ find ./bin/ -name '*.sh' -exec sed -ri 's|^#!/bin/sh$|#!/usr/bin/env bash|' '{}' + # verify Tomcat Native is working properly RUN set -e \ && nativeLines="$(catalina.sh configtest 2>&1)" \ && nativeLines="$(echo "$nativeLines" | grep 'Apache Tomcat Native')" \ && nativeLines="$(echo "$nativeLines" | sort -u)" \ && if ! echo "$nativeLines" | grep 'INFO: Loaded APR based Apache Tomcat Native library' >&2; then \ echo >&2 "$nativeLines"; \ exit 1; \ fi #替换tomcat-users.xml文件 ADD tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xml ADD context.xml /usr/local/tomcat/webapps/manager/META-INF/context.xml EXPOSE 8080 CMD ["catalina.sh", "run"]
其实就是拿了一个官网的tomcat中的dockerfile
就在上面添加了一句
#替换tomcat-users.xml文件 ADD tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xml #替换context.xml文件 ADD context.xml /usr/local/tomcat/webapps/manager/META-INF/context.xml
本来是想直接在官方的tomcat上面替换tomcat-users.xml文件,但是修改过文件以后要重启tomcat,我就不知道咋弄了
运行dockerfile,打开tomcat界面时,
在浏览器中访问 http://yourIp:port/manager/text (tomcat 7、8、9) ;
http://yourIp:port/manager/html (tomcat 6)
如果出现输入账户和密码的弹框,表示成功。
2.在maven的配置文件setting.xml中servers中添加:
<servers> <server> <id>myTomcat</id> <username>hongdada</username> <password>hongdada</password> </server> </servers>
3.在springmvc项目的pom.xml中添加插件:
<!-- maven远程项目部署插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <!-- 填写自己服务器的IP地址和端口号,并且其他的不变,Tomcat7、8、9使用这个,Tomcat6使用另一个--> <url>http://18.16.202.33:8080/manager/text</url> <server>myTomcat</server> <!-- 若tomcat项目中已存在,且使"mvn tomcat7:deploy"命令必须要设置下面的代码 --> <!-- 更新项目时,仅需要执行"mvn tomcat7:redeploy"命令即可 --> <!-- 上述命令无论服务器是tomcat7、8或9,均是使用"mvn tomcat7:deploy"或"mvn tomcat7:redeploy" --> <update>true</update> <!-- 项目路径 --> <path>/${project.artifactId}</path> </configuration> </plugin>
4.部署:
命令:
D:\Development\SpringMVCDemo (master -> origin) $ mvn clean package -U -Dmaven.test.skip=true tomcat7:redeploy
或者
结果访问:
参考:
https://blog.csdn.net/boling_cavalry/article/details/70184605