1、在宿主机创建文件夹tomcat7.0_jdk1.7,下载tomcat7.0和jdk放到此目录
core@coreos ~/tomcat7.0_jdk1.7 $ ls
Dockerfile apache-tomcat-7.0.68 jdk run.sh
2、编写Dockerfile
core@coreos ~/tomcat7.0_jdk1.7 $ cat Dockerfile
FROM sshd:centos6.5
MAINTAINER created from fhy_good365@163.com
ENV CATALINA_HOME /tomcat
ENV JAVA_HOME /jdk
ADD apache-tomcat-7.0.68 /tomcat
ADD jdk /jdk
ADD run.sh /run.sh
RUN chmod +x /*.sh
RUN chmod +x /tomcat/bin/*.sh
EXPOSE 8080
CMD ["/run.sh"]
3、编写启动脚本
core@coreos ~/tomcat7.0_jdk1.7 $ cat run.sh
#!/bin/bash
/usr/sbin/sshd -D &
exec ${CATALINA_HOME}/bin/catalina.sh run
4、构建镜像
core@coreos ~/tomcat7.0_jdk1.7 $ docker build -t tomcat7.0:jdk1.7 .
Sending build context to Docker daemon 268 MB
Step 0 : FROM sshd:centos6.5
---> 927c364b7d38
Step 1 : MAINTAINER created from fhy_good365@163.com
---> Running in dbe0cf04ccb4
---> b83808f1f0b4
Removing intermediate container dbe0cf04ccb4
Step 2 : ENV CATALINA_HOME /tomcat
---> Running in 70c091408d93
---> 6f263961315e
Removing intermediate container 70c091408d93
Step 3 : ENV JAVA_HOME /jdk
---> Running in 6df28d9568f1
---> d7eaf153a445
Removing intermediate container 6df28d9568f1
Step 4 : ADD apache-tomcat-7.0.68 /tomcat
---> 276ee172e278
Removing intermediate container 571356fcfc87
Step 5 : ADD jdk /jdk
---> 1e0cf5451c77
Removing intermediate container 55369bd5c76b
Step 6 : ADD run.sh /run.sh
---> 6fafe2f87f50
Removing intermediate container 4b837311c25b
Step 7 : RUN chmod +x /*.sh
---> Running in 045a3a3cef48
---> 58309d5d10cf
Removing intermediate container 045a3a3cef48
Step 8 : RUN chmod +x /tomcat/bin/*.sh
---> Running in 0a8c807609fc
---> 107a88bd0ec4
Removing intermediate container 0a8c807609fc
Step 9 : EXPOSE 8080
---> Running in 1b191fa71408
---> a3473288abb2
Removing intermediate container 1b191fa71408
Step 10 : CMD /run.sh
---> Running in bef4749ea518
---> 1e511ddc1f15
Removing intermediate container bef4749ea518
Successfully built 1e511ddc1f15
5、查看镜像
core@coreos ~/tomcat7.0_jdk1.7 $ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
tomcat7.0 jdk1.7 1e511ddc1f15 3 minutes ago 741.2 MB
6、启动容器
core@coreos ~/tomcat7.0_jdk1.7 $ docker run -d -P tomcat7.0:jdk1.7
7、查看容器
core@coreos ~/tomcat7.0_jdk1.7 $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b32f0cc6beb7 tomcat7.0:jdk1.7 "/run.sh" 5 minutes ago Up About a minute 0.0.0.0:32770->8080/tcp admiring_fermat
8、访问容器应用,注意:访问宿主机的32770端口
本文出自 “hanyun.fang” 博客,请务必保留此出处http://hanyun.blog.51cto.com/1060170/1752662
原文地址:http://hanyun.blog.51cto.com/1060170/1752662