Docker Tomcat users configuration not working

Borgy Manotoy picture Borgy Manotoy · Mar 9, 2017 · Viewed 15.8k times · Source

Update: cleanup and directly indicate the problem and the solution.

PROBLEM:

Docker-tomcat was properly installed and running, except for the 403 Access error in the Manager App. It also seems that my docker tomcat cannot find my tomcat-users.xml configuration.

SOLUTION

Thanks to Farhad and Sanket for the answers.

[Files]:

Dockerfile

FROM tomcat:8.5.11
MAINTAINER Borgy Manotoy <[email protected]>

# Update Apt and then install Nano editor (RUN can be removed)
RUN apt-get update && apt-get install -y \
    nano \
&& mkdir -p /usr/local/tomcat/conf

# Copy configurations (Tomcat users, Manager app)
COPY tomcat-users.xml /usr/local/tomcat/conf/
COPY context.xml /usr/local/tomcat/webapps/manager/META-INF/

Tomcat Users Configuration (conf/tomcat-users.xml)

<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"/>
    <role rolename="manager-script"/>
    <user username="admin" password="password" roles="manager-gui,manager-script" />
</tomcat-users>

Application Context (webapps/manager/META-INF/context.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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" />
  -->
</Context>

[STEPS & COMMANDS]:

  1. Build Docker Image

    docker build -t borgymanotoy/my-tomcat-docker .

  2. Run Image (my-tomcat-docker and set port to 8088)

    docker run --name my-tomcat-docker-container -p 8088:8080 -it -d borgymanotoy/my-tomcat-docker

  3. Go to the container's bash (to check files inside the container thru bash)

    docker exec -it biyahe-tomcat-docker-container bash

Answer

Farhad Farahi picture Farhad Farahi · Mar 9, 2017

First you need to expose your application in the container, so you can connect to it from dockerhost/network.

docker run -d -p 8000:8080 tomcat:8.5.11-jre8

You need to change 2 files in order to access the mangaer app from remote host. (Browser on Docker host is considered remote, only packets received on containers loopback are considered local for tomcat)

  1. /usr/local/tomcat/webapps/manager/META-INF/context.xml Note the commented section.

    <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" />
    -->
    

Please note the commented section.

  1. /usr/local/tomcat/conf/tomcat-users.xml as you stated in the question.

    <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"/>
    <role rolename="manager-script"/>
    <user username="admin" password="password" roles="manager-gui,manager-script" />
    

In order to make changes to files in the container, You can try building your own image, but I suggest using docker volumes or bind mounts.

Also make sure you restart the container so the changes take effect.