Home > Software engineering >  How to change the base URL in Tomcat when deployed on docker
How to change the base URL in Tomcat when deployed on docker

Time:10-11

I'm pulling a docker image that has an app running on Tomcat 8.5 base image. I'd like to expose a different port (8080 is already in use on the docker machine) and use the docker machine's DHCP name to handle request. I deploy the service using docker compose like so:

services:                             
  erddap-server:                      
    image: axiom/docker-erddap # Based off of Tomcat 8.5'ish
    container_name: erddap-server
    ports:
      - 8081:8080
    environment:
      - ERDDAP_MIN_MEMORY=1G
      - ERDDAP_MAX_MEMORY=2G
    env_file:
      - .env
    restart: unless-stopped

When I navigate to http://my-docker-server.com:8081/my_app it works fine. If I then click a link in the app it automatically redirects me to http://localhost:8080/my_app/my_link which fails since that's pointing to the wrong machine.

Any ideas on what to change? There must be a way to specify the hostname/base url/root url or whatever it's actually called but I can't find it. If there's a way to configure this via an env variable then that would be first prize.

CLARIFICATION: Taking a look at the server.xml config file for any mention of "localhost" or "8080" shows this:

Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    <Connector server="Apache" secure="true" port="8080" protocol="HTTP/1.1"
port="8080" protocol="HTTP/1.1"

And

<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost"  appBase="webapps"
               prefix="localhost_access_log" suffix=".txt"

CodePudding user response:

This is an erddap application specific problem. The <base_url> item in the /usr/local/tomcat/content/erddap/setup.xml file needs to be changed to reflect the machine/container name. It's set to default to localhost:8080.

In my case I changed it to:

<baseUrl>http://my-docker-server.com:8081</baseUrl>

and then used it as docker bind mount:

volumes: 
      - erddap-data:/erddapData 
      - ./my_erddap_setup.xml:/usr/local/tomcat/content/erddap/setup.xml

A better solution would be to have it completely configured by .env vars but this works.

  • Related