Search code examples
dockerjakarta-eeglassfishpayara-micro

How can I dynamically set the context-root in Payara Micro?


I am building a docker image for an application that is deployed to several environments. The context-root needs to be different for some of these environments.

Until now I used payara/server-full as the base image but I want to switch to the lightweight payara/micro image.

In the former I was able to set the context-root using:

${PAYARA_PATH}/generate_deploy_commands.sh --contextroot "${CONTEXT_ROOT}" 

This means I was able to dynamically configure the context root when starting a docker container using an environment variable.

I failed to find a similar option for payara micro deployments. As a fallback I tried creating a glassfish-web.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
    <context-root>__CONTEXT_ROOT__</context-root>
</glassfish-web-app>

During container start up (before starting payara micro) I then replaced __CONTEXT_ROOT__ with the value of the environment variable (e.g. 'api/v1') and added it to the WEB-INF directory of the var file.

It seems payara micro doesn't use the context-root specification of that config file. The application is still deployed based on the file name of the '.war'. I could rename the '.war' based on the environment variable but that doesn't allow any 'slashes'/nested paths. Or is there a way to do that?

I'm running out of ideas and didn't find any solutions in similar questions and bugs. Thanks for your help.


Solution

  • Okay, so since I didn't seem to find a built-in way to achieve this I basically copied the behavior of the server-full docker image and it worked.

    In detail:

    1. copied the generate_deploy_commands.sh script to my build folder
    2. added the script to the container during build and making it executable
    3. added env var ENV POSTBOOT_COMMANDS postboot.txt and ENV CONTEXT_ROOT api
    4. ran ${PAYARA_PATH}/generate_deploy_commands.sh --contextroot "${CONTEXT_ROOT}" as part of docker RUN, but before actually starting payara
    5. started payara with --postbootcommandfile $POSTBOOT_COMMANDS
    6. now we can set context root using -e CONTEXT_ROOT=api/v1 when running the docker container

    I hope this helps others looking for a solution.