0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

tomcat: docker環境下で管理画面にアクセスするためのDockerfileは?

Last updated at Posted at 2024-05-06

はじめに:Tomcatとは

Apache Tomcat は、Java用のWebコンテナである。
war(Web Application Archive)ファイルを用いてWebページやRESTAPIを公開することができる。

この記事でできること

dockerのTomcatイメージはデフォルトでは管理画面を提供しないので、利用にはひと手間が必要。
この記事では、docker環境下でtomcatの管理画面の利用方法を説明する。

  • docker環境下で、http://localhost:8080/manager/html から tomcatの管理画面にアクセスできる

image.png

前置き

このページではdockerはセットアップできている前提とする。dockerのセットアップ方法はこちら

結局、どうやったらできるのか

以下の通り。

compose.yml
services:
  backend:
      build: .
      tty: true
      ports:
        - 8080:8080
Dockerfile
# syntax=docker.io/docker/dockerfile:1
FROM tomcat:9.0-jre8-temurin

# tomcat managerのリソースを配置する(デフォルトでは、そもそもManagerページ自体が無い)。
# かわりに、mvコマンドを実行する手もある。
COPY --from=tomcat:9.0-jre8-temurin /usr/local/tomcat/webapps.dist/manager/ /usr/local/tomcat/webapps/manager/

# manager-guiロールを有効化する。
COPY tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xml
# ローカルホスト以外からアクセスできない制限を解除する。
COPY context.xml /usr/local/tomcat/webapps/manager/META-INF/context.xml

Dockerfile内のxmlは、以下のように定義する。

この例では、管理画面にアクセスする時のユーザー/passwordはtomcat/tomcatである。

tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
  <role rolename="manager-gui"/>
  <user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

アクセスするには:localhost:8080/manager/html

docker composeでコンテナを起動する。

docker compose up -d

例の場合、コンテナを起動に下記URLへアクセスすると、管理画面が表示される。

http://localhost:8080/manager/html

image.png

公式docs

HTML User-friendly Interface

Configuring Manager Application Access

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?