Docker-Tomcat构建镜像
1、构建tomcat基础镜像
1.1、需求
基于java环境镜像定制tomcat镜像 JDK可以下载官方,也可以自定义,参考文章:https://www.cnblogs.com/ygbh/p/17413896.html#_label1
1.2、创建Dockerfile专用目录并且下载tomcat
mkdir /docker/images/tomcat -p && cd /docker/images/tomcat
curl -o apache-tomcat-9.0.75.tar.gz https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.75/bin/apache-tomcat-9.0.75.tar.gz
1.3、定制Dockerfile
cat << 'CAT_END' > Dockerfile FROM ubuntu-jdk:8u121 MAINTAINER cyc author ENV TZ "Asia/Shanghai" ENV LANG en_US.UTF-8 ENV TERM xterm ENV TOMCAT_MAJOR_VERSION 9 ENV TOMCAT_MINOR_VERSION 9.0.75 ENV CATALINA_HOME /usr/local/tomcat ENV APP_DIR ${CATALINA_HOME}/webapps ADD apache-tomcat-9.0.75.tar.gz /usr/local/ RUN ln -s /usr/local/apache-tomcat-9.0.75 /usr/local/tomcat CMD ["/bin/bash"] CAT_END
1.4、构建tomcat镜像
docker build -t ubuntu-tomcat:v9.0.75 /docker/images/tomcat/
1.5、使用新镜像启动一个容器,查看效果
]# docker run -it --rm -p 8080:8080 ubuntu-tomcat:v9.0.75 /bin/bash root@5ebc849ec4b1:/# ll /usr/local/tomcat/ total 128 drwxr-xr-x. 9 root root 220 May 19 04:30 ./ drwxr-xr-x. 1 root root 20 May 19 04:30 ../ -rw-r-----. 1 root root 20123 Apr 14 19:40 BUILDING.txt -rw-r-----. 1 root root 6210 Apr 14 19:40 CONTRIBUTING.md -rw-r-----. 1 root root 60393 Apr 14 19:40 LICENSE -rw-r-----. 1 root root 2333 Apr 14 19:40 NOTICE -rw-r-----. 1 root root 3398 Apr 14 19:40 README.md -rw-r-----. 1 root root 6775 Apr 14 19:40 RELEASE-NOTES -rw-r-----. 1 root root 16076 Apr 14 19:40 RUNNING.txt drwxr-x---. 2 root root 4096 Apr 14 19:40 bin/ drwx------. 2 root root 238 Apr 14 19:40 conf/ drwxr-x---. 2 root root 4096 Apr 14 19:40 lib/ drwxr-x---. 2 root root 6 Apr 14 19:40 logs/ drwxr-x---. 2 root root 30 Apr 14 19:40 temp/ drwxr-x---. 7 root root 81 Apr 14 19:40 webapps/ drwxr-x---. 2 root root 6 Apr 14 19:40 work/ 注意: 因为在构建镜像的时候,已经启动了命令bash,所以可以直接进入进去
1.6、容器手动开启tomcat服务
]# docker run -it --rm -p 8080:8080 ubuntu-tomcat:v9.0.75 /bin/bash root@4ba25e49df87:/# /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/java/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started.
1.7、访问测试
2、构建tomcat项目镜像
2.1、需求
基于基础的 tomcat镜像,运行两个专用的tomcat项目
2.2、创建项目目录和下载tomcat安装解压
mkdir -p /docker/tomcat/webapp{1,2} -p cd /docker/tomcat && curl -o apache-tomcat-9.0.75.tar.gz https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.75/bin/apache-tomcat-9.0.75.tar.gz && tar xf apache-tomcat-9.0.75.tar.gz
# tomcat版本记得一致,主要用于复制配置文件,修改配置之用
2.3、定制tomcat-webapp1
2.3.1、修改tomcat配置server.xml
cd /docker/tomcat/webapp1/ cp ../apache-tomcat-9.0.75/conf/server.xml . ]# vi server.xml # 主让tomcat加载项目的位置 <Host name="localhost" appBase="/usr/local/tomcat/webapps" unpackWARs="true" autoDeploy="false">
2.3.2、定制 tomcat-app1 的应用首页
mkdir ROOT echo "Welcome to Tomcat app1"> ROOT/index.jsp tar zcf ROOT.tar.gz ROOT
2.3.3、定制tomcat服务启动脚本
cat << 'CAT_END' > entrypoint.sh #!/bin/bash ${CATALINA_HOME}/bin/catalina.sh start tail -f ${CATALINA_HOME}/logs/catalina.out CAT_END
2.3.4、定制Dockerfile
cat << 'CAT_END' > Dockerfile FROM ubuntu-tomcat:v9.0.75 MAINTAINER cyc author WORKDIR /usr/local/tomcat COPY server.xml ./conf/server.xml COPY entrypoint.sh /root/ ADD ROOT.tar.gz ./webapps/ EXPOSE 8080 CMD ["bash","/root/entrypoint.sh"] CAT_END
2.3.5、构建镜像
docker build -t tomcat-webapp1:v0.1 .
2.3.6、运行测试
docker run -d -p 8080:8080 tomcat-webapp1:v0.1 ]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES be6fb6f5c9fe tomcat-webapp1:v0.1 "bash /root/entrypoi…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp pensive_rhodes ]# curl 192.168.10.19:8080 Welcome to Tomcat app1
2.4、定制tomcat-webapp2
2.4.1、说明
同样的方式定制webapp2镜像
# 唯一的区别就是主页不一样
2.4.2、访问效果