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 ・ Д ・)
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!
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
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>
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: {}
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
zsh
brew install maven
mvn -v
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
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
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
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
~~ Realized by referring to this site. ~~
~~ I hit a wall, but this stackOverFlow Solved ~~
If you think about it, it was easier than using maven for the following reasons. (I was worried about that ...)
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
I want to set this site as a reference
I want to set it referring to here
Basics of various configuration files Detailed settings Details of thread pool
Recommended Posts