Open In App
Related Articles

Guide to deploy containers on Google Cloud Run

Like Article
Save Article
Report issue
Google Cloud Run is one of the fast and scalable solution for your API deployment over secure https connection and can be considered as first choice of every developer. Well firebase is for static websites, right? What if we can deploy Dynamic websites on firebase too? All you need is Cloud Run. Here, in this tutorial we will be showing you how you can deploy a simple website on Cloud Run. we will be using Flask for the website. First set-up your system with Google Cloud SDK. If you don’t know how to do it I recommend you read through their official documentation here.

Enable Cloud Run API

Next you need to enable the Cloud Run API.
  • Go to Marketplace in Cloud Console.
  • Then Search for Cloud Run API and enable it
  • After all these steps are done your page should look something like this


Creating the project

Now the fun part begins. Let’s get to the code.
  • First create a project folder. In my case, I will be using the name Cloud Run Tutorial.
  • Go to the project folder and create a file called requirements.txt and add Flask==1.1.1 to the file.
  • Now create a python file called and add the following lines to it.
    # importing Flask
    from flask import Flask
    app = Flask(__name__)
    # specifying the route
    def hello_world():
        return 'Hello, World !'
    # this if block make sure that Flask app runs 
    # only if the file is executed directly
    if __name__ == '__main__':
        # exposing port 8080 of localhost ='', port = 8080)

    If you have any confusion with the above code or want to learn more I prefer you read the flask documentation here.

  • Next create a Dockerfile with the name Dockerfile (without any extension) and add the following lines of code to it.
    #pulls python 3.7’s image from the docker hub
    FROM python:alpine3.7 
    #copies the flask app into the container
    COPY . /app
    #sets the working directory
    WORKDIR /app
    #install each library written in requirements.txt
    RUN pip install -r requirements.txt 
    #exposes port 8080
    EXPOSE 8080 
    #Entrypoint and CMD together just execute the command 
    #python which runs this file
    ENTRYPOINT [ "python"
    CMD [ ""

Deploy the project

Now your project is ready to be deployed. To deploy it to Google Cloud Run, we need to containerize the project and the container to Google Container Registry (gcr). Let’s see how it can be done.
  • Go to your project directory and open up the terminal.
  • Type gcloud init and select the GCP project in which you want to deploy the website and note the project id.
  • Type export project_id=THE_PROJECT_ID_NOTED.
  • Then type gcloud builds submit –tag${project_id}/helloworld:v1. This will take few minutes to complete.
  • Then type gcloud run deploy –image${project_id}/helloworld:v1 –platform managed
    • You will be prompted for the service name: press ENTER to accept the default name, helloworld.
    • You will be prompted for region: select the region of your choice.
    • You will be prompted to allow unauthenticated invocations: respond y

    Then wait for a few moments until the deployment is complete. On success, the command line displays the service URL.

  • Visit your deployed container by opening the service URL in a web browser.


If you have done all the steps correctly then, your Cloud Run page should look something like this Final Cloud Run

Congratulations! You have just deployed an application packaged in a container image to Cloud Run. Cloud Run automatically and horizontally scales your container image to handle the received requests, then scales down when demand decreases.

Last Updated : 19 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads