创建Dockerfile,构建jdk+tomcat环境
Dockerfile文件
[root@controller jdk]# cat Dockerfile
#pull base image
FROM ubuntu
MAINTAINER andy.liu "andyliu208@qq.com"
#update source
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
RUN apt-get update
#install curl
RUN apt-get install curl -y
#install JDK7
RUN cd /tmp && curl -L 'http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz' -H 'Cookie: oraclelicense=accept-securebackup-cookie; gpw_e24=Dockerfile' | tar -xz
RUN mkdir -p /usr/lib/jvm
RUN mv /tmp/jdk1.7.0_65/ /usr/lib/jvm/java-7-oracle/
#set oracle JDK 7 as default Java
RUN update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 300
RUN update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 300
ENV JAVA_HOME /usr/lib/jvm/java-7-oracle/
# Install tomcat7
RUN cd /tmp && curl -L 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz' | tar -xz
RUN mv /tmp/apache-tomcat-7.0.8/ /opt/tomcat7/
ENV CATALINA_HOME /opt/tomcat7
ENV PATH $PATH:$CATALINA_HOME/bin
ADD tomcat7.sh /etc/init.d/tomcat7
RUN chmod 755 /etc/init.d/tomcat7
# Expose ports.
EXPOSE 8080
# Define default command.
ENTRYPOINT service tomcat7 start && tail -f /opt/tomcat7/logs/catalina.out
tomcat7.sh
[root@controller jdk]# cat tomcat7.sh
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/
export TOMCAT_HOME=/opt/tomcat7
case $1 in
start)
sh $TOMCAT_HOME/bin/startup.sh
;;
stop)
sh $TOMCAT_HOME/bin/shutdown.sh
;;
restart)
sh $TOMCAT_HOME/bin/shutdown.sh
sh $TOMCAT_HOME/bin/startup.sh
;;
esac
exit 0
构建镜像
[root@controller jdk]# docker build -t candela/ubuntu/jdk-tomcat:V1 .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM ubuntu
---> 6a2f32de169d
Step 2 : MAINTAINER andy.liu "andyliu208@qq.com"
---> Using cache
---> 0f6041c706fd
Step 3 : RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
---> Using cache
---> d877b5463bf8
Step 4 : RUN apt-get update
---> Using cache
---> 8c0ed7a91df9
Step 5 : RUN apt-get install curl -y
---> Using cache
---> e071c2ba5a18
Step 6 : RUN cd /tmp && curl -L 'http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz' -H 'Cookie: oraclelicense=accept-securebackup-cookie; gpw_e24=Dockerfile' | tar -xz
---> Using cache
---> 1469c40ab52b
Step 7 : RUN mkdir -p /usr/lib/jvm
---> Using cache
---> c7233f76460c
Step 8 : RUN mv /tmp/jdk1.7.0_65/ /usr/lib/jvm/java-7-oracle/
---> Using cache
---> 919c171d76da
Step 9 : RUN update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 300
---> Using cache
---> 2d33e67e6aff
Step 10 : RUN update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 300
---> Using cache
---> 311bb36e1832
Step 11 : ENV JAVA_HOME /usr/lib/jvm/java-7-oracle/
---> Using cache
---> 3527d811ab1b
Step 12 : RUN cd /tmp && curl -L 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz' | tar -xz
---> Using cache
---> 0af25e62d20d
Step 13 : RUN mv /tmp/apache-tomcat-7.0.8/ /opt/tomcat7/
---> Using cache
---> c26c0e7b9b58
Step 14 : ENV CATALINA_HOME /opt/tomcat7
---> Using cache
---> c38b8c600225
Step 15 : ENV PATH $PATH:$CATALINA_HOME/bin
---> Using cache
---> 89fb15999e0a
Step 16 : ADD tomcat7.sh /etc/init.d/tomcat7
---> f52f362eac4b
Removing intermediate container 193e75c67b03
Step 17 : RUN chmod 755 /etc/init.d/tomcat7
---> Running in b36ea8a9680d
---> cbe2f0a115c1
Removing intermediate container b36ea8a9680d
Step 18 : EXPOSE 8080
---> Running in fe75b152d188
---> 48ed4c005dc0
Removing intermediate container fe75b152d188
Step 19 : ENTRYPOINT service tomcat7 start && tail -f /opt/tomcat7/logs/catalina.out
---> Running in be9b80dff87c
---> 74784c73edf0
Removing intermediate container be9b80dff87c
Successfully built 74784c73edf0
[root@controller jdk]# docker run -d -p 8090:8080 candela/
candela/dockerfile candela/dockerfile:v1 candela/ubuntu/jdk-tomcat candela/ubuntu/jdk-tomcat:V1
[root@controller jdk]# docker run -d --name web1 -h web1 -p 8090:8080 candela/ubuntu/jdk-tomcat:V1
5d6a8e2d0be53f78ca5ba2873ae3e9437c23d65715c649396093fd32e0aed17c
[root@controller jdk]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d6a8e2d0be5 candela/ubuntu/jdk-tomcat:V1 "/bin/sh -c 'service " 7 seconds ago Up 5 seconds 0.0.0.0:8090->8080/tcp web1
66f4b715f48d candela/dockerfile:v1 "/bin/bash" 47 minutes ago Up 47 minutes dockfile
159e17c5b266 postgres "docker-entrypoint.sh" About an hour ago Up About an hour 5432/tcp db1
默认情况下,tomcat会占用8080端口,刚才在启动container的时候,指定了 -p 8090:8080,映射到宿主机端口就是8090。
http://<host>:8090 host为主机IP
http://192.168.111.29:8090/
本文转自 OpenStack2015 博客,原文链接: http://blog.51cto.com/andyliu/1919282 如需转载请自行联系原作者