DEV Community

Munisekhar Udavalapati
Munisekhar Udavalapati

Posted on

πŸš€ Deploy NestJS Application on GCP By Munisekhar Udavalapati

# πŸš€ Deploy NestJS Application on GCP for Free
Enter fullscreen mode Exit fullscreen mode

## 1️⃣ Create a Google Cloud Account
Enter fullscreen mode Exit fullscreen mode
  • Go to Google Cloud Console
  • Sign up for $300 free credits (valid for 90 days).
  • Enable Billing (required even for free-tier services).

## 2️⃣ Install Required CLI Tools
Enter fullscreen mode Exit fullscreen mode
# Install Google Cloud SDK
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-441.0.0-linux-x86_64.tar.gz
tar -xvzf google-cloud-sdk-*.tar.gz
./google-cloud-sdk/install.sh

# Restart terminal and initialize GCP CLI
gcloud init
Enter fullscreen mode Exit fullscreen mode
  • Authenticate with your Google account.
  • Set your default project and region.

## 3️⃣ Enable Required GCP Services
Enter fullscreen mode Exit fullscreen mode
gcloud services enable run.googleapis.com containerregistry.googleapis.com artifactregistry.googleapis.com
Enter fullscreen mode Exit fullscreen mode

## 4️⃣ Clone Your GitHub Repository
Enter fullscreen mode Exit fullscreen mode
git clone <YOUR_GITHUB_REPO_URL>
cd <YOUR_PROJECT_FOLDER>
Enter fullscreen mode Exit fullscreen mode

## 5️⃣ Create a Dockerfile (if not already present)
Enter fullscreen mode Exit fullscreen mode
# Use official Node.js image
FROM node:18

# Set the working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the app files
COPY . .

# Build the app
RUN npm run build

# Expose the port
EXPOSE 3000

# Start the application
CMD ["npm", "run", "start"]
Enter fullscreen mode Exit fullscreen mode

## 6️⃣ Build and Push Docker Image to GCP
Enter fullscreen mode Exit fullscreen mode
# Authenticate Docker with Google Cloud
gcloud auth configure-docker

# Set your project ID
export PROJECT_ID=$(gcloud config get-value project)

# Build the Docker image
docker build -t gcr.io/$PROJECT_ID/nest-app .

# Push the image to Google Container Registry
docker push gcr.io/$PROJECT_ID/nest-app
Enter fullscreen mode Exit fullscreen mode

## 7️⃣ Deploy to Cloud Run
Enter fullscreen mode Exit fullscreen mode
gcloud run deploy nest-app \
  --image gcr.io/$PROJECT_ID/nest-app \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated
Enter fullscreen mode Exit fullscreen mode
  • Service name β†’ Press Enter (default: nest-app)
  • Region β†’ Choose us-central1 (free tier)
  • Allow unauthenticated β†’ y (for public access)

## 8️⃣ Get the Live URL
Enter fullscreen mode Exit fullscreen mode
Service [nest-app] revision [nest-app-xxxxx] has been deployed and is serving traffic at:
https://nest-app-xxxxxxxxxx-uc.a.run.app
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Your NestJS app is live! πŸš€


## 9️⃣ Optional: Connect a Custom Domain
Enter fullscreen mode Exit fullscreen mode
  • Go to Cloud Run > Your Service > Custom Domains
  • Follow the setup process to map your domain.

βœ… Final Notes
Enter fullscreen mode Exit fullscreen mode
  • Cloud Run provides 2 million free requests per month.
  • For databases, consider Firestore (free tier available).
  • Want a free VM? Use Compute Engine (e2-micro instance).

πŸš€ Happy coding! πŸ”₯

Top comments (0)