首页 > 代码库 > 在Docker容器中部署Web应用
在Docker容器中部署Web应用
本文直接讲解如何在Docker容器中实战部署一个Web应用程序,关于Docker相关的概念和如何安装Docker请参考相关资料完成。
第一步:工具准备
演示如何在Docker容器中部署一个Java Web应用程序,需要准备的软件工具包括:jre,tomcat和webapp应用。另外,为了实现在容器启动时自动启动webapp,需要编写一个脚本工具完成该工作。
安装jre,请参考:http://www.wikihow.com/Install-Java-on-Linux
安装tomcat,请参考:http://blog.csdn.net/williamcs/article/details/8579957
如何在tomcat中部署webapp,请参考:
http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html
http://www.cnblogs.com/zengsong-restService/archive/2013/01/04/2844568.html
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 name="localhost" 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
INFO: The APR based Apache Tomcat Nativelibrary which allows optimal performance in production environments was notfound on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Feb 02, 2015 1:38:42 PMorg.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 onhttp-8080
Feb 02, 2015 1:38:42 PMorg.apache.catalina.startup.Catalina load
INFO: Initialization processed in 788 ms
Feb 02, 2015 1:38:42 PMorg.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Feb 02, 2015 1:38:42 PM org.apache.catalina.core.StandardEnginestart
INFO: Starting Servlet Engine: ApacheTomcat/6.0.35
Feb 02, 2015 1:38:42 PMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directorymanager
Feb 02, 2015 1:38:42 PM org.apache.catalina.startup.HostConfigdeployDirectory
INFO: Deploying web application directorydocs
Feb 02, 2015 1:38:42 PMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directoryROOT
Feb 02, 2015 1:38:42 PM org.apache.catalina.startup.HostConfigdeployDirectory
INFO: Deploying web application directoryexamples
Feb 02, 2015 1:38:43 PMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directoryhost-manager
Feb 02, 2015 1:38:43 PM org.apache.coyote.http11.Http11Protocolstart
INFO: Starting Coyote HTTP/1.1 on http-8080
Feb 02, 2015 1:38:43 PMorg.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Feb 02, 2015 1:38:43 PMorg.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/27 config=null
Feb 02, 2015 1:38:43 PMorg.apache.catalina.startup.Catalina start
INFO: Server startup in 842 ms
如日志所示,Docker容器已经启动,并且其中安装的tomcat已经成功启动。
访问:http://172.17.0.12:8080/myweb/,Everything is ok!
小技巧:启动容器时带参数-t -i和不带参数的区别:带参数-t -i时可以通过Ctrl+C停止容器运行,不带参数-t -i启动时,停止容器只能通过命令:$sudo docker stop $containerid实现。
在Docker容器中部署Web应用