Pivotal Knowledge Base

Follow

How to Modify the Application Container Environment prior to Application Execution

Environment

Pivotal Cloud Foundry® (PCF) all versions

Purpose

This article will explain how to initialize or modify the application container environment prior to the startup of an application instance.  Part of the application startup process includes a pre-runtime hook, which in turn executes a .profile script.  Inside that script, you can put any commands to initialize or manipulate the initial environment, before your application code executes. 

Procedure

The `.profile` scripts are to be included in the application. The following the instructions below to set this up with your application.

1. Under the application project root directory, create a file named .profile.

2. Edit the .profile file with your favorite text editor. 

For example:

export WELCOME_MESSAGE="Welcome to PCF"
echo $WELCOME_MESSAGE

You can add any bash shell statements or commands that you required into this file.  Examples are coping/moving files, exporting environment variables, running scripts bundled with your application, etc.

It is not necessary to give the .profile script execute permissions with chmod, but won't hurt if you do.

3. To confirm the script is working, run cf push to deploy the application to PCF and monitor the application's logs with cf logs <APP_NAME>.  You should see the output from the echo statement before your application starts.

2016-06-23T20:31:22.56+0900 [APP/0] OUT Welcome to PCF 

Additional Information

For Java applications, .profile file needs to go into the root of the WAR or JAR file, which is usually src/main/webapp for WAR files or src/main/resources for JAR files, but the path could differ depending on how your application is structured. You can run jar tf <jar-or-war-file> to confirm that the .profile file is at the root of the WAR / JAR file.

The .profile script is executed as the vcap user in the application container, thus any operation or file access that requires root privilege cannot be performed successfully.

 

Comments

Powered by Zendesk