Introduction
Google Cloud Run is a fully managed serverless platform that allows you to run stateless containers. It's an excellent choice for deploying Node.js applications, offering scalability, ease of use, and cost-effectiveness. In this guide, we'll walk through the process of deploying a Node.js application on Google Cloud Run.
Prerequisites
Before we begin, make sure you have:
- A Google Cloud Platform (GCP) account
- Google Cloud SDK installed on your local machine
- Node.js and npm installed
- Docker installed (for local testing)
Step 1: Prepare Your Node.js Application
First, let's create a simple Express.js application:
const express = require('express'); const app = express(); const port = process.env.PORT || 8080; app.get('/', (req, res) => { res.send('Hello from Google Cloud Run!'); }); app.listen(port, () => { console.log(`Server running on port ${port}`); });
Save this as app.js
in your project directory.
Step 2: Create a Dockerfile
Next, create a Dockerfile
in your project root:
FROM node:14 WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD [ "node", "app.js" ]
This Dockerfile sets up the environment for your Node.js application.
Step 3: Create a .dockerignore File
Create a .dockerignore
file to exclude unnecessary files from your Docker image:
node_modules
npm-debug.log
Step 4: Build and Test Your Container Locally
Before deploying, it's a good idea to test your container locally:
docker build -t my-nodejs-app . docker run -p 8080:8080 my-nodejs-app
Visit http://localhost:8080
to ensure your app is working correctly.
Step 5: Set Up Google Cloud Project
If you haven't already, create a new Google Cloud project:
gcloud projects create my-nodejs-project gcloud config set project my-nodejs-project
Enable the necessary APIs:
gcloud services enable run.googleapis.com
Step 6: Build and Push Your Container Image
Use Google Cloud Build to build and push your container image:
gcloud builds submit --tag gcr.io/my-nodejs-project/my-nodejs-app
This command builds your container and pushes it to Google Container Registry.
Step 7: Deploy to Google Cloud Run
Now, deploy your application to Cloud Run:
gcloud run deploy my-nodejs-service \ --image gcr.io/my-nodejs-project/my-nodejs-app \ --platform managed \ --region us-central1 \ --allow-unauthenticated
This command creates a new Cloud Run service named my-nodejs-service
using your container image.
Step 8: Access Your Deployed Application
After deployment, Cloud Run will provide you with a URL to access your application. You can also find this URL using:
gcloud run services describe my-nodejs-service --platform managed --region us-central1
Managing Your Deployment
Updating Your Application
To update your application, simply rebuild your container and redeploy:
gcloud builds submit --tag gcr.io/my-nodejs-project/my-nodejs-app gcloud run deploy my-nodejs-service --image gcr.io/my-nodejs-project/my-nodejs-app
Monitoring and Logging
Google Cloud provides built-in monitoring and logging for Cloud Run services. Access these features in the Google Cloud Console under the "Monitoring" and "Logging" sections.
Scaling
Cloud Run automatically scales your application based on incoming requests. You can configure the maximum number of instances and concurrency:
gcloud run services update my-nodejs-service \ --max-instances=10 \ --concurrency=80
Best Practices
- Optimize Your Container: Keep your container image small and efficient.
- Handle Graceful Shutdowns: Implement proper shutdown handling in your Node.js application.
- Use Environment Variables: Store configuration in environment variables rather than hardcoding them.
- Implement Health Checks: Add a health check endpoint to your application for better reliability.
Troubleshooting
If you encounter issues:
- Check your application logs in the Google Cloud Console.
- Ensure your application is listening on the port specified by the
PORT
environment variable. - Verify that your Dockerfile is correctly configured.
- Double-check that all necessary APIs are enabled in your Google Cloud project.