本文直接講解如何在Docker容器中實(shí)戰(zhàn)部署一個(gè)Web應(yīng)用程序,,關(guān)于Docker相關(guān)的概念和如何安裝Docker請(qǐng)參考相關(guān)資料完成,。 第一步:工具準(zhǔn)備 演示如何在Docker容器中部署一個(gè)Java Web應(yīng)用程序,需要準(zhǔn)備的軟件工具包括:jre,,tomcat和webapp應(yīng)用,。另外,為了實(shí)現(xiàn)在容器啟動(dòng)時(shí)自動(dòng)啟動(dòng)webapp,需要編寫一個(gè)腳本工具完成該工作,。 安裝jre,,請(qǐng)參考:http://www./Install-Java-on-Linux 安裝tomcat,請(qǐng)參考:http://blog.csdn.net/williamcs/article/details/8579957 如何在tomcat中部署webapp,,請(qǐng)參考: http://tomcat./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 // 自己寫一個(gè)web應(yīng)用即可,,才發(fā)現(xiàn)csdn不允許上傳附件 start_tomcat.sh
第二步:制作鏡像 通過編寫Dockerfile的方式制作鏡像。 需要在Dockerfile中完成如下幾項(xiàng)工作: (1)安裝jre (2)安裝tomcat,,并完成在tomcat中部署web應(yīng)用的基本配置(為實(shí)現(xiàn)此功能:在制作鏡像之前直接先完成tomcat的基礎(chǔ)配置,,然后直接拷貝到鏡像中即可)。 (3)對(duì)外開發(fā)8080端口(具體的端口值可以根據(jù)實(shí)際Tomcat配置參數(shù)為準(zhǔn)),。
$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>節(jié)點(diǎn)中添加如下配置:<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 |
注意:啟動(dòng)tomcat時(shí)必須通過$TOMCAT_HOME/bin/catalina.sh實(shí)現(xiàn),,不能使用$TOMCAT_HOME/bin/startup.sh啟動(dòng),,否則腳本執(zhí)行后容器立刻就退出了。 編寫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 |
創(chuàng)建鏡像 $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 |
第三部:啟動(dòng)容器 webapp通過數(shù)據(jù)卷掛在到容器中進(jìn)行部署,,不需要拷貝到鏡像中,。 $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容器已經(jīng)啟動(dòng),,并且其中安裝的tomcat已經(jīng)成功啟動(dòng),。 訪問:http://172.17.0.12:8080/myweb/,Everything is ok,! 小技巧:啟動(dòng)容器時(shí)帶參數(shù)-t -i和不帶參數(shù)的區(qū)別:帶參數(shù)-t -i時(shí)可以通過Ctrl+C停止容器運(yùn)行,,不帶參數(shù)-t -i啟動(dòng)時(shí),停止容器只能通過命令:$sudo docker stop $containerid實(shí)現(xiàn),。
|