Skip to content Skip to sidebar Skip to footer

Add Java/jre/jvm In Python Flask Cloud Foundry/ibm Cloud Application

I am running a python flask application in a Cloud Foundry/IBM Cloud-environment. In my application I try to connect to DB2 Warehouse on Cloud with the IBMDBPY-package. This packag

Solution 1:

After much trial and error, the solution that worked for me was a multi build pack deployment as described below:

cf push -b https://github.com/cloudfoundry/multi-buildpack

and in the root of your project include a multi-buildpack.yml with the following

buildpacks:
  - https://github.com/cloudfoundry/apt-buildpack
  - https://github.com/cloudfoundry/python-buildpack

and an apt.yml with the following:

--- packages:openjdk-8-jrerepos:debhttp://ppa.launchpad.net/openjdk-r/ppa/ubuntutrustymainkeys:https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A

In the runtime.txt file, (also in the root of the project) is the version of python python-3.6.6

Unfortunately, JAVA gets installed in your home deps directory and as such you'll have to create a JAVA_HOME environment variable in the manifest.yml.

JAVA_HOME: /home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/

I also tried to add jre/bin to the path this way

PATH: /bin:/usr/bin:/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/bin

However the push wipes this out and installs only the default path /bin;/usr/bin fortunately for me, JAVA_HOME was enough to get jaydebapi to work with the database driver for the jar files I had. If you need this environment variable, maybe try using the python os package to issue a command to modify the path as part of the startup.

Solution 2:

To include a more modern answer. As I write this, most (all worth using) versions of Cloud Foundry you encounter will support multiple buildpacks out-of-the-box. Thus you don't need the multi-buildpack buildpack anymore.

Instead, you can simply cf push and specify multiple buildpacks.

https://docs.cloudfoundry.org/buildpacks/use-multiple-buildpacks.html

This can either be done by setting multiple -b flags to cf push or by using a manifest.yml file and doing something like this:

...
buildpacks:
  - buildpack_1
  - buildpack_2
...

In either case, the execute in the order you list them.

https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#buildpack

The rest of the answer remains the same as @lamonaki's answer.

  • Invoke both the apt-buildpack and the Python buildpack, in that order.

  • Add the apt.yml file and in it, indicate the Java package you'd like to install.

    Ex from @lamonaki's answer:

    --- packages:openjdk-8-jrerepos:debhttp://ppa.launchpad.net/openjdk-r/ppa/ubuntutrustymainkeys:https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
  • Add runtime.txt to set the Python version you'd like to install

  • Add a .profile file to the root of the project, just like apt.yml and runtime.txt. In that, add lines for export JAVA_HOME=/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/ and export PATH=$PATH:$JAVA_HOME/bin and potentially LD_LIBRARY_PATH if you need to reference any custom shared libraries in your Java or Python code.

You might be thinking, why apt-buildpack instead of the Java buildpack. Unfortunately, the Java buildpack in its current incarnation only supports running as a final buildpack (i.e. the last buildpack in the list of buildpacks). That rules it out as a good candidate here since you want the Python buildpack to be last. The Java Cloud Native Buildpacks will resolve the issue, but as I write this, there are no Cloud Native Buildpacks which run natively on CF.

Post a Comment for "Add Java/jre/jvm In Python Flask Cloud Foundry/ibm Cloud Application"