Weasyprint Dockerfile for GAE

拜拜、爱过 提交于 2021-02-05 12:14:16

问题


I am trying to install weasyprint on gae, i know we can install external libraries by passing it in a Dockerfile by changing the runtime from python to custom in app.yaml. I am having trouble creating the Dockerfile for weasyprint libraries.


回答1:


Here is a simple example that I wrote following these instructions. I have tested it and the deployment on GAE has been successful for me:

Dockerfile

FROM gcr.io/google-appengine/python

# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
# Use -p python3 or -p python3.7 to select python version. Default is version 2.
RUN virtualenv /env -p python3.7

# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

# Install platform's packages required for WeasyPrint
RUN apt-get update && apt-get -y install build-essential python3-dev python3-pip \
python3-setuptools python3-wheel python3-cffi \
libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv.
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Add the application source code.
ADD . /app

# Run a WSGI server to serve the application. gunicorn must be declared as
# a dependency in requirements.txt.
CMD gunicorn -b :$PORT main:app

app.yaml

runtime: custom
env: flex

rquirements.txt

gunicorn==19.1.1
Flask==1.0.2
WeasyPrint>=0.34

main.py

from flask import Flask
from weasyprint import *


app = Flask(__name__)


@app.route('/')
def hello():
    return 'Success!'


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080)

Note that as WeasyPrint documentation mentions, platform's packages (such as cairo, Pango and GDK-PixBuf) must be installed separately. They are installed with the following command I added in the Dockerfile:

RUN apt-get update && apt-get -y install build-essential python3-dev python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info


来源:https://stackoverflow.com/questions/61600712/weasyprint-dockerfile-for-gae

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