其他参考:
CentOS Docker 安装
Docker 部署 Tomcat


本文环境为CentOS,在制作镜像之前,请先下载好Linux版的JDK和Tomcat,我所演示的版本为:jdk-8u162-linux-x64.tar.gzapache-tomcat-8.0.47.tar.gz

  • 保证JDK、Tomcat、Dockerfile 处于同一目录下(本次演示目录为:/home)
[root@iZbp1b5k5bjps0dw8owk7tZ home]# ls
apache-tomcat-8.0.47.tar.gz  Dockerfile  jdk-8u162-linux-x64.tar.gz
[root@iZbp1b5k5bjps0dw8owk7tZ home]#

编写docker构建文件(Dockerfile)

# 编辑Dockerfile
vi Dockerfile

不知道什么是Dockerfile? 也不会写?,OK,Ctrl+C 就好了,不过请将jdk和tomcat的版本和版本后面对应的路径改为自己指定的版本和路径

# 基础镜像
FROM centos
# 作者
MAINTAINER  yezi
# 把jdk与tomcat添加到容器中
ADD jdk-8u162-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.0.47.tar.gz /usr/local/
# 配置环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_162
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.47
ENV CATALINA_BASE /usr/local/apache-tomcat-8.0.47
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 容器运行时监听的端口
EXPOSE 8080
#配置容器启动后执行的命令
ENTRYPOINT /usr/local/apache-tomcat-8.0.47/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.0.47/logs/catalina.out
#启动时运行tomcat
CMD ["/usr/local/apache-tomcat-8.0.47/bin/catalina.sh","run"]
  • ENTRYPOINT 这个命令主要是为了解决 tomcat 容器不打印 catalina.out 日志,可以省去

构建镜像

# 以当前目录下(.)的 Dockerfile 文件 构建镜像名为 yezi/tomcat 的镜像
docker build -t yezi/tomcat .
[root@iZbp1b5k5bjps0dw8owk7tZ test]# docker build -t yezi/tomcat .
Sending build context to Docker daemon  199.2MB
Step 1/11 : FROM centos
 ---> 49f7960eb7e4
Step 2/11 : MAINTAINER  yezi
 ---> Running in 23ab370a223c
Removing intermediate container 23ab370a223c
 ---> eb683d133105
Step 3/11 : ADD jdk-8u162-linux-x64.tar.gz /usr/local/
 ---> cf74a462ef4a
Step 4/11 : ADD apache-tomcat-8.0.47.tar.gz /usr/local/
 ---> 50e1b915e404
Step 5/11 : ENV JAVA_HOME /usr/local/jdk1.8.0_162
 ---> Running in 62651c01180d
Removing intermediate container 62651c01180d
 ---> 0f440b8fb05c
Step 6/11 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in e7b22ff3daae
Removing intermediate container e7b22ff3daae
 ---> 1d7561a73619
Step 7/11 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.47
 ---> Running in 10d6926828af
Removing intermediate container 10d6926828af
 ---> 7ee45ddd1cac
Step 8/11 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.0.47
 ---> Running in 6dc85ca63503
Removing intermediate container 6dc85ca63503
 ---> 9ccb74f42ac8
Step 9/11 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in 95f3447c1345
Removing intermediate container 95f3447c1345
 ---> afa972c0579f
Step 10/11 : EXPOSE 8080
 ---> Running in 8ec9bc003730
Removing intermediate container 8ec9bc003730
 ---> 0d64ccb2b12e
Step 11/11 : CMD ["/usr/local/apache-tomcat-8.0.47/bin/catalina.sh","run"]
 ---> Running in 60877645aadd
Removing intermediate container 60877645aadd
 ---> ed785fcf2d5f
Successfully built ed785fcf2d5f
Successfully tagged yezi/tomcat:latest
[root@iZbp1b5k5bjps0dw8owk7tZ test]#

查看构建的镜像

[root@iZbp1b5k5bjps0dw8owk7tZ home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
yezi/tomcat         latest              ed785fcf2d5f        About an hour ago   598MB
tomcat              latest              2d084b11164d        31 hours ago        463MB
centos              latest              49f7960eb7e4        4 weeks ago         200MB
[root@iZbp1b5k5bjps0dw8owk7tZ home]#

启动镜像

docker run -d -p 8080:8080 yezi/tomcat

results matching ""

    No results matching ""