本文直接讲解如何在Docker容器中实战部署一个Web应用程序,关于Docker相关的概念和如何安装Docker请参考相关资料完成。
第一步:工具准备演示如何在Docker容器中部署一个Java Web应用程序,需要准备的软件工具包括:jre,tomcat和webapp应用。另外,为了实现在容器启动时自动启动webapp,需要编写一个脚本工具完成该工作。
安装jre,请参考:
Ubuntu 12.04下安装JDK1.7
Ubuntu 14.04 LTS安装Oracle JDK 1.8
CentOS6.3安装JDK和环境配置
安装Tomcat,请参考:
CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤
Linux(CentOS)下安装 JDK与Tomcat
如何在Tomcat中部署webapp,请参考:
jre1.8.0_31.tar.gz // 可以选择其他版本
apache-tomcat-6.0.35.tar.gz // 可以选择其他版本
MyWeb.war // 自己写一个web应用即可,才发现csdn不允许上传附件
start_tomcat.sh
第二步:制作镜像通过编写Dockerfile的方式制作镜像。
需要在Dockerfile中完成如下几项工作:
(1)安装jre
(2)安装tomcat,并完成在tomcat中部署web应用的基本配置(为实现此功能:在制作镜像之前直接先完成tomcat的基础配置,然后直接拷贝到镜像中即可)。
(3)对外开发8080端口(具体的端口值可以根据实际Tomcat配置参数为准)。
$mkdir docker
$cd docker
$mkdir webapps
$cp jre1.8.0_31.tar.gz .
$tar xzvf jre1.8.0_31.tar.gz
$cp apache-tomcat-6.0.35.tar.gz
$tar xzvf apache-tomcat-6.0.35.tar.gz
$cp MyWeb.war ./webapps/
$vim apache-tomcat-6.0.35/conf/server.xml
编辑tomcat配置文件:server.xml,在<Host>节点中添加如下配置:<Context path="myweb"docBase="/webapps/MyWeb.war" reloadable="false"workDir="/tomcat_work"/>,配置片段如下所示:
<Host appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, shareauthentication between web applications
Documentation at:/docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn"/>
-->
<!-- Access log processes all example.
Documentation at:/docs/config/valve.html -->
<!--
<ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"
prefix="localhost_access_log." suffix=".txt"pattern="common" resolveHosts="false"/>
-->
<Contextpath="/myweb"docBase="/webapps/MyWeb.war"reloadable="false"workDir="/tomcat_work"/>
</Host>
$vim start_tomcat.sh
#!/bin/bash
#Date: 2015/02/02
#Desc:
# Start tomcat with docker containerstart.
echo "Start Tomcat ..."
# Export java path
export PATH=$PATH:/usr/local/java/bin
# Display container ipaddress
ifconfig
# Start tomcat
bash /usr/local/tomcat/bin/catalina.sh run
注意:启动tomcat时必须通过$TOMCAT_HOME/bin/catalina.sh实现,不能使用$TOMCAT_HOME/bin/startup.sh启动,否则脚本执行后容器立刻就退出了。
编写Dockerfile#Build java web app container image
FROM docker.cn/docker/ubuntu:14.04
MAINTAINER chenchanghui<nuccch2010.163.com>
#Make java and tomcat install directory
RUN mkdir /usr/local/java
RUN mkdir /usr/local/tomcat
#Copy jre and tomcat into image
ADD jre1.8.0_31 /usr/local/java/
ADD apache-tomcat-6.0.35 /usr/local/tomcat/
ADD start_tomcat.sh start_tomcat.sh
#Expose http port
EXPOSE 8080
创建镜像$sudo docker build -t=”ubuntu/myweb:tomcat”.
Sendingbuild context to Docker daemon 270.6 MB
Sendingbuild context to Docker daemon
Step 0 :FROM docker.cn/docker/ubuntu:14.04
---> b39b81afc8ca
Step 1 :MAINTAINER chenchanghui <nuccch2010.163.com>
---> Running in cd9ba3324dae
---> ab45c422bdf5
Removingintermediate container cd9ba3324dae
Step 2 :RUN mkdir /usr/local/java
---> Running in f640de521691
---> bd94048cb633
Removingintermediate container f640de521691
Step 3 :RUN mkdir /usr/local/tomcat
---> Running in de4a392ec89d
---> 956ac99b8bec
Removingintermediate container de4a392ec89d
Step 4 :ADD jre1.8.0_31 /usr/local/java/
---> e3181a61f635
Removingintermediate container b69c147f28fe
Step 5 :ADD apache-tomcat-6.0.35 /usr/local/tomcat/
---> 9169a4ab9a80
Removingintermediate container c190162d7a5c
Step 6 :ADD start_tomcat.sh start_tomcat.sh
---> cf61f83dc0b0
Removingintermediate container 3f10c2a9e374
Step 7 :EXPOSE 8080
--->abea02c999a2
Removingintermediate container a3841acba123
Successfullybuilt abea02c999a2
第三部:启动容器webapp通过数据卷挂在到容器中进行部署,不需要拷贝到镜像中。
$sudo docker run -t -i --name tomcat –v /home/$username/docker/webapps:/webapps/
ubuntu/myweb:tomcat /bin/bash/start_tomcat.sh
Start Tomcat ...
eth0 Link encap:Ethernet HWaddr02:42:ac:11:00:0c
inet addr:172.17.0.12 Bcast:0.0.0.0 Mask:255.255.0.0
……..
Feb 02, 2015 1:38:42 PMorg.apache.catalina.core.AprLifecycleListener init