Flexibility First - Payara Docker Images Allow a Completely Custom Start-up Configuration
Originally published on 03 Apr 2018
Last updated on 24 May 2021
We work hard to make Payara Server robust, reliable, and innovative so that it's perfect for production deployments. But we never forget about the users and developers. For them, we target flexibility and ease of use in every new feature we add and every tool in the ecosystem we maintain. Our Payara Docker images are an example of it and we're happy about the positive feedback from the user community we receive, as well as the constructive suggestions that help us improve user experience in the future.
Learn How to Use the Payara Platform with Docker in our User Guides:
We've designed the Payara Docker images so that the image can be used from scratch and also everything can be easily configured without building a custom Docker image:
- when the Docker container is started, Payara Server is started automatically
- one or more applications can be dropped into a local directory mounted as a Docker volume and they will be deployed upon start-up
- it's possible to customize how Payara Server is started by environment properties or even custom scripts executed before the server is started
While most of the above aims at ease of use, the last point is especially important to give users flexibility. The startup sequence of the Docker container is split into separate scripts, which you may reuse if you need complete control over how the container is started.
The Payara Docker image uses Tini in the entrypoint, which manages all the child processes properly and guarantees that the Payara Server process runs seamlessly as the main docker process.
The default CMD
argument runs the bin/entrypoint.sh
script, which in turn runs the following:
${SCRIPT_DIR}/init_*.sh
scripts that you may provide for custom use as waiting or initializing during startup, before Payara Server starts. The docker image already contains one such script, init_1_generate_deploy_commands.sh, which generates a command file with commands to deploy applications in the deployment directory${SCRIPT_DIR}/startInForeground.sh
. This script starts the server in the foreground, after all init scripts are finished, in a manner that allows the Payara instance to be controlled by the docker host.
If you need to modify the command file script, e.g. to add a datasource during server start-up, you can create a custom command file and append the generated command file to it. Since version 5.184, it's no longer necessary to modify the entrypoint. Instead, it's enough just to create a custom $POSTBOOT_COMMANDS file:
FROM payara/server-full:5.201
COPY application.war $DEPLOY_DIR
RUN echo 'create-jdbc-connection-pool --datasourceclassname org.postgresql.ds.PGConnectionPoolDataSource --restype javax.sql.ConnectionPoolDataSource --property user=postgres:password=password:DatabaseName=fin_dev:ServerName=${ENV=HOST_IP}:port=5432 fing-pool' \
> $POSTBOOT_COMMANDS
The above will:
- Copy the application to the deployment directory
- Write the custom asadmin command to the default postboot commands file
- Trigger the standard init script, which in turn will generate a deploy command for application.war. The deploy command will be appended after the custom asadmin command to the default postboot commands file
- During server startup, the custom command will be applied first and then the application will be deployed
As in the above example, you can use Payara Server variable references in the POSTBOOT_COMMANDS file and Payara Server will replace them with the value of an environment variable or system property before executing the commands.
We can further customize the startup sequence, e.g. to change the order of commands in the postboot command file. To do that, we can create an init script create_conn_pool.sh which will be executed after the deploy commands are generated:
echo 'create-jdbc-connection-pool --datasourceclassname org.postgresql.ds.PGConnectionPoolDataSource --restype javax.sql.ConnectionPoolDataSource --property user=postgres:password=password:DatabaseName=fin_dev:ServerName=${ENV=HOST_IP}:port=5432 fing-pool' \
>> $POSTBOOT_COMMANDS
We'll copy the script to the Docker image into the "bin" directory with a name that starts with "init_" and so that the name is ordered alphanumerically after the script init_1_generate_deploy_commands.sh. A good name would be init_2_create_conn_pool.sh.
Having a file create_conn_pool.sh with the custom asadmin commands, the Dockerfile would then look like this:
FROM payara/server-full
COPY application.war $DEPLOY_DIR
COPY create_conn_pool.sh ${SCRIPT_DIR}/init_2_create_conn_pool.sh
This will:
- Copy the application to the deployment directory
- Copy a custom shell script so that it's executed after the script that generates the deploy commands
- Trigger the standard init script, which in turn will generate a deploy command for application.war in the default postboot commands file
- Trigger the init_2_create_conn_pool.sh script to append the custom commands after the deploy command
- During server startup, the deploy command will be applied first and then all then the creation of the connection pool.
You can create even more complex scripts that completely rewrite the postboot command file or even apply configuration changes which aren't possible using boot command files.
Related Posts
The Payara Monthly Catch - October 2024
Published on 30 Oct 2024
by Chiara Civardi
0 Comments
The Payara Monthly Catch - September 2024
Published on 30 Sep 2024
by Chiara Civardi
0 Comments