Getting Started with GCP Cloud Run and Deploy Your First Application
Google Cloud Run is a fully managed serverless compute platform that enables you to run stateless HTTP containers on the Google Cloud Platform (GCP). It provides an easy and cost-effective way to deploy your containerized applications, with automatic scaling and built-in security features. In this article, we will explore the basics of GCP Cloud Run and create a simple example.
Learn to leverage the power of Google Cloud Platform (GCP) and unlock the potential of cloud computing with our comprehensive course.
Google Cloud Platform MasterClass: GCP for Absolute Beginner
Getting Started with GCP Cloud Run
Before we get started with Cloud Run, you will need to have the following prerequisites:
- A Google Cloud Platform account
- A containerized application that you want to deploy to Cloud Run
- The Cloud SDK installed on your local machine
To create a new Cloud Run service, follow these steps:
- Open the GCP Console and select your project.
- Navigate to the Cloud Run page from the left-hand side navigation bar.
- Click on “Create Service” to begin.
- In the “Create Service” window, you will be prompted to configure your service settings. Give your service a name, select the region, and choose your deployment platform.
- Click on “Next” to continue.
- In the next step, you will need to configure your container. If you already have a container in a container registry, select “Use an existing container image”. Otherwise, you can choose “Build and deploy a new container image”.
- If you select “Build and deploy a new container image”, you will need to specify your Dockerfile location, image name, and tag. You can also specify any build arguments or environment variables that your container requires.
- Click on “Create” to create your Cloud Run service.
Deploying a Sample Application to GCP Cloud Run
Now that you have created a new Cloud Run service, it’s time to deploy a sample application. In this example, we will deploy a simple Python Flask application that returns a “Hello, World!” message.
- Create a new directory on your local machine and create a new file called app.py with the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
2. Create a new file called Dockerfile with the following code:
# Use the official Python image as the base image
FROM python:3.8-slim-buster
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file into the container and install the dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code into the container
COPY . .
# Expose the port that the application will be listening on
EXPOSE 8080
# Set the command that will be run when the container starts
CMD ["python", "app.py"]
3. Create a new file called requirement.txt with the following code:
flask==1.1.2
4. Build your Docker image by running the following command in your terminal:
docker build -t gcr.io/[PROJECT_ID]/hello-world
Congratulations! You have created your first Cloud Run App.