Deploying Dash to Google Cloud Run in 5 minutes
By: Arturo Tagle Correa
Tags: Data Visualization, Docker, Dash, Data Science, Plotly

Setting up a simple Dash app

This instructions and the example are taken from Dash’s documentation, but are slightly modified in order to run the app on Cloud Run.

Setup and install Python

For this step, you will need Python3 and pip installed. Installation will depend on your OS and settings. On Mac OSX, we recommend install Python using Brew. For installing Python on other OS, you can refer to The Hitchhiker’s Guide to Python.

After installing Python , we recommend running every project on it’s own virtual environment. To enable a virtual environment (or virtualenv or venv for short), run on the directory you are creating the project:

python -m venv .venv

This will creat a virtualenv on folder .venv . To start using this environment, we need to activate it:

. ./.venv/bin/activate

Set up and install Dash

First, install dash and pandas using pip :

pip install dash pandas

And then, freeze these requirements into requirements.txt file:

pip freeze > requirements.txt

This will generate file requirements.txt file, with all libraries and dependencies of the project.

Then, create a simple layout on file app.py . This example is almost the same as the Dash docs, but notice the server variable on line 14. This variable is the Flask instance of our Dash app that need to be used when run on a container.

app.py — Dash example app

Running Dash locally

To run this app locally, on the console run:

python app.py

This will run our app, which will be available on http://0.0.0.0:8080/ and it should look like this:

Dash app running

Setting up a Dockerfile for the app

To run our app in Cloud Run, we will need to define a Dockerfile. Add this to Dockerfile file:

Dockerfile — Dash example app

The main features of this file are:

Use Python slim buster image: Debian-based, lightweigth image of Python. Install requirements from requirements.txt file, and Gunicorn , a Python Http server. Create user app and use this user, so the app doesn’t run on sudo user. Execute app with Gunicorn. Notice the binding on app:server , where app is our app.py file and server is our Flask instance we exposed before.

Compiling the image and running in Cloud Run

Now that we have our Dockerfile ready, we can compile the image, push it to a registry and make the deployment in Cloud Run.

For the image registry, we will use Google Container Registry (gcr.io), but moving to the new Artifact Registry should be straightforward.

For this step, you’ll need GCP SDK set up, and Docker installed (Docker docs).

First, compile the app’s Docker image:

docker build -f Dockerfile -t gcr.io/{PROJECT_ID}/{IMAGE}:{TAG} .

Where PROJECT_ID is your GCP project id, IMAGE is the name the image will have, and TAG is the tag (or version) of the image being built (optional, more info in Docker reference).

When the image is built, push it to the registry (more info on push command in Docker reference):

docker push gcr.io/{PROJECT_ID}/{IMAGE}:{TAG}

And finally, deploy it to Cloud Run using gcloud (gcloud docs):

gcloud run deploy dash_demo \

--image=gcr.io/{PROJECT_ID}/{IMAGE}:{TAG} \

--platform=managed \

--region=us-central1 \ # GCP region

--timeout=60 \ # Request timeout (max 900 secs)

--concurrency=80 \ # Max request concurrency (default 80)

--cpu=1 \ # Instance number of CPU's (default 1)

--memory=256Mi \ # Instance memory (default 256Mi)

--max-instances=10 \ # Max instances

--allow-unauthenticated # Make service publicly available

We have now deployed our dash app, and it should be publicly available on the URL provided by Cloud Run.

Next steps

We have a working Dash app, with hardcoded data to produce the graph shown before (lines 22 to 26 on app.py file of this example).

Try this out! This app can easily be connected to your favorite database: SQL, Google BigQuery or any other data source. We will be posting more on this topic later.