You can install packages using your console. For example, in macOS:
brew install tesseract
In Google Cloud Functions, you can't install system packages via terminal. Thus, if you want to have a specific system configuration, you'll need to tell somehow Google Cloud how to configure your environment.
That's why dockerizing is useful, because it allows you to set a system with a concrete configuration (container). Google Cloud Build allows you to use a Docker file to configure your application.
Let's assume that you want to deploy the following Python app:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello world' if __name__ == "__main__": app.run(debug = True, host = '0.0.0.0', port = int(os.environ.get('PORT', 8080)))
The app will return 'Hello world' when you access to the route
/ from your browser.
In addition to the app, create a folder with the following files:
├── hello_world | ├── app.py | ├── requirements.txt | ├── Dockerfile | ├── cloudbuild.yaml
You can specify the Python dependencies to be installed by Google Cloud via
requirements.txt . The following dependencies won't be used, but we are going to install them anyway for this example:
You can specify the desired container's state via
FROM python:3.7 RUN mkdir /app ADD . /app WORKDIR /app RUN pip install Flask gunicorn RUN pip install -r requirements.txt RUN apt-get update && apt-get install tesseract-ocr CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app
Dockerfile will configure the system with
layoutparser[ocr]. Moreover, it'll install
tesseract-ocr, not as a Python dependency, but as a global package.
At this point, you can configure the
steps: - name: 'docker' args: [ 'build', '-t', 'gcr.io/$PROJECT_NAME/helloooooo', '.' ] images: - 'gcr.io/$PROJECT_NAME/helloooooo' tags: ['gcp-cloud-build-sample-build']
To build the container:
gcloud build submit
At this point, you can use Cloud Run to deploy the container:
gcloud run deploy --image gcr.io/$PROJECT_NAME/helloooooo
After deploying the container, you'll receive an URL to access the service.
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.