How To Deploy A Simple Dockerized Flask App On Google Cloud Run

Google Cloud Platform offers a wide range of serviced which allows you to deploy your own web applications and share them with the whole world. Here you have: App Engine, Compute Engine, Kubernetes clusters or even Cloud Functions for simple endpoints. All this solutions are mainly differ with technical complexity, flexibility and of course costs.

Google Cloud Run – is relatively new service for app deployment purposes offered by Google Cloud Platform. It runs containerized applications and it’s very cost-effective at least in comparison to App Engine. Maybe it is a little bit harder to deploy on Cloud Run then App engine if you are not familiar with Docker, but it’s worth it 🙂
So lets create a simple Flask app and deploy it on Google Cloud Run:

1. Create Python project and add requirements.txt

Lets start from creating a requirements.txt file in Python project directory, which should contain flask and gunicorn libraries:

after that – install that libraries for your project.

2. Create a Flask app

Create a file in your project and paste there the code below:

import os
from flask import Flask

app = Flask(__name__)

def hello_world():
    return "Hello!"

if __name__ == "__main__":, host="", port=int(os.environ.get("PORT", 8080)))

This is your flask app. You can try to run it ans see something like this:

and after that – try adres in your browser – you should see “Hello!” there.

3. Add Dockerfile and .dockerignore

In order to run your Flask application on Cloud Run – you should create a Dockerfile. Dockerfile is something like a list of instructions describing step by step what should be done in order to run your application. So add a file called Dockerfile to your project and paste this text there:

FROM python:3.8.0-slim

# Copy local code to the container image
COPY . /app

# Sets the working directory

# Upgrade PIP
RUN pip install --upgrade pip

#Install python libraries from requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Set $PORT environment variable

# Run the web service on container startup
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

I have added comments to the Dockerfile so you can understand what each step do. Also we should add .dockerignore file to our project – it says what files in our project should be ignored and not used in the build. It makes sure we will not add unnecessary files to docker image and keep it lightweight.
So – create .dockerignore which should contain text below:

Now – we have created all necessary files so your directory should look like this:

4. Create a GCP project and install Google Cloud SDK on your computer

Go to and create a Google Cloud project if you still don’t have one.
Also in order to deploy your Docker container on Google Cloud – you need to install Google Cloud SDK on your computer so you will be able to do that using your local console. It’s quite easy to do, here is an official Google instruction:
When Google Cloud SDK will be successfully installed – you should be able to run gcloud init command in your console:

Run it and follow the steps.

5. Build a container and deploy your app to Cloud Run

After providing all necessary informations (selecting project, zone etc.) in you console after gcloud init – you can build your container image using Cloud Build, by running this command (just replace YOUR-PROJECT-ID with the ID of your GCP project):

gcloud builds submit --tag

When building will be finished, run this command in order to deploy your container image on Cloud Run:

gcloud run deploy --image --platform managed

When the deployment will be finished – you should see your app in Cloud Run tab:

If you will click on the name of your app – you will open a panel with a link to your app and more details like requests chart, logs, etc.:

You can also change request timeout, memory, CPU allocated to instances and other important settings by clicking on “Edit and deploy new revision” button at the top of the panel.

Hope my article will help you to deploy your first contenerized web application on Cloud Run so you will create your unicorn startup and change the world 😉

5 2 votes
Article Rating
Notify of
Inline Feedbacks
View all comments