制作自己的Tomcat镜像
- 准备镜像文件:tomcat、jdk的压缩包
tomcat 和 jdk 程序包下载地址
https://downloads.apache.org/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
https://download.oracle.com/otn/java/jdk/8u202-b08/1961070e4c9b4e26a04e7f5a083f551e/jdk-8u202-linux-x64.tar.gz
- 编写Dockerfile文件,名字 固定命名为 Dockerfile ,这样在构建镜像的时候就不用去人为的指定使用哪个dockerfile来构建镜像了。
# vim Dockerfile
FROM centos
MAINTAINER wanglei<wanglei.work@qq.com>
COPY readme.txt /usr/local/readme.txt
# 会自动解压到指定目录,不需要人为去解压
ADD jdk-8u202-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.54.tar.gz /usr/local/
RUN yum install -y vim
ENV MYPATH /usr/local/
WORKDIR $MYPATH
ENV JAVA_HOME $MYPATH/jdk1.8.0_202
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME $MYPATH/apache-tomcat-9.0.54
ENV CATALINA_BASH $MYPATH/apache-tomcat-9.0.54
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
VOLUME ["$CATALINA_HOME/logs/","$CATALINA_HOME/webapps"]
#VOLUME ["/usr/local/apache-tomcat-9.0.54/logs/","/usr/local/apache-tomcat-9.0.54/webapps"]
# 注意:tail -F ,这里如果用小写的f,启动容器的时候打印完日志后会退出,用大写-F容器才会停留
CMD $CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out
- 构建镜像
docker build -t mytomcat .
[root@localhost mytomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 6313f84af805 Less than a second ago 680MB
mytomcat latest 24162998b8a5 2 minutes ago 722MB
[root@localhost mytomcat]# docker run -it -P mytomcat
29-Sep-2021 05:52:57.375 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.54/webapps/manager] has finished in [33] ms
29-Sep-2021 05:52:57.381 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [“http-nio-8080”]
29-Sep-2021 05:52:57.424 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1085] milliseconds
# Ctrl + p + q 退出容器(容器并不会停止)
[root@localhost mytomcat]# netstat -lntp # 用下面的 docker port 命令查看容器暴露的端口更好
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:49155 0.0.0.0:* LISTEN 94690/docker-proxy
tcp6 0 0 :::49155 ::😗 LISTEN 94694/docker-proxy
[root@localhost mytomcat]# docker port infallible_lamport # docker port 命令查看容器暴露的端口
8080/tcp -> 0.0.0.0:49160
8080/tcp -> :::49160
反向获取镜像的Dockerfile
docker history --format {{.CreatedBy}} --no-trunc=true mytomcat |sed "s/\/bin\/sh\ -c\ \#(nop)\ //g"|sed "s/\/bin\/sh\ -c/RUN/g" | tac
# 建议使用下面这个,命令的可读性更好
docker history --format {{.CreatedBy}} --no-trunc=true mytomcat |sed "s@/bin/sh -c #(nop) @@g"|sed "s@/bin/sh -c@RUN@g" | tac
ADD file:805cb5e15fb6e0bb0326ca33fd2942e068863ce2a8491bb71522c652f31fb466 in /
LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20210915
CMD ["/bin/bash"]
MAINTAINER wanglei<wanglei.work@qq.com>
COPY file:cc617d883c53bd08fa2bcaaed13d5b24aaf7aec1f9216c5ab74bfe1342d4bae9 in /usr/local/readme.txt
ADD file:65d2a21c343d4f40c5b76862829bee1b24ef58f2bd077129ce6e61765786a638 in /usr/local/
ADD file:6612fb6514125866337a8c36e7c026f68d6c970007aed0a977b31a00e48c6944 in /usr/local/
RUN yum install -y vim
ENV MYPATH=/usr/local/
WORKDIR /usr/local/
ENV JAVA_HOME=/usr/local//jdk1.8.0_202
ENV CLASSPATH=/usr/local//jdk1.8.0_202/lib/dt.jar:/usr/local//jdk1.8.0_202/lib/tools.jar
ENV CATALINA_HOME=/usr/local//apache-tomcat-9.0.54
ENV CATALINA_BASH=/usr/local//apache-tomcat-9.0.54
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local//jdk1.8.0_202/bin:/usr/local//apache-tomcat-9.0.54/lib:/usr/local//apache-tomcat-9.0.54/bin
EXPOSE 8080
CMD ["/bin/sh" “-c” “$CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out”]