Running Scripts in Containers

If there’s more that you want to do in the Liferay container beyond setting up Tomcat and Liferay files, deploying arifacts, and applying patches, you can use scripts. The container scans for scripts in specific folders at several points in its lifecycle. The following table describes the script insertion points.

Lifecycle PhaseDescriptionTarget Container Folder
Pre-ConfigureExecute scripts before the Configure Phase/usr/local/liferay/scripts/pre-configure
ConfigureExecute after copying files to [Liferay Home]/mnt/liferay/scripts
Pre-StartupExecute scripts before starting up Tomcat/usr/local/liferay/scripts/pre-startup
Post-ShutdownExecute scripts after shutting down Tomcat/usr/local/liferay/scripts/post-shutdown

The following sections demonstrate creating and running scripts in the above-mentioned phases.

Running Scripts During the Configure Phase

If there are more ways that you want to configure Tomcat and Liferay than what the Configure Phase offers, implement them in a Configure Phase script. Please see Container Lifecycle and API for details on the Configure Phase.

Here are steps for setting up Configure Phase scripts using a bind mount.

  1. Create an arbitrary folder for your local configuration scripts.

    mkdir scripts
    
    tip

    If you plan to mount the container’s /mnt/liferay folder already, you can put your Configure Phase scripts into a folder called scripts in your local mount folder to include in the Configure Phase. The [local-folder]/scripts folder would map to the container’s /mnt/liferay/scripts folder.

  2. Create a script(s) for executing your pre-configuration actions.

    echo "inside some-pre-configure.sh" > scripts/configure-phase-script.sh
    
  3. Run a Docker container that bind mounts the script’s folder to the container’s /mnt/liferay/scripts folder.

    docker run -v $(pwd)/scripts:/opt/liferay/scripts ...
    

The entry point executes the script in the Configure Phase after copying files to /mnt/liferay/files and it prints this message:

[LIFERAY] Executing scripts in /mnt/liferay/scripts:

[LIFERAY] Executing configure-phase-script.sh.
in configure-phase-script.sh

Running Scripts in Other Phases

The container provides ways to execute scripts outside of the Configure Phase too.

Lifecycle PhaseDescriptionTarget Container Folder
Pre-ConfigureExecute scripts before the Configure Phase/usr/local/liferay/scripts/pre-configure
Pre-StartupExecute scripts before starting up Tomcat/usr/local/liferay/scripts/pre-startup
Post-ShutdownExecute scripts after shutting down Tomcat/usr/local/liferay/scripts/post-shutdown

Notice that the container’s /usr/local/liferay/scripts folder has this structure:

/usr/local/liferay/scripts
├───pre-configure
├───pre-startup
└───post-shutdown

If you create a host folder with the same structure (see below) and populate it with your scripts, you can make your scripts available to the container by mapping your host folder to the /usr/local/liferay/scripts folder.

[host folder]
├───pre-configure
├───pre-startup
└───post-shutdown

Here are generic steps for creating scripts in the subfolders mentioned above:

  1. Create an arbitrary parent folder for the script phase folders.

    mkdir [host-folder]
    
  2. Create script phase folders.

    cd [host-folder]
    mkdir pre-configure pre-startup post-shutdown
    
  3. Implement your actions in arbitrary scripts in the phase folders.

    warning

    Don’t use the exit keyword in your scripts. Executing exit in a script breaks the entry point startup process.

    echo "inside pre-configure-script.sh" > pre-configure/some-pre-configure-script.sh
    
    echo "inside pre-startup-script.sh" > pre-startup/some-pre-startup-script.sh
    
    echo "inside some-post-shutdown-script.sh" > post-shutdown/some-post-shutdown-script.sh
    
  4. Run a Docker container that bind mounts your host folder to the container’s /usr/local/liferay/scripts/ folder.

    docker run -v $(pwd)/[host-folder]:/usr/local/liferay/scripts ...
    

The entry point executes the scripts during their respective phases and prints messages like this one:

[LIFERAY] Executing scripts in /usr/local/liferay/scripts/pre-configure:

[LIFERAY] Executing some-pre-configure-script.sh.
inside some-pre-configure-script.sh

Conclusion

Now you know how to execute scripts in all parts of the container’s lifecycle.

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy