Vendure eCommerce on Google Cloud Run

For my company Pinelab.studio I decided to build a SaaS eCommerce platform, to hold multiple webshops. After doing some research I stumbled upon Vendure.io, a headless eCommerce framework written in NodeJS with a GraphQL API. What more could you want?

For scalability and cost reduction I am running Vendure on Google Cloud Run, here is how to do it!

Minimal effort

What needs to be done

  • Listen to the correct Cloud Run port
  • A MySQL database for Vendure
  • Dockerize the application
  • Deploy to Google Cloud Run

Google Storage

This file shows you how to implement Google Storage.

Configure port

apiOptions: {
port: process.env.PORT || 3000
}

This will listen to the PORT variable if defined by Google, or port 3000 when running locally.

MySQL database

  • Create a database and user. Save the host, username, password and database name
  • Set the host, username, password and databasename in vendure-config.ts :
dbConnectionOptions: {
type: ‘mysql’,
synchronize: true,
logging: false,
username: DATABASE_USER,
password: DATABASE_PASSWORD,
host: DATABASE_HOST,
database: DATABASE_NAME,
}

Remember to set synchronize: false after the initial startup, to prevent possible data loss later.

Dockerize the application

First, create a Dockerfile :

# https://github.com/vendure-ecommerce/vendure/issues/458
FROM
node:12

WORKDIR /
usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY
package*.json ./

RUN
npm ci --only=production

# Bundle app source
COPY
. .

# Run the web service on container startup.
CMD
[ "npm", "start"]

Run the following commands in the root folder of your project to build the Docker container and upload it to Google’s container registry (GCR):

docker build -t eu.gcr.io/your-gcloud-project/your-app .
# Configure docker to use Google authentication
gcloud auth configure-docker -q
docker
push eu.gcr.io/your-gcloud-project/your-app

Deploy

gcloud run deploy shops-test \
--quiet \
--image "eu.gcr.io/your-gcloud-project/your-app:latest" \
--region "europe-west1" \
--platform "managed" \
--allow-unauthenticated \
--memory=1G

Improvements

For the complete project: https://github.com/martijnvdbrug/shops

Am I missing something? let me know!

Pinelab specializes in static websites, webshops, and backend development using NodeJS and GraphQL. Visit Pinelab.studio or leave a comment here.

Fullstack developing for the web, check out Pinelab.studio for more!