The biggest problem with serverless functions is the limitations of the runtime. Most cloud providers only offer a few different programming languages and if you need any special dependencies in your runtime then one of your only options is to provision a virtual machine or something like App Engine to deploy your code - and when you use one of those options you have to worry about scaling them up or down.

GCP has a product called Cloud Run that is the future of cloud computing because allows you to convert any back-end code written in any programming language with any dependencies into a serverless function. Meaning you can deploy fully managed microservices that can auto-scale while only paying for the actual resources that you use.

If you are a developer, all you have to do is write your code and wrap it in a docker image.

The code and image needs to be stateless - which means you cannot save permanent files to the container filesystem, and you cannot run a SQL database and expect to have access to that data later.

If you want to have stateful data you must use a solution that is designed to store stateful data such as Cloud Storage, Firestore or Cloud SQL.

A few examples of how you would use this in a real world project would be hosting a Wordpress site that stores data on Firebase Storage. Or using Ruby on Rails to deploy code that is connected to a Cloud SQL database. Or creating Restful APIs to graph GQL APIs. You can use Angular universal or React Next

But today I will have a server-side rendered JavaScript Application and containerize it with docker, deploy it to cloud run and then hook it up to firebase hosting.

So let’s start this project by setting up this Javascript Knucks app - which is just a view app which is server-side rendered with express Javascript. We will then create a docker image for it so we can deploy it as a microservice to cloud run integrated with firebase.

So I’ve got a fresh node VM installed, updated and upgraded. I’ve installed vscode, docker, javascript, firebase command line tools and Google Cloud SDK

I’ll pop into the command line and get this package

npx create-nuxt-app ssr-app

select default install options - Universal rendering

open up vscode and then run npm run dev - this runs the app locally. So now it is just running the express javascript node.

So now if I launch my webbrowser and look up localhost:3000 i can see my nuxt app running my javascript

Let’s head back to vscode and create a Dockerfile - where we will build our configuration.

FROM node:10 #linux distro with version 10

WORKDIR /usr/src/app #here im just picking the directory i want this service to run out of

ENV PORT 8080 #set env variable called port - cloud run will take this port and expose your server to it when you deploy
ENV HOST 0.0.0.0 # # set host env variable so nuxt can find the host and port

COPY package*.json ./ # the json package including the working files will be copied to the WORKDIR

RUN npm install --only=production # this is to install production dependencies needed

COPY . . # copy dependencies to working directory

RUN npm run build # This is to run NPM build to build the production nuxt app

CMD npm start # command that actually starts the service once all the above lines have been executed to setup the application

now lets hop into our /apps/ssr-app directory and run sudo docker build ./ once its complete the output will provide the docker image ID which we can use to run it locally with

sudo docker run -p 8080:8080 <id>