准备apache包和jdk并解压
[root@localhost tomcat]# ll
总用量 196728
-rw-r--r--. 1 root root 9690027 7月 17 2020 apache-tomcat-8.5.40.tar.gz
-rw-r--r--. 1 root root 674 8月 2 20:19 Dockerfile
-rw-r--r--. 1 root root 191753373 7月 17 2020 jdk-8u191-linux-x64.tar.gz
[root@localhost tomcat]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost tomcat]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost tomcat]# ll
总用量 196728
drwxr-xr-x. 9 root root 220 8月 3 13:58 apache-tomcat-8.5.40
-rw-r--r--. 1 root root 9690027 7月 17 2020 apache-tomcat-8.5.40.tar.gz
-rw-r--r--. 1 root root 552 8月 3 13:55 Dockerfile
drwxr-xr-x. 7 10 143 245 10月 6 2018 jdk1.8.0_191
-rw-r--r--. 1 root root 191753373 7月 17 2020 jdk-8u191-linux-x64.tar.gz
创建工作目录
[root@localhost ]# mkdir tomcat
编写dockerfile文件
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux <crushlinux@163.com
ADD jdk1.8.0_191 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /use/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.40 /usr/local/tomcat
RUN chmod 755 /usr/local/tomcat/bin/startup.sh
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
~
具体来说,这个Dockerfile文件的内容如下:
FROM centos:7
:表示基础镜像是CentOS 7。MAINTAINER Crushlinux <crushlinux@163.com>
:表示维护者信息,即该Docker镜像的作者和联系方式。ADD jdk1.8.0_191 /usr/local/java
:将JDK压缩包解压到/usr/local/java
目录下。ENV JAVA_HOME /usr/local/java
:设置JAVA_HOME
环境变量为/usr/local/java
。ENV JAVA_BIN /usr/local/java/bin
:设置JAVA_BIN
环境变量为/usr/local/java/bin
。ENV JRE_HOME /usr/local/java/jre
:设置JRE_HOME
环境变量为/usr/local/java/jre
。ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
:将/usr/local/java/bin
和/usr/local/java/jre/bin
添加到PATH
环境变量中。ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
:设置CLASSPATH
环境变量为/usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
。ADD apache-tomcat-8.5.40 /usr/local/tomcat
:将Tomcat压缩包解压到/usr/local/tomcat
目录下。RUN chmod 755 /usr/local/tomcat/bin/startup.sh
:设置/usr/local/tomcat/bin/startup.sh
文件的权限为755。RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
:创建一个软链接将系统时区设置为上海。EXPOSE 8080
:将容器的8080端口暴露出来
构建镜像
[root@localhost tomcat]# docker build -t tomcat:new .
Sending build context to Docker daemon 614 MB
Step 1/13 : FROM centos:7
---> 74d9a3a7d55d
Step 2/13 : MAINTAINER Crushlinux <crushlinux@163.com
---> Using cache
---> efa399b03844
Step 3/13 : ADD jdk1.8.0_191 /usr/local/java
---> 6c3bcd8412e8
Removing intermediate container 2ee47f838026
Step 4/13 : ENV JAVA_HOME /usr/local/java
---> Running in e9f1e8c91c15
---> a2bfe81ba7f5
Removing intermediate container e9f1e8c91c15
Step 5/13 : ENV JAVA_BIN /use/local/java/bin
---> Running in abbe840f87de
---> 8a62172d033a
Removing intermediate container abbe840f87de
Step 6/13 : ENV JRE_HOME /usr/local/java/jre
---> Running in 4209c7fe0c4a
---> 9936e3b14561
Removing intermediate container 4209c7fe0c4a
Step 7/13 : ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
---> Running in 3f266fb8c13e
---> 11bfac038ca4
Removing intermediate container 3f266fb8c13e
Step 8/13 : ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
---> Running in 681f67cde11e
---> 62e50122b88a
Removing intermediate container 681f67cde11e
Step 9/13 : ADD apache-tomcat-8.5.40 /usr/local/tomcat
---> abfee70cf80b
Removing intermediate container 456e93af2eb9
Step 10/13 : RUN chmod 755 /usr/local/tomcat/bin/startup.sh
---> Running in 886999728ff0
---> bcdc9c2a7630
Removing intermediate container 886999728ff0
Step 11/13 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
---> Running in b513e27c3035
---> eafbe22ec214
Removing intermediate container b513e27c3035
Step 12/13 : EXPOSE 8080
---> Running in 9d3d4047a2e3
---> 4149863793a1
Removing intermediate container 9d3d4047a2e3
Step 13/13 : CMD /usr/local/tomcat/bin/catalina.sh run
---> Running in 97d46c7d6b1b
---> ccdba3e6ed6a
Removing intermediate container 97d46c7d6b1b
Successfully built ccdba3e6ed6a
查看
[root@localhost tomcat]# docker images tomcat:new
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new ccdba3e6ed6a 48 seconds ago 1e+03 MB
测试容器
[root@localhost tomcat]# docker run -d -p 8080:8080 --name=tomcat tomcat:new
19e5c9217b9ab431c75a7c07f1e8fd7571b67f9744ee060271163300a5dba64c
浏览器访问测试