Apache Tomcat Docker

Posted onby admin
-->

それぞれコンテナを作成して最後にdocker-compose.ymlでくっつける。とりあえず最低限動くものを作って今後カスタマイズしていきたい! Apacheコンテナ作成. 個人的にapache on centos勉強したいのでインストールしてイメージ作成. Apache Tomcat (or simply Tomcat) is an open source web server and servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Oracle, and provides a 'pure Java' HTTP web server environment for Java code to run in. In the simplest config Tomcat runs in a single operating system process. The process run. Official-images repo’s library/tomcat file. Source of this description: docs repo’s tomcat/ directory. Supported Docker versions: the latest release (down to 1.6 on a best-effort basis) What is Tomcat? Apache Tomcat (or simply Tomcat) is an open source web server and servlet container developed by the Apache Software Foundation (ASF). Docker - Apache Tomcat Docker - NodeJS Docker - NodeJS with hostname Docker Compose - NodeJS with MongoDB Docker - Prometheus and Grafana with Docker-compose Docker - StatsD/Graphite/Grafana Docker - Deploying a Java EE JBoss/WildFly Application on AWS Elastic Beanstalk Using Docker Containers Docker: NodeJS with GCP Kubernetes Engine.

Apache Tomcat is a popular, open-source implementation of the Java Servlet and Java Server technologies. This article shows you how to build a container with Apache Tomcat and a simple Web application, deploy the container to a Service Fabric cluster running Linux, and connect to the Web application.

To learn more about Apache Tomcat, see the Apache Tomcat homepage.

Prerequisites

  • A development computer running:

    • Service Fabric SDK and tools.
    • Docker CE for Linux.
  • A container registry in Azure Container Registry. You can create a container registry in your Azure subscription using the Azure portal or the Azure CLI.

Build a Tomcat image and run it locally

Follow the steps in this section to build a Docker image based on an Apache Tomcat image and a simple Web app and then run it in a container on your local system.

  1. Clone the Service Fabric getting started with Java samples repository on your development computer.

  2. Change directories to the Apache Tomcat server sample directory (service-fabric-java-getting-started/container-apache-tomcat-web-server-sample):

  3. Create a Docker file based on the official Tomcat image located on Docker Hub and the Tomcat server sample. In the service-fabric-java-getting-started/container-apache-tomcat-web-server-sample directory, create a file named Dockerfile (with no file extension). Add the following to Dockerfile and save your changes:

    See the Dockerfile reference for more information.

  4. Login in to Docker and run the docker build command to create the image that runs your web application:

    This command builds the new image using the instructions in the Dockerfile, naming (-t tagging) the image tomcattest. To build a container image, the base image is first downloaded down from Docker Hub and the application is added to it.

    Once the build command completes, run the docker images command to see information on the new image:

  5. Verify that your containerized application runs locally before pushing it the container registry:

    • --name names the container, so you can refer to it using a friendly name rather than its ID.
    • -p specifies the port mapping between the container and the host OS.

    Note

    The port you open with the -p parameter should be the port your Tomcat application listens to requests on. In the current example, there is a Connector configured in the ApacheTomcat/conf/server.xml file to listen on port 8080 for HTTP requests. This port is mapped to port 8080 on the host.

    To learn about other parameters, see the Docker run documentation.

  6. To test your container, open a browser and enter one of the following URLs. You will see a variant of the 'Hello World!' welcome screen for each URL.

    • http://localhost:8080/hello
    • http://localhost:8080/hello/sayhello
    • http://localhost:8080/hello/sayhi
  7. Stop the container and delete it from your development computer:

Apache

Push the Tomcat image to your container registry

Now that you've verified that the Tomcat image runs in a container on your development computer, push it to a repository in a container registry to reduce disruption to your image development and deployment workflows. This article uses Azure Container Registry to store the image, but, with some modification of steps, you can use any container registry you choose. In this article the registry name is assumed to be myregistry and the full registry name is myregistry.azurecr.io. Change these appropriately for your scenario.

Tomcat
  1. Run docker login to sign in to your container registry with your registry credentials.

    The following example passes the ID and password of an Azure Active Directory service principal. For example, you might have assigned a service principal to your registry for an automation scenario. Or, you could sign in using your registry username and password.

  2. The following command creates a tag, or alias, of the image, with a fully qualified path to your registry. This example places the image in the samples namespace to avoid clutter in the root of the registry.

  3. Push the image to your container registry:

Build and deploy the Service Fabric container application

Now that you've pushed the Tomcat image to a container registry, you can build and deploy a Service Fabric container application that pulls the Tomcat image from your registry and runs it as a containerized service in your cluster.

  1. Create a new directory outside of your local clone (outside of the service-fabric-java-getting-started directory tree). Switch to it and use Yeoman to create a scaffold for a container application:

    Enter the following values when prompted:

    • Name your application: ServiceFabricTomcat
    • Name of the application service: TomcatService
    • Input the Image Name: Provide the URL for the container image in your container registry; for example, myregistry.azurecr.io/samples/tomcattest.
    • Commands: Leave this blank. Since this image has a workload entry-point defined, you don't need to explicitly specify input commands (commands run inside the container, which will keep the container running after startup).
    • Number of instances of guest container application: 1
  2. In the service manifest (ServiceFabricTomcat/ServiceFabricTomcat/TomcatServicePkg/ServiceManifest.xml), add the following XML under the root ServiceManfest tag to open the port your application is listening to requests on. The Endpoint tag declares the protocol and port for the endpoint. For this article, the containerized service listens on port 8080:

  1. In the application manifest (ServiceFabricTomcat/ServiceFabricTomcat/ApplicationManifest.xml), under the ServiceManifestImport tag, add the following XML. Replace the AccountName and Password in the RepositoryCredentials tag with the name of your container registry and the password required to sign in to it.

The ContainerHostPolicies tag specifies policies for activating container hosts.

Tomcat docker example
  • The PortBinding tag configures the container port-to-host port mapping policy. The ContainerPort attribute is set to 8080 because the container exposes port 8080, as specified in the Dockerfile. The EndpointRef attribute is set to 'endpointTest', the endpoint defined in the service manifest in the previous step. Thus, incoming requests to the service on port 8080 are mapped to port 8080 on the container.
  • The RepositoryCredentials tag specifies the credentials that the container needs to authenticate with the (private) repository where it pulls the image from. You don't need this policy if the image will be pulled from a public repository.
  1. In the ServiceFabricTomcat folder, connect to your service fabric cluster.
  • To connect to the local Service Fabric cluster, run:

  • To connect to a secure Azure cluster, make sure the client certificate is present as a .pem file in the ServiceFabricTomcat directory, and run:

    In the preceding command, replace your-certificate.pem with the name of your client certificate file. In development and test environments, the cluster certificate is often used as the client certificate. If your certificate is not self-signed, omit the -no-verify parameter.

    Cluster certificates are typically downloaded locally as .pfx files. If you don't already have your certificate in PEM format, you can run the following command to create a .pem file from a .pfx file:

    If your .pfx file is not password protected, use -passin pass: for the last parameter.

  1. Run the install script provided in the template to deploy the application to your cluster. The script copies the application package to the cluster's image store, registers the application type, and creates an instance of the application.

After you have run the install script, open a browser and navigate to Service Fabric Explorer:

  • On a local cluster, use http://localhost:19080/Explorer (replace localhost with the private IP of the VM if using Vagrant on Mac OS X).
  • On a secure Azure cluster, use https://PublicIPorFQDN:19080/Explorer.

Expand the Applications node and note that there is now an entry for your application type, ServiceFabricTomcatType, and another for the first instance of that type. It may take a few minutes for the application to fully deploy, so be patient.

Dockerfile For Apache Tomcat

  1. To access the application on the Tomcat server, open a browser window and enter any of the following URLs. If you deployed to the local cluster, use localhost for PublicIPorFQDN. You will see a variant of the 'Hello World!' welcome screen for each URL.

    • http://PublicIPorFQDN:8080/hello
    • http://PublicIPorFQDN:8080/hello/sayhello
    • http://PublicIPorFQDN:8080/hello/sayhi

Clean up

Use the uninstall script provided in the template to delete the application instance from your cluster and unregister the application type.

After you push the image to the container registry you can delete the local image from your development computer:

Next steps

  • For quick steps on additional Linux container features, read Create your first Service Fabric container application on Linux.
  • For more detailed steps on Linux containers, read the Create a Linux container app tutorial tutorial.
  • Learn more about running containers on Service Fabric.

Tomcat startup time is reduced by ~30% by enabling the Shared Class Cache (SCC) in the Docker images.

In my earlier blog , I measured the Performance of Tomcat with Tradelite7 Application without using any docker images and we saw the boost of Startup time with default Shared Cache in OpenJ9. In this article, let’s do some experiments using docker images of Tomcat and how the Shared Class Cache affects the Startup Performance.

As default SCC with OpenJ9 is disabled when running in Docker, we need to explicitly enable SCC. If we enable SCC in runtime, the caching classes and AoT doesn’t persist in docker when it stops. So, one way to get the SCC improvements is by populating SCC during build time.

In this article, I will show how to create an SCC during build and compare the Performance with “No SCC” and “explicit SCC” for Tomcat with OpenJ9.

To start with, I picked the Dockerfile of Tomcat 9 for OpenJ9 from Tomcat Docker library and used it to build the default Tomcat docker images which has “No SCC”.

Now, we have to create a docker image which uses SCC with OpenJ9.
For that, following modifications are done in Docker file

  1. Add JAVA_OPTS which has SharedClassCache related options. -Xshareclasses:name=tomcat_scc,cacheDir=/tmp,enableBCI -Xscmx20M
  1. Do a cold Tomcat Startup run as part of build itself. This helps in creating the SCC required.
  2. Now, change the JAVA_OPTS to make -Xshareclasses flag readonly. , to avoid any AOT that happens at the start of container as it doesn’t give any benefit to the container.
    Below are the lines I added in Dockerfile for above instructions. (just before the EXPOSE 8080 command)


With the above changes in Dockerfile, we can build an image which uses SCC with OpenJ9. I also created an another image which runs the tomcat Server for 10 times (instead of one time as mentioned in above snippet) to populate the SCC – only to check if that can improve more Startup time.

To calculate the Startup time and Footprint, below methods are used:

Apache Tomcat Docker Login

  • For Startup time: Need to mount the logs directory of the container into your server and Use the log (catalina.out) generated by the server and look for INFO [main] org.apache.catalina.startup.Catalina.start Server startup in **** milliseconds.
  • For Footprint: I initially used docker stats to collect the Memory_Usage of a container. Because docker stats doesn’t add up Cache memory, the docker with SCC showed lesser memory_usage (which is not we are looking for!)
    So, I had to measure it by summing up RSS of a process and Huge pages used (if enabled). RSS of a process is calculated using “ps -p <pid> -o rss”. Huge Pages used is the difference of the value from “/proc/meminfo” before and after the tomcat Startup.

The command below is used to start the docker container:

Below are the results of Startup and Footprint from my experiments:

Above graphs Y-axis uses normalized data since it’s the relative picture that matters.
From the graph data above, OpenJ9-ExplicitSCC(with 10 warm-ups during build) didn’t help much in reducing the Startup time as OpenJ9-ExplicitSCC (which does a single run during build) also gave similar results.

Size of docker images (on disk) with tomcat using ExplicitSCC and the original one are:

Tomcat

Tomcat Docker Hub

All Above experiments are done on a machine with following config

Apache Tomcat Docker

  • CPU Type: Intel(R) Xeon(R) Gold 6126 CPU @ 2.60GHz
  • 24 physical, 48 logical processors.
  • Pinned 4 processors to the server during the run. (used numactl — physcpubind=0–3 — membind=0)

Apache Tomcat Docker

Summary:

Apache Tomcat Docker Download

  • From the data we got, we can confirm that OpenJ9 with SCC has an out-of-box ~30% reduced Startup time with only 2% increase in Footprint against no SCC.
  • Because of this reduced Startup time, the Tomcat community should perhaps consider adding a step to create an SCC by doing a single start of Tomcat Server during build into their OpenJ9 based docker images.