Deploying Applications with Google Cloud Run: A Step-by-Step Guide

Abinav Ghimire
readytowork, Inc.
Published in
5 min readMar 20, 2024

In the fast-paced world of technology, deploying applications to the cloud has become a critical task for modern development teams. Google Cloud Platform (GCP) offers a suite of tools tailored to streamline this process, with Cloud Run standing out as a particularly user-friendly option. Essentially, Cloud Run provides a hassle-free environment where applications can run seamlessly in the cloud, sparing developers the headache of dealing with intricate technical setups.

Deploy with cloud run

For developers, Cloud Run is akin to a secret weapon. By eliminating the need to grapple with server configurations, it allows them to focus their energy on what they do best: crafting innovative and user-friendly applications. Integrated seamlessly within GCP’s ecosystem, Cloud Run harmoniously interacts with other Google services, further enhancing its appeal. In a world where agility and adaptability are paramount, Cloud Run empowers development teams to swiftly deploy their applications, enabling businesses to stay ahead in the ever-evolving digital landscape.

This article assumes that you have installed Go in your system and have a Go project already set up.

Install Dependencies

Before you begin deploying your application, ensure that you have the necessary dependencies installed. This typically includes Docker, Google Cloud SDK, which provides the gcloud command-line interface for interacting with GCP services and Python as the G-Cloud sdk is dependent on it.

Authenticate and Select Project

Authentication is essential for accessing your GCP resources. Use the gcloud auth login command to authenticate with your Google Cloud account. If you have installed all the dependencies correctly, you will be redirected to login/sign up page which will authenticate you with the gcloud CLI. Once authenticated, use gcloud config set project PROJECT_ID to select the GCP project where you want to deploy your application. You can find your PROJECT_ID from Google Cloud console dashboard’s Project info section.

Project info section in Gcloud

Prepare Docker Image

After setting the project id, we need to containerize our application as Cloud Run requires your application to be packaged as a Docker container. The Dockerfile will be somewhat as given below:

FROM golang:1.18-alpine

WORKDIR /backend-rnd

# Copy everything from this project into the filesystem of the container.
COPY . .

RUN go mod tidy

# Compile the binary exe for our app.
RUN go build -o ./out/dist .

# Start the application.
CMD ./out/dist

First we need to build this container in our local machine using the docker. You can use this command in your terminal to build your container.

# Build the image:
# IMAGE_TAG is set to backend-rnd for this article purpose
docker build -t $IMAGE_TAG -f path/to/Dockerfile --platform linux/x86_64 .

Now, we need to tag the Docker image that we just built using the docker tag command with the format gcr.io/project-id/tag-you-want-in-gcloud. This will be your final command: docker tag backend-rnd gcr.io/deploy-test/test-deploy:new-tag. Finally, push the tagged Docker image to Google Container Registry (GCR) using docker push gcr.io/deploy-test/test-deploy:new-tag .

If everything goes right, we should see our new container in the Container Registry section in the G-Cloud console. Now, select the tagged image you pushed to GCR. Click on the settings icon and choose “Deploy to Cloud Run” to deploy the image to Cloud Run.

Deploy

Configuring the deployment settings

  • Deployment Settings: Specify essential deployment settings such as the service name, region, and maximum instances to ensure optimal performance and scalability.
  • Environment Variables and Permissions: Configure environment variables for your Cloud Run service, including any required variables like BUCKET_NAME, GOOGLE_CLOUD_CREDS, etc. Additionally, ensure that necessary permissions, such as roles assigned to the service account, are correctly set to enable seamless operation.
Deployment security roles
  • Container Port Configuration: Verify that the container port value specified during Cloud Run service creation matches the port defined in your application’s environment. This alignment ensures that incoming requests are correctly directed to the intended port within the container, facilitating smooth communication and functionality.
  • Cloud SQL Connection: If you have a database in the G-cloud SQL, be sure to add the connection string in the Cloud SQL Connection section inside Containers tab.
Deployed test-app

Finally, click the “create” button, and behold! Your application will be launched into the Cloud Run universe and your URL will be ready at the top, ready to shine bright! 🚀

Conclusion

Deploying applications to the Google Cloud Platform using Cloud Run offers a scalable and efficient solution for running containerized workloads. By following the steps outlined in this article, you can deploy your applications to the cloud with ease, enabling you to focus on building and delivering value to your users.

With Cloud Run’s serverless platform, you can leverage the power of GCP’s infrastructure without worrying about managing servers or scaling resources. Start deploying your applications to Cloud Run today and experience the benefits of server-less computing on the Google Cloud Platform.

Happy Coding. 🏹

--

--