DASH Apps with Docker and Google Cloud Run

DASH Apps with Docker and Google Cloud Run

March 12, 2023

You have built an awsome app using Python (and maybe Dash), but now you are wondering how could it be deployed.

Let me guide you through a simple 4 steps guide to have your DASH app moved from local to be deployed on your own domain.

  1. Get the Python App Ready
  2. Prepare the Dockerfile
  3. And the Python Dependencies
  4. Code is done, now go to Google Cloud UI to deploy the Python App

Make sure that you understand the pricing of Google Cloud before deploying ❗

Be aware that:

ArtifactsOpen or Not
The codePotentially Yes
InfrastructureNo, Operating under Google’s Control ❗

For more control over infrastructure, you can try SelfHosting

The App

This is the core of the problem. You want to make sure that your app gets deployed effectively to the world, so that they can use it.

Make sure that you have your app.py including the following:

app = dash.Dash(__name__, external_stylesheets=['https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'])

#...
#the rest of your app
#...

# Start of the application
if __name__ == '__main__':
    
    app.run_server(debug=False, host="0.0.0.0", port=8050)

The Docker Container

Let’s make sure that the app will run consistently across environments, in this case, the google computers that will power the app.

Dockerfile

You will need this file so that Docker will know:

  • Which base image to use
  • Where is your app.py located
  • Which libraries are to be included in the container
  • Which port is the one to be exposed
  • What to execute when the container will be running
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.8

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

#COPY ./app ./ 
#source-destination

# Install production dependencies.
RUN pip install -r requirements.txt

EXPOSE 8050

CMD python ./app/app.py 

Dependencies

This will vary according to the libraries that your app uses.

Basically the idea is to include here ALL the packages, so that they will get included in the docker container with the version that you specified.

A requirements.txt file looks like this:

dash==2.7.0 
plotly==5.11.0

pandas==1.2.4
numpy==1.22.4

sympy==1.7.1
scipy==1.7.1

Deploying the App

Once you will have your container in DockerHub, you can deploy it to Google Cloud Run in few clicks:

  • Create a Service
  • Provide the container’s URL - For example: docker.io/your_dockerhub_user/your_container_name:latest
  • Choose the Port - For example: 8050
  • Assign resources

This will publish your app in a Google’s domain.

But what if you want to user your own?

Mapping Domains

By following this steps, the app will use one of your domains:

  1. Visit https://console.cloud.google.com/run/domains?
  2. Add a mapping
    • Select the service to map - the GCR instance that we just created
    • Specify domain
  3. Update the CNAME as GCR will indicate
    • For example: connect.domains.google.com

FAQ

CI/CD Pipeline

It would be great to have your code changes automatically deployed to DockerHub, so that GCR can read the newest image.

This is an optional step!

You have examples of CI/CD on these projects: