1.Dockerfile脚本
需要下载两个包
apache-tomcat-9.0.8.tar.gz
jdk-8u171-linux-x64.tar.gz
FROM centos
MAINTAINER zzyy<zzyybs@126.com>
#把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下
COPY c.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中
ADD apache-tomcat-9.0.41.tar.gz /usr/local/
ADD jdk-8u281-linux-x64.tar.gz /usr/local/
#安装vim编辑器
RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.41
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.41
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.41/bin/startup.sh"]
#CMD["/usr/local/apache-tomcat-9.0.41/bin/catalina.sh" ,"run"]
CMD /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.41/bin/logs/catalina.out
2.创建镜像
root@test mydocker]# docker build -f /mydocker/tomcatfile -t tomcat2 .
Sending build context to Docker daemon 155.2MB
Step 1/15 : FROM centos
---> 300e315adb2f
Step 2/15 : MAINTAINER zzyy<zzyybs@126.com>
---> Using cache
---> e1b1d7522039
Step 3/15 : COPY c.txt /usr/local/cincontainer.txt
---> Using cache
---> 7059c680c735
Step 4/15 : ADD apache-tomcat-9.0.41.tar.gz /usr/local/
---> Using cache
---> b236dc34677b
Step 5/15 : ADD jdk-8u281-linux-x64.tar.gz /usr/local/
---> Using cache
---> 6edeaf32e67c
Step 6/15 : RUN yum -y install vim
---> Using cache
---> f098797c0ca1
Step 7/15 : ENV MYPATH /usr/local
---> Using cache
---> adf68f1a8b4e
Step 8/15 : WORKDIR $MYPATH
---> Using cache
---> f5a51782665b
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_171
---> Using cache
---> 67e366964602
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Using cache
---> 8ad4eabe3b0c
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.41
---> Using cache
---> 8e0b7f86c73d
Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.41
---> Using cache
---> fd62c1c27136
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Using cache
---> 736cbb5b652f
Step 14/15 : EXPOSE 8080
---> Using cache
---> 6f46b5bea76a
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.41/bin/logs/catalina.out
---> Running in b035d3acf741
Removing intermediate container b035d3acf741
---> b18d544aa0b6
Successfully built b18d544aa0b6
Successfully tagged tomcat2:latest
3. 运行镜像并绑定容器卷
[root@test mydocker]# docker run -d -p 9080:8080 --name mytomcat2 -v /root/mydockerfile/tomcat9/test:/usr/local/apache-tomcat-9.0.41/webapps/test -v /root/mydockerfile/tomcat9/tomcat9logs/:/usr/local/apache-tomcat-9.0.41/logs --privileged=true tomcat2