I wanted to test if it would be possible to deploy our web, frontity.org (https://github.com/frontity/frontity.org), in Google Cloud Run. I still want to do more tests, but I was able to deploy an initial version so I thought it could be interesting to other users as well. This is the current status:
- Live example -> https://frontity-org-fkikxso7pq-ew.a.run.app/
- gcloud GitHub branch -> https://github.com/frontity/frontity.org/tree/gcloud
And this is the process I followed:
1. Create a project in Google Cloud Run
The first step is to create a project to deploy our app. You should go to https://console.cloud.google.com and create it there. Select a name and a project ID will be assigned. In our case our project ID is frontity-org
.
2. Enable the proper APIs
In the project you just have created, you should go to the APIs and enable the ones we will be using: Cloud Run API and Cloud Build API.
3. Get your app ready
You have to have a Frontity project you want to deploy. You can create one using our quickstart guide. We are using the one we already have for our web - https://github.com/frontity/frontity.org.
Apart from your app code, you have to add two files at the root of your project: Dockerfile
and .gcloudignore
.
Dockerfile
The Dockerfile I used for this first test is:
FROM node:12-alpine
RUN mkdir -p usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install --only=production
RUN npx frontity build
EXPOSE 8080
CMD ["npx", "frontity", "serve", "--port", "8080"]
.gcloudignore
This substitute the common .dockerignore. This is the one used for this first test:
README.md
node_modules
npm-debug.log
.git
4. Build you container
We start using Google Cloud and Docker now, so you should have Docker and Google Cloud SDK (https://cloud.google.com/sdk/docs/) installed.
In order to build it, you have to:
Login in gcloud
First step, is to authenticate, so you can work in our project. In order to do so you should run:
gcloud auth login
It will prompt a new tab in the browser to authenticate.
Set the project we want to work on
You have to select the project you want to work on with the following command:
gcloud config set project PROJECT_ID
In our case:
gcloud config set project frontity-org
Build the container
You have to use the following command, that will read the Dockerfile:
gcloud builds submit --tag gcr.io/PROJECT_ID/CONTAINER
In our case:
gcloud builds submit --tag gcr.io/frontity-org/frontity-org
5. Deploy to Google Cloud Run
Once the build has finished, and it has succeeded, you can deploy it using this command:
gcloud run deploy --image gcr.io/PROJECT_ID/CONTAINER --platform managed
In our case:
gcloud run deploy --image gcr.io/frontity-org/frontity-org --platform managed
It’ll ask you for the service name, the region, and if you want to allow unauthenticated. You can select any service name and region, but when the CLI ask if you want to allow unauthenticated, you have to select “yes”.
And that’s it! You may need to wait some minutes until the Service works as expected, but with these steps it should be enough.
The next test I’d like to run is to use Google Cloud CDN to serve the static files. According to its docs it seems possible. I’m planning to follow this guide to do it and I’ll post the results here.