Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
Google Cloud Run - Working with Python
Next article icon

Google Cloud Run - Working with Python

Last Updated : 13 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Python is one of the most popular programming languages and growing. It's well-suited for a number of use cases, including web applications, machine learning, and big data. Cloud Run lets you use any runtime you want, making it easy to deploy Python in a serverless way. Cloud Run is also fully managed, meaning you don't have to worry about infrastructure scaling if your service starts getting a ton of traffic.

 If you have an existing stateless Python app, all you need to do is add one file to deploy a surface to Cloud Run. No code changes needed. Users like to use Flask for small services like this because it's a lightweight framework that's easy to set up. Its service has the basics, an HTML file where one can create a form to get user input, a simple CSS file, and an app.py file where we set routes and define functions.

The last file that you will need to define is the Docker file. This allows users to customize the runtime of their container to suit their needs exactly. Containers are a way to isolate our application to make it run the same no matter where it's deployed.

When creating a Docker file, we first need to specify a base Docker image with the FROM command as below:

FROM python: 3.8-slim

This is where you set your Python runtime.

The COPY command adds files from your Docker client's current directory as below:

COPY. . /

The RUN command installs Flask, gunicorn, and currency converter dependencies for the service.

RUN pip install Flask gunicorn CurrencyConverter

And finally, CMD is a command to start the application inside the container and bind it to a port.

CMD gunicorn --bind :$PORT app:app

The app: app at the end means import our app from the app.py file.

One of the advantages of Cloud Run is that you can run any Python version you want as long as there is a base Docker image available for it. You can even use the newest version of Python, version 3.8, if you want to.

Now that we have our Docker file, we can build our container with Cloud Build. This bundles up our code along with everything we've added in our Docker file and pushes it to the Container Registry, a place to store container images. And finally, we deploy the service to Cloud Run.

It only takes two commands to get the service out to the world. In the terminal, we first build the container using the builds command.

$ gcloud builds submit --tag gcr.io/PROJECT_ID/PROJECT-NAME

And then we deploy the service using the container image we just built.

$ gcloud run deploy --image gcr.io/PROJECT_ID/PROJECT-NAME

If we check out the Cloud Run section of Google Cloud console, we can see our Cloud Run service. If we click the service, we can see important info, like metrics and the URL of our service.

Here users can also redirect or split user traffic to previous revisions if they discover the latest revision has a breaking change.


Next Article
Google Cloud Run - Working with Python

D

ddeevviissaavviittaa
Improve
Article Tags :
  • Cloud Computing
  • Google-Cloud-Platform

Similar Reads

    Scheduling Cron Job on Google Cloud
    Python is often called a glue language because it's a powerful tool for easily combining multiple different systems. Part of this is because Python has great client library support, which lets you interact with lots of APIs, products, and services from the comfort of Python. But it doesn't just end
    3 min read
    Google Workspace APIs - Using G Suite API
    Most of us are familiar with various Google Workspace products(also called G Suite) like Calendar, Drive, and Gmail, etc.  But along with these products, Google also provides APIs to access the G Suite products to build your own service as you can customize them according to your needs. In this arti
    4 min read
    How to Run a Python Script
    Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc. You might get these Python scripts if you are a begin
    6 min read
    Python Automation Tutorial: Beginner to Advanced
    Python is a very powerful programming language and it's expanding quickly because of its ease of use and straightforward syntax. In this Python Automation Tutorial, we will explore various techniques and libraries in Python to automate repetitive tasks. Automation can save you time and reduce errors
    10 min read
    How to run Python code on Google Colaboratory
    Prerequisite: How to use Google Colab Google provides Jupyter Notebook like interface to run Python code on online Virtual Machines. In this article, we will see how to run simple Python code on Google Colab. Step #1: Open https://colab.research.google.com/ Step #2: Select New Python3 Notebook Step
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences