Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation

Overview

I want to make a web application with Java, so I'll try to mess up the local environment! !! I don't want to depend on the IDE, so I'll do my best to create a command-based environment (tsu ・ Д ・)

The resulting environment

Build a Docker environment

Create each container and attach them with docker-compose.yml at the end. For the time being, I want to make something that works at least and customize it in the future!

Apache container creation

I personally want to study apache on centos, so install it and create an image

Dockerfile


FROM centos:7

RUN yum -y update && yum clean all
RUN yum -y install httpd httpd-devel gcc* make && yum clean all

# mod_jk conf files
ADD httpd-proxy.conf /etc/httpd/conf.d/

# Simple startup script to avoid some issues observed with container restart 
ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh

CMD ["/run-httpd.sh"]

httpd-proxy.conf


# forward to tomcat container
ProxyPass / ajp://tomcat:8009/

run-httpd.sh


#!/bin/bash

# Make sure we're not confused by old, incompletely-shutdown httpd
# context after restarting the container.  httpd won't start correctly
# if it thinks it is already running.
rm -rf /run/httpd/* /tmp/httpd*

exec /usr/sbin/apachectl -DFOREGROUND

Tomcat container creation

Dockerfile


FROM tomcat:9.0.1-alpine

MAINTAINER Uchiyama <[email protected]>

ADD tomcat-users.xml /usr/local/tomcat/conf/
ADD context.xml /usr/local/tomcat/webapps/manager/META-INF/context.xml

RUN rm -rf /usr/local/tomcat/webapps/ROOT

CMD ["catalina.sh", "run"]

tomcat-users.xml


<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager-script"/>
  <user username="manager" password="manager!" roles="manager-script"/>
</tomcat-users>

To allow tomcat manager to run on anything other than the hostname localhost. See this stackOverflow for details.

context.xml


<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
<!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

Link with docker-compose

docker-compose.yml


version: '3'
services:
  httpd:
    container_name: httpd-container
    build: ./docker/httpd
    ports:
      - "80:80"
  tomcat:
    container_name: tomcat-container
    build: ./docker/tomcat
    expose:
      - "8009"
volumes:
  data: {}

Try to move

zsh


docker-compose up -d --build
docker-compose ps

Confirm that the container has started and click the URL below to see the tomcat home page. http://localhost/

If it doesn't work, check the access log of httpd or tomcat container to see how far you have reached.

zash


docker exec -it httpd-container tail -f /etc/httpd/logs/access_log
docker exec -it tomcat-container1 tail -f /usr/local/tomcat/logs/localhost_access_log.`date +%Y-%m-%d`.log

Create Java application with Maven

Install maven

zsh


brew install maven
mvn -v

Create a project (template) with Maven

zsh


mvn archetype:generate \ #Project creation
-DgroupId=tech.ucwork \ #Package name specification
-DartifactId=myapp \ #Application name specification
-DarchetypeArtifactId=maven-archetype-webapp #Initial application format specification (web)
-DinteractiveMode=false #Do not run interactively

Hello World display in Java

Directory structure

I forgot which site I referred to, but I will create a file like this

zsh


$ tree -NL 5 src                                                                                                                     +[master]
src
└── main
    ├── java
    │   └── tech
    │       └── ucwork
    │           └── HelloServlet.java
    ├── resources
    │   └── log4j.properties
    └── webapp
        ├── WEB-INF
        │   └── web.xml
        └── index.jsp

7 directories, 4 files

java file

HelloServlet.java


package tech.ucwork;

import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log = LoggerFactory.getLogger(HelloServlet.class);
	
    public HelloServlet() {
    }
    
    @Override
    public void init() {
    	log.debug("servlet init...");
    }

    @Override
  	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	log.debug("servlet service...");
      PrintWriter out = response.getWriter();
	  	out.println("hello5");
  	}
}

log4j.properties


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1} - %m%n

log4j.rootLogger=debug, stdout

Build & packaging with Maven

Command execution with pom.xml like this!

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>tech.ucwork</groupId>
  <artifactId>myapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>myapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.7</version>
        <scope>test</scope>
    </dependency>
    <dependency> 
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>
  </dependencies>
  <build>
    <!--Plugin for m compiled with maven-->
    <finalName>${project.artifactId}-${project.version}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <encoding>UTF-8</encoding>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <!--Plugin for expanding war files from maven to tomcat-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/app</path><!--File (directory name) to be expanded under webapps-->
          <server>tomcat-localhost</server>
          <url>http://localhost/manager/text</url>
        </configuration>
      </plugin>
    </plugins>
  </build> 
</project>

zsh


mvn clean package

Deploy to tomcat

Deploy to tomcat with maven

~~ Realized by referring to this site. ~~

~~ I hit a wall, but this stackOverFlow Solved ~~

Deploy war file to tomcat

If you think about it, it was easier than using maven for the following reasons. (I was worried about that ...)

  1. I don't know how to throw to all tomcat when deploying manager function via http when multiple tomcat is configured
  2. How about being able to deploy via a browser in the first place? Although there are basic authentication products
  3. The theory that it could be deployed by attaching a war file under {TOMCAT_HOME} / webapps /

zsh


mvn clean package
docker cp target/myapp-1.0-SNAPSHOT.war tomcat-container:/usr/local/tomcat/webapps/app.war

Reference) Basics of Maven

TODO

Tomcat initial setting related (deletion of existing files & settings such as logs)

I want to set this site as a reference

Load distribution & session management in Apache and Tomcat cluster configuration

I want to set it referring to here

Various setting tuning of Tomcat

Basics of various configuration files Detailed settings Details of thread pool

Recommended Posts

Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
Prepare a scraping environment with Docker and Java
Build Java development environment with WSL2 Docker VS Code
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Build WordPress environment with Docker (Local) and AWS (Production)
Build docker environment with WSL
Build a Node-RED environment with Docker to move and understand
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build PlantUML environment with VSCode + Docker
Build Rails environment with Docker Compose
Build docker + laravel environment with laradock
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
I tried to build the environment of PlantUML Server with Docker
Until you build the docker environment and start / stop the Ubuntu container
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
Make a daily build of the TOPPERS kernel with Gitlab and Docker
Building Rails 6 and PostgreSQL environment with Docker
Build a PureScript development environment with Docker
Build and manage RStudio environment with Docker-compose
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a Wordpress development environment with Docker
CICS-Run Java applications-(2) Build management with Maven
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Reduce Java / Maven build time with Nexus 3 in OpenShift (okd 3.11) DevOps environment
Build TensorFlow operation check environment with Docker
Install Docker and create Java runtime environment
How to build Rails 6 environment with Docker
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Java automated test implementation with JUnit 5 + Apache Maven
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Build and test Java + Gradle applications with Wercker
Build Apache / Tomcat development environment on Cent OS 7
Build an E2E test environment with Selenium (Java)
Build Spring Boot project by environment with Gradle
Automate Java (Maven) project build with CircleCI + Orbs
Build a development environment for Docker, java, vscode
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Build a Java development environment with VS Code
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Compiled kotlin with cli with docker and created an environment that can be executed with java
Create a Java (Maven) project with VS Code and develop it on a Docker container
Build Elastic Stack with Docker and analyze IIS logs
Build Java development environment with VS Code on Mac
How to build docker environment with Gradle for intelliJ
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Easily build a Vue.js environment with Docker + Vue CLI
Search by POST request with Azure Search + Java Apache HttpClient
[Note] Build a Python3 environment with Docker in EC2
[First team development ③] Share the development environment created with Docker