-
Notifications
You must be signed in to change notification settings - Fork 0
Deploy Next.js App into Google Cloud Run(GCR) with Docker
References:Fix the typo, spells, grammar, or any other issue. Then show me the fixes with the explanation with the line number.
Create a Front App immediately with Next.js on Google Cloud Run
- Check for prerequisites
prerequisite:
- Next.js app with DockerFiles
- Google Cloud Platform Account Google Cloud Platform
- Docker brew docker
- [Optional] Update setting if local machine is Apple silicone, Above M1 MacBook.
Check for Dockerfile
- add
--platform linux/amd64
flag to the Dockerfile.
# Example
# from
FROM node:18-alpine AS deps
# to
FROM --platform=linux/amd64 node:18-alpine AS deps
- enable
buildx
buildx
enables to build image for other architecture such as amd64More Info: How to install and enable buildx
# if using Docker Desktop set true on experimental
...
},
"experimental": true,
...
- Go to your next.js app
- go to
package.json
- set the PORT as shown below:
"scripts":
{
"dev": "next dev",
"build": "next build",
"start": "next start -p $PORT"
}
NOTE: if you want to run the production build:
# PORT=8080 yarn start npm run start -p 8080 # run Docker build docker run -e PORT=8080 -p 3000:8080 next-app
- Install SDK with brew
You can alternatively follow the Google's official installation guide:
brew install --cask google-cloud-sdk
- Find the location of
google-cloud-sdk
find / -name google-cloud-sdk 2>/dev/null
# it must be:
# .../Caskroom/google-cloud-sdk/<version>/google-cloud-sdk/install.sh
- Run install
/System/Volumes/Data/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/install.sh
# docker build <PATH> -t <REPO_NAME>:<TAG>
docker build . -t example-project:latest
- Go to GCP
- Select project from dropdown next to Google Cloud Logo
- Check your project ID See the example
NOTE:
I will use
this-is-my-project-ID
as example project ID
- Create a different namespace docker image tag
# docker tag <SOURCE_REPO_NAME> gcr.io/<GCP_PROJECT_ID>/<GCR_REPO_NAME>:<GCR_TAG>
docker tag example-project:latest gcr.io/this-is-my-project-ID/example-project:latest
- Create Google Container Registry
CredHelper
gcloud auth configure-docker
[optional] Credential Error
unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication
# login to Google Cloud Platform
gcloud auth login
# change project ID if necessary
gcloud config set project [PROJECT_ID]
# check the current project ID
gcloud config get-value project
- Push the docker image to Google Container Registry (GCR)
# push to GCR
docker push gcr.io/this-is-my-project-ID/example-project:latest
Checkout regions and zone of GCP
GCP Regions and zones VPC Connector: Check Troubleshooting for how to create VPC Connector
Google Secrets Manager: How to create Google Secret Manager
- Run Deploy (Choose the applicable one of commands below)
VPC Connector: Check Troubleshooting for how to create VPC Connector
Google Secrets Manager: How to create Google Secret Manager
- Without Static IP & VPC connector
# deploy to GCR
# gcloud run deploy --image gcr-io/[project ID]/[docker-image tag] --project [project ID] platform managed --region [region such as us-central1]--image --allow-unauthenticated
gcloud run deploy \
--image gcr.io/this-is-my-project-ID/example-project:latest \
--project this-is-my-project-ID \
--platform managed \
--region us-central1 \
--allow-unauthenticated
- If you have static IP & VPC connector & Secret from Google Secret Manager
gcloud run deploy \
--image gcr.io/this-is-my-project-ID/example-project:latest \
--project this-is-my-project-ID \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--vpc-connector=example-connector \
--region=us-central1 \
--vpc-egress=all-traffic \
--update-secrets MONGODB_URI=example-secrets:latest
- Enter service name
# new service will be created as shown below
example-project-app01
# login if asked
For More information:
- GCP Regions and zones: GCP Regions and zones
- VPC Connector: Check Troubleshooting for how to create VPC Connector
- Google Secrets Manager: How to create Google Secret Manager
- Create
.env.local
file at root if you do not have one.
Note: make sure to list the
.env.local
to.gitignore
.
- Defined the following Environment Variable.
# SERVICE_NAME=[Your app name running on the ]
# IMAGE_NAME=[Your docker image name]
# IMAGE_TAG=[Your docker image tag]
# GCP_PROJECT_ID=[project ID of Google Cloud Platform]
# DEPLOY_REGION=[region of deployment app on Google Cloud Run]
#
# Environment Variable shown below are optional and only works when they are set
# on Google Cloud Platform. Deploy.sh will ignore if values are not provided.
#
# VPC_CONNECTOR=[Your VPC Connector]
# VPC_REGION=[Your VPC Connector region]
# SECRET_ENVIRONMENT_VARIABLE_[Name]=[Environment Variable Name]
# SECRET_NAME_[Name]=[Google Secret name]
# example
SERVICE_NAME=example-service-name
IMAGE_NAME=example-project
IMAGE_TAG=latest
GCP_PROJECT_ID=my-project-id
DEPLOY_REGION=us-central1
VPC_CONNECTOR=example-vpc-connector
VPC_REGION=us-central1
SECRET_ENVIRONMENT_VARIABLE_MONGODB=MONGODB_KEY
SECRET_NAME_MONGODB=MONGODB_KEY
- [optional] Login to Google Cloud Platform if necessary
gcloud auth login
- Run deployment
sh deploy.sh