Docker构建tomcat镜像并发布web应用

该tomcat的构建基于上篇文章中sun server jre 8

一、编写Dockerfile文件


FROM jiucool/java:server-jre-8u74

ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME

# see https://www.apache.org/dist/tomcat/tomcat-8/KEYS
RUN set -ex \
	&& for key in \
		05AB33110949707C93A279E3D3EFE6B686867BA6 \
		07E48665A34DCAFAE522E5E6266191C37C037D42 \
		47309207D818FFD8DCD3F83F1931D684307A10A5 \
		541FBE7D8F78B25E055DDEE13C370389288584E7 \
		61B832AC2F1C5A90F0F9B00A1C506407564C17A3 \
		79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED \
		9BA44C2621385CB966EBA586F72C284D731FABEE \
		A27677289986DB50844682F8ACB77FC2E86E29AC \
		A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 \
		DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 \
		F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE \
		F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23 \
	; do \
		gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
	done

ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.0.32
ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz

RUN set -x \
	&& curl -fSL "$TOMCAT_TGZ_URL" -o tomcat.tar.gz \
	&& curl -fSL "$TOMCAT_TGZ_URL.asc" -o tomcat.tar.gz.asc \
	&& 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 webapps/host-manager \
	&& rm -rf webapps/manager \
	&& rm -rf webapps/examples \
	&& rm -rf webapps/docs

EXPOSE 8080
CMD ["catalina.sh", "run"]

参考自官方文件:https://github.com/docker-library/tomcat/tree/ed98c30c1cd42c53831f64dffa78a0abf7db8e9a 同时也建议大家,特别是在初学时多参考一下官方相关文件的实现。

二、生成镜像文件

docker build -t jiucool/tomcat:8.0.32 .
推送到官方docker hub 中
docker push jiucool/tomcat:8.0.32
验证是否成功

[root@iZ25bgrg55xZ dockerconf]# docker search jiucool/tomcat
NAME             DESCRIPTION   STARS     OFFICIAL   AUTOMATED
jiucool/tomcat   tomcat基础镜像    0 

一切OK

三、部署我们的WEB应用

为了更方便,这里我们用自己的server.xml配置替换容器中默认配置,添加宿主主机应用目录映射至容器目录。

我们自定义配置文件地址为:/home/docker/dockerconf/server.xml,将WEB应用放到/home/docker/dockerconf/ROOT目录中,然后执行如下命令:
docker run -d --name testtomcat -p 8999:8080 -v /home/docker/dockerconf/server.xml:/usr/local/tomcat/conf/server.xml:ro -v /home/docker/dockerconf/ROOT:/usr/local/tomcat/webapps/ROOT:rw jiucool/tomcat:latest
查看容器运行情况

[root@iZ25bgrg55xZ dockerconf]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                    NAMES
3e73ae2c1d76        jiucool/tomcat:latest   "catalina.sh run"   6 seconds ago       Up 6 seconds        0.0.0.0:8999->8080/tcp   testtomcat

访问 http://ip:8999 查看,已经访问成功。

Leave a Reply


正在读取数据……