Installing Java in Google App Engine with Python runtime

 ̄綄美尐妖づ 提交于 2021-02-08 05:44:49

问题


I am new to the Google App Engine. I created a new project with a python runtime and used Flask to expose some API endpoints. One of the methods uses a python library(tabula-py) which has dependency on Java (8+). When I ran locally everything worked but, this is failing after deploying to gcloud. Any tips on setting up Java in the App Engine? I can't install it through the requirements.txt

Thanks a lot!

Regards, Abhi


回答1:


GAE runs your apps in containers that by default only have the runtime that you specify in the app.yaml. However, you can set a custom runtime with Python and Java to run your application with.

In order to do so you must use the GAE Flexible environment and define something like the following in the Dockerfile:

### 1. Get Linux
FROM alpine:3.7

### 2. Get Java via the package manager
RUN apk update \
&& apk upgrade \
&& apk add --no-cache bash \
&& apk add --no-cache --virtual=build-dependencies unzip \
&& apk add --no-cache curl \
&& apk add --no-cache openjdk8-jre

### 3. Get Python, PIP

RUN apk add --no-cache python3 \
&& python3 -m ensurepip \
&& pip3 install --upgrade pip setuptools \
&& rm -r /usr/lib/python*/ensurepip && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache

ENV FLASK_APP main.py
ENV FLASK_RUN_HOST 0.0.0.0
ENV FLASK_RUN_PORT 8080
### Get Flask for the app
RUN pip install --trusted-host pypi.python.org flask

####
#### OPTIONAL : 4. SET JAVA_HOME environment variable, uncomment the line below if you need it

#ENV JAVA_HOME="/usr/lib/jvm/java-1.8-openjdk"

####

EXPOSE 8080
ADD main.py /
CMD ["flask", "run"]

I tested the custom runtime and it worked for me, but I wasn't able to test the tabula-py library because for some reason, the import fails in my environment (even locally). However, I believe it should work.

For reference, I based my Dockerfile on the one in this answer



来源:https://stackoverflow.com/questions/59925185/installing-java-in-google-app-engine-with-python-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!