vivion34 发表于 2015-12-25 08:36:03

在Docker容器中部署Web应用

在Docker容器中部署Web应用
[日期:2015-02-05]来源:Linux社区作者:nuccch[字体:大 中 小]


本文直接讲解如何在Docker容器中实战部署一个Web应用程序,关于Docker相关的概念和如何安装Docker请参考相关资料完成。
第一步:工具准备
  演示如何在Docker容器中部署一个Java Web应用程序,需要准备的软件工具包括:jre,tomcat和webapp应用。另外,为了实现在容器启动时自动启动webapp,需要编写一个脚本工具完成该工作。
  安装jre,请参考:
  Ubuntu 12.04下安装JDK1.7 http://www.iyunv.com/Linux/2012-06/62239.htm
  Ubuntu 14.04 LTS安装Oracle JDK 1.8http://www.iyunv.com/Linux/2014-11/109216.htm
  CentOS6.3安装JDK和环境配置 http://www.iyunv.com/Linux/2012-09/70780.htm
  Ubuntu 14.04 安装 JDK8http://www.iyunv.com/Linux/2014-09/106218.htm
  Ubuntu下安装JDK图文解析 http://www.iyunv.com/Linux/2014-09/107291.htm
  安装Tomcat,请参考:
  CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤http://www.iyunv.com/Linux/2015-01/111485.htm
  Linux(CentOS)下安装 JDK与Tomcathttp://www.iyunv.com/Linux/2015-01/111119.htm
  如何在Tomcat中部署webapp,请参考:
  http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html
  http://www.iyunv.com/Linux/2015-02/112887.htm
  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,在节点中添加如下配置:,配置片段如下所示:
  
  
  
  
  
  
  
  
  
  
  $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
  #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
  ---> 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:EthernetHWaddr02:42:ac:11:00:0c
  inet addr:172.17.0.12 Bcast:0.0.0.0Mask: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/27config=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实现。
  CentOS 6/7系列安装Docker http://www.iyunv.com/Linux/2014-07/104768.htm
  Docker的搭建Gitlab CI 全过程详解 http://www.iyunv.com/Linux/2013-12/93537.htm
  Docker安装应用(CentOS 6.5_x64) http://www.iyunv.com/Linux/2014-07/104595.htm
  在 Docker 中使用 MySQL http://www.iyunv.com/Linux/2014-01/95354.htm
  在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker http://www.iyunv.com/Linux/2014-10/108184.htm
  Docker安装应用(CentOS 6.5_x64) http://www.iyunv.com/Linux/2014-07/104595.htm
  Ubuntu 14.04安装Dockerhttp://www.iyunv.com/linux/2014-08/105656.htm
  阿里云CentOS 6.5 模板上安装 Docker http://www.iyunv.com/Linux/2014-11/109107.htm
  Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里
  本文永久更新链接地址:http://www.iyunv.com/Linux/2015-02/112889.htm
页: [1]
查看完整版本: 在Docker容器中部署Web应用