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


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


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 
RUN chmod -v +x /

CMD ["/"]


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


# 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


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 ["", "run"]


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

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


<?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

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  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"/>

Link with docker-compose


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

Try to move


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.


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


brew install maven
mvn -v

Create a project (template) with Maven


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


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

7 directories, 4 files

java file

package tech.ucwork;

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;


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

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

### direct log messages to stdout ###
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!


<project xmlns="" xmlns:xsi=""
  <name>myapp Maven Webapp</name>
    <!--Plugin for m compiled with maven-->
      <!--Plugin for expanding war files from maven to tomcat-->
          <path>/app</path><!--File (directory name) to be expanded under webapps-->


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 /


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

Reference) Basics of Maven


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