Deploying Web Apps in Docker containers

Source: Internet
Author: User
Tags apache tomcat docker run

This article directly explains how to deploy a Web application in a Docker container, and refer to the relevant information for Docker-related concepts and how to install Docker.

First Step: Tool preparation

Demonstrates how to deploy a Java Web application in a Docker container, including software tools for: Jre,tomcat and WebApp applications. In addition, in order to automatically start WebApp when the container starts, you need to write a script tool to do the work.

To install the JRE, please refer to: http://www.wikihow.com/Install-Java-on-Linux

To install Tomcat, please refer to: http://blog.csdn.net/williamcs/article/details/8579957

How to deploy WebApp in Tomcat, refer to:

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//can choose another version

APACHE-TOMCAT-6.0.35.TAR.GZ//can choose another version

Myweb.war//write a Web app on your own to find csdn not allowed to upload attachments

start_tomcat.sh



Step two: Make a mirror

Make the image by writing Dockerfile.

There are several tasks that need to be done in Dockerfile:

(1) Installing the JRE

(2) Install Tomcat and complete the basic configuration of deploying Web Apps in Tomcat (to do this: complete the base configuration of Tomcat directly prior to making the image and then copy directly to the image).

(3) External development of 8080 ports (the specific port value can be based on the actual tomcat configuration parameters).

$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

Edit the Tomcat configuration file: Server.xml, add the following configuration to the <Host> node: <context path= "MyWeb" docbase= "/webapps/myweb.war" reloadable= " False "workdir="/tomcat_work "/> the configuration fragment is as follows:

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

Note: When you start TOMCAT, it must be implemented through $tomcat_home/bin/catalina.sh and cannot be started with $tomcat_home/bin/startup.sh, or the container exits immediately after the script executes.

Writing 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


Creating mirrors

$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


Part III: Starting the container

The webapp is mounted in a container through a data volume and does not need to be copied into the mirror.

$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, 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 O n the Java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

Feb, 1:38:42 PMorg.apache.coyote.http11.Http11Protocol Init

Info:initializing Coyote http/1.1 onhttp-8080

Feb, 1:38:42 PMorg.apache.catalina.startup.Catalina load

Info:initialization processed in 788 ms

Feb, 1:38:42 PMorg.apache.catalina.core.StandardService start

Info:starting Service Catalina

Feb 1:38:42 PM Org.apache.catalina.core.StandardEnginestart

Info:starting Servlet engine:apachetomcat/6.0.35

Feb, 1:38:42 PMorg.apache.catalina.startup.HostConfig deploydirectory

Info:deploying Web Application Directorymanager

Feb 1:38:42 PM org.apache.catalina.startup.HostConfigdeployDirectory

Info:deploying Web Application Directorydocs

Feb, 1:38:42 PMorg.apache.catalina.startup.HostConfig deploydirectory

Info:deploying Web Application Directoryroot

Feb 1:38:42 PM org.apache.catalina.startup.HostConfigdeployDirectory

Info:deploying Web Application Directoryexamples

Feb, 1:38:43 PMorg.apache.catalina.startup.HostConfig deploydirectory

Info:deploying Web Application Directoryhost-manager

Feb 1:38:43 PM Org.apache.coyote.http11.Http11Protocolstart

Info:starting Coyote http/1.1 on http-8080

Feb, 1:38:43 PMorg.apache.jk.common.ChannelSocket Init

INFO:JK:AJP13 Listening on/0.0.0.0:8009

Feb, 1:38:43 PMorg.apache.jk.server.JkMain start

INFO:JK running Id=0 time=0/27 config=null

Feb, 1:38:43 PMorg.apache.catalina.startup.Catalina start

Info:server Startup in 842 MS


As the log shows, the Docker container has started and the Tomcat installed in it has started successfully.

Visit: http://172.17.0.12:8080/myweb/,Everything is ok!

Tips: the difference between parameters-t-i and without parameters when starting a container: With parameter-t-i, you can stop the container by CTRL + C, without parameters-t-i start, stop container can only be implemented by command: $sudo Docker stop $containerid.


Deploying Web Apps in Docker containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.