thanks @Ismail and @eljamz, I succeeded in deploying metabase on cloud run thanks to you!
As I had to fiddle around a lot to make it work, I thought I would share the detailed steps.
TL;DR we set up postgres in cloud sql as the backend database of metabase, and then deploy a metabase docker file in Cloud Run
I just wanted to play around, so this is proabably not made for production. Here is my step by step guide. I just assume you already have a google cloud project with billing enabled.
I. enable apis
to make your cloud run instance communicate with the postgres instance we are going to set up , we need to enable cloud sql admin api:
- go to APIs and service, library
- search for google cloud SQL admin api
- enable
II. set up the application database using cloud SQL
- Go to Storage >> Cloud SQL in GCP console.
- Click Create Instance.
- Click Choose Postgres.
- Enter instance name.
- The password you enter here will be for the root user of the database so copy it safe.
- I chose version 10
optional:
7. select the region closest to where you are located
8. customize your instance, and choose machine type, shared core (cheaper)
- once your postgres SQL instance is ready, click on it to get the conection name (should be something like projectname:datacentrelocation:databasename) and copy it somewhere
III. set up google cloud CLI and then prepare metabase docker in upload in cloud run
- in order to send your local docker files to your cloud instance, you have to install the Google Cloud CLI command line tool by going to Install the gcloud CLI | Google Cloud and follow instructions
- in your local computer, in command line, run
gcloud init
and follow instructions
3. on your local computer, create a new directory in your computer named metabase
4. Open your favorite editor and create a file named Dockerfile
inside the metabase
directory. Don't forget the upper case on the D or it wont work
5. Copy the below code and add this in your Dockerfile
file .
Replace the cloudsql_connection_name with your Cloud SQL instance connection name.
FROM metabase/metabase
RUN apk update
RUN apk add --no-cache socat
# Set up symlink to Cloud SQL UNIX socket
RUN ln -s /cloudsql/cloudsql_connection_name/.s.PGSQL.5432 pg.sock
COPY startup.sh startup.sh
RUN chmod +x startup.sh
ENTRYPOINT ["./startup.sh"]
- Save your file.
- Create a new file in the same
metabase
directory and name it as startup.sh
Copy the below code and add this in your startup.sh
file.
#! /bin/bash
# Forward TCP:5432 to Cloud SQL Unix socket
nohup socat -d -d TCP4-LISTEN:5432,fork UNIX-CONNECT:pg.sock &
# Runs Metabase
/app/run_metabase.sh
- save file
- Inside your terminal switch to the metabase folder you jsut created and set your Gcloud project by running:
gcloud config set project your_project_name
- Then, we can build the image (image name contains project name and the folder name, metabase in this case):
gcloud builds submit --tag Google Cloud console
- It takes some time, and then we can deploy it to the Cloud run by:
gcloud run deploy metabase --region europe-west1 --platform managed --image Google Cloud console --port 80
-
During the very first deployment, it will ask if we want to allow unauthenticated invocations — which will basically make the instance public or accessible with permissions.
-
And now, when we check in the Gcloud, we’ll see our instance running. it is not working yet. now, in the google console, click on cloud run, and then on your metabase instance
-
at the top, click on edit & deploy new revision and add the following parameters:
container port: 3000
optinonaly , to keep cost down, set memory to1 Gib and CPU to 1, autoscaling minimum isntance to 1 and max to 1
15 below, set environment variables to the following:
MB_DB_TYPE = in my case ‘postgres’ without the quotes
MB_DB_DBNAME = your dbname
MB_DB_USER = your db username
MB_DB_PASS = the db password
MB_DB_HOST = 127.0.0.1
JAVA_OPTS = ‘-Xmx2048m’ <---- without the quotes but with the dash at the beginning, this is to allow the container to use almost all the 1GB of memory instead of only 400Mb as default.
-
click on the “CONNECTIONS” tab:
Select you Cloud SQL Connection (it should match your startup.sh instance name)
-
click on deploy
-
now wait for the deployment to finish, go back to your cloud run instance and you should have a url at the top, click on it and you start playing around!