Pivotal Knowledge Base

Follow

How to initialize the application container environment with .profile.d script

Environment

Product Version
Pivotal Cloud Foundry® (PCF)  
 
Purpose
This article will explain how to initialize the application container environment with .profile.d script. During startup, the container starts a bash shell that runs any code under $HOME/app/.profile.d/ before executing the start command. You can put bash code in this file to manipulate the initial environment, at runtime. 
 
Procedure
.profile.d scripts will be run prior to the application starting. The scripts can either be packaged into custom buildpacks or included in the application. Following are the instructions to package .profile.d script into the application: 

1. Under the application project root directory, create .profile.d folder.

2. Under .profile.d directory, create a script file named init.sh (or any other valid file name).
export WELCOME_MESSAGE="Welcome to PCF"
echo $WELCOME_MESSAGE
You can add any other shell scripts such as coping files, configuring environment variables, etc.

3. cf push the application to PCF, monitor app logs with cf logs <APP_NAME>.
2016-06-23T20:31:22.56+0900 [APP/0] OUT Welcome to PCF 
Additional Information
For Java applications, .profile.d directory 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. You can run jar tf <jar-or-war-file> to confirm that the .profile.d directory is at the root of the WAR / JAR file.
 
.profile.d script is executed under vcap user in the Diego container, thus any operation or file access that requires root privilege cannot be performed successfully.

Comments

Powered by Zendesk