とりあえずコード書けよ

技術的なことの備忘録。

dockerのtomcatでスタートページとかWebアプリケーションマネージャが404になる場合

結論

webapps.distをwebappsにリネーム

$ mv webapps webapps2
$ mv webapps.dist webapps

環境

  • dockerイメージ
    tomcat:9.0.70-jdk17-corretto-al2

詳細

1.コンテナを起動する

$ docker run -d -p 8080:8080 --name tomcat9 tomcat:9.0.70-jdk17-corretto-al2
Unable to find image 'tomcat:9.0.70-jdk17-corretto-al2' locally
9.0.70-jdk17-corretto-al2: Pulling from library/tomcat
5b4a36b5b78f: Already exists 
583e089287ff: Already exists 
d30e8da53c91: Already exists 
bd78d75dc197: Already exists 
07116e0d2eba: Already exists 
Digest: sha256:c94a52613fb41b81226468bf90226d1b8d9d8e4587daa3db50f49c4bc70c314c
Status: Downloaded newer image for tomcat:9.0.70-jdk17-corretto-al2
68674e39a2244bc1ad5294e92277c7e756c430cbdaac8c54b43d6f1542250c46

2.コンテナの確認

$ docker ps
CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS          PORTS                    NAMES
68674e39a224   tomcat:9.0.70-jdk17-corretto-al2   "catalina.sh run"        7 seconds ago    Up 6 seconds    0.0.0.0:8080->8080/tcp                 tomcat9

3.localhost:8080にアクセスすると404になる

tomcat 404

原因は、いちいちRUNで中身消すなりするの面倒だよねという話でスタートページなどの配置が変更されているためな模様 github.com

実際にlsで中身を見てみると下記のような構成になっている。

$ docker exec -it tomcat9 /bin/bash
$ pwd
/usr/local/tomcat
$ ls
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work

webapps.distがデフォルトのスタートページなどが入っている。

$ ls webapps.dist
ROOT  docs  examples  host-manager  manager

なので、webapps.distをリネームしてやると良い 4.リネームする

$ mv webapps webapps2
$ mv webapps.dist webapps

5.conf/tomcat-users.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.
-->
<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">
<!--
  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.

  Built-in Tomcat manager roles:
    - manager-gui    - allows access to the HTML GUI and the status pages
    - manager-script - allows access to the HTTP API and the status pages
    - manager-jmx    - allows access to the JMX proxy and the status pages
    - manager-status - allows access to the status pages only

  The users below are wrapped in a comment and are therefore ignored. If you
  wish to configure one or more of these users for use with the manager web
  application, do not forget to remove the <!.. ..> that surrounds them. You
  will also need to set the passwords to something appropriate.
-->
<!--
  <user username="admin" password="<must-be-changed>" roles="manager-gui"/>
  <user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
</tomcat-users>

◯変更後

<?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">
  <user username="admin" password="###適当に変更###" roles="manager-gui"/>
</tomcat-users>

6.webapps/manager/META-INF/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" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <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>

◯変更後

<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
<!--
  <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>

所感

やはりだいたい公式に答えは書いてある