该实例将继承基础centos镜像,然后将我们自己的 jdk 和 tomcat 安装包拷贝且解压到 centos 镜像的 /usr/local 目录。然后配置 JAVA_HOME、CLASSPATH、PATH、CATALINA_HOME 和 CATALINA_BASE 环境变量。
Dockerfile文件内容如下:
FROM centos MAINTAINER hxin<hxinsnow@qq.com> # 把宿主主机当前上下文的 c.txt 拷贝到容器 /usr/local 路径下 COPY c.txt /usr/local/container.txt # 把 java 与 tomcat 添加到容器中 ADD jdk-8u171-linux-i586.tar.gz /usr/local ADD apache-tomcat-9.0.22.tar.gz /usr/local # 安装 vim 编辑器 RUN yum -y install vim # 解决 -bash: /usr/local/jdk1.8/jdk1.8.0_181/bin/java: # /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 问题 RUN yum -y install glibc.i686 # 设置工作访问时候的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.22 ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.22 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin # 容器运行时监听的端口 EXPOSE 8080 # 启动时运行tomcat # ENTRYPOINT ["/usr/local/apache-tomcat-9.0.8/bin/startup.bat"] # CMD ["/usr/local/apache-tomcat-9.0.8/bin/catalina.sh", "run"] CMD $CATALINA_HOME/bin/startup.sh && tail -f $CATALINA_HOME/logs/catalina.out
使用 docker build -f Dockerfile -t tomcat9 . 命令构建镜像,构建过程如下:
$ docker build -f Dockerfile -t tomcat9 . Sending build context to Docker daemon 204.8MB Step 1/16 : FROM centos ---> 9f38484d220f Step 2/16 : MAINTAINER hxin<hxinsnow@qq.com> ---> Running in 59f9540e3ff0 Removing intermediate container 59f9540e3ff0 ---> 8cd1a6e0430e Step 3/16 : COPY c.txt /usr/local/container.txt ---> 3afbe94bccd2 Step 4/16 : ADD jdk-8u171-linux-i586.tar.gz /usr/local ---> 0eea6afa060b Step 5/16 : ADD apache-tomcat-9.0.22.tar.gz /usr/local ---> d9079b92bfab Step 6/16 : RUN yum -y install vim ---> Running in 838bb06d6ebd Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package vim-enhanced.x86_64 2:7.4.160-6.el7_6 will be installed --> Processing Dependency: vim-common = 2:7.4.160-6.el7_6 for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64 ...more... perl-threads-shared.x86_64 0:1.43-6.el7 vim-common.x86_64 2:7.4.160-6.el7_6 vim-filesystem.x86_64 2:7.4.160-6.el7_6 which.x86_64 0:2.20-7.el7 Complete! Removing intermediate container 838bb06d6ebd ---> 215720ff5c7a Step 7/16 : RUN yum -y install glibc.i686 ---> Running in 64609a51c9e5 Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package glibc.i686 0:2.17-260.el7_6.6 will be installed ...more... Complete! Removing intermediate container 64609a51c9e5 ---> e88d9fb84bcd Step 8/16 : ENV MYPATH /usr/local ---> Running in 3e518019d13f Removing intermediate container 3e518019d13f ---> a8fc032fe78b Step 9/16 : WORKDIR $MYPATH ---> Running in 1d3b4f8722be Removing intermediate container 1d3b4f8722be ---> 0668f8a9f001 Step 10/16 : ENV JAVA_HOME /usr/local/jdk1.8.0_171 ---> Running in da0566d8dcc9 Removing intermediate container da0566d8dcc9 ---> 497cbba75487 Step 11/16 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ---> Running in a8fc3364b46a Removing intermediate container a8fc3364b46a ---> 4c44e8c93b97 Step 12/16 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22 ---> Running in 9d2eaf2fc451 Removing intermediate container 9d2eaf2fc451 ---> ef484938826f Step 13/16 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.22 ---> Running in abfee0ec7e1d Removing intermediate container abfee0ec7e1d ---> dedd306660ce Step 14/16 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Running in 169ee2c5eef1 Removing intermediate container 169ee2c5eef1 ---> b1809ccfbeda Step 15/16 : EXPOSE 8080 ---> Running in b597f8a70106 Removing intermediate container b597f8a70106 ---> 5740e6bcb807 Step 16/16 : CMD $CATALINA_HOME/bin/startup.sh && tail -f $CATALINA_HOME/logs/catalina.out ---> Running in cc8ff26f52fd Removing intermediate container cc8ff26f52fd ---> 2619ee45e83f Successfully built 2619ee45e83f Successfully tagged tomcat9:latest
构建成功后,我们使用 docker images 命令查看镜像列表,如下:
$ docker images tomcat9 REPOSITORY TAG IMAGE ID CREATED SIZE tomcat9 latest 2619ee45e83f 8 minutes ago 916MB
最后使用 docker run -it -p 8080:8080 tomcat9 命令运行容器:
$ docker run -it -p 8080:8080 tomcat9 Using CATALINA_BASE: /usr/local/apache-tomcat-9.0.22 Using CATALINA_HOME: /usr/local/apache-tomcat-9.0.22 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.22/temp Using JRE_HOME: /usr/local/jdk1.8.0_171 Using CLASSPATH: /usr/local/apache-tomcat-9.0.22/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.22/bin/tomcat-juli.jar Tomcat started. ...more... org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.22/webapps/manager] has finished in [45] ms 10-Jul-2019 14:12:12.706 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 10-Jul-2019 14:12:12.721 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] 10-Jul-2019 14:12:12.726 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1,231] milliseconds
此时,我们就可以在浏览器访问tomcat了。
注意:如果你的宿主主机存在防火墙,你需要将 8080 端口加入到防火墙的白名单。