DEV Community

Filipe A. Barroso
Filipe A. Barroso

Posted on • Originally published at filipebarroso.com on

Deploy a Dart Shelf server to Google Run

Photo by guille pozzi on Unsplash
Image description

Let us learn how to deploy your Dart project to Google Run. I will assume you already have a GCP project running, have gcloud installed, Docker installed, and are familiar with their CLI commands. You will also need a Billing account associated to your GCP Project, and the following GCP services activated in your project:

  • Container Registry API
  • Cloud Run Admin API

Cloud Run while has a Free-tier, it is not a Free service, check out the pricing page for more detailed information: https://cloud.google.com/run#section-14

Cloud Run

"Cloud Run: Container to production in seconds."

Cloud Run is a recent Google Cloud Platform product, built on top of KNative published in 2018, which just recently released version 1.0. After 1 year of the release of KNative, Google announced the Cloud Run in which with one container, you can deploy a containerized project, production-ready. It doesn't necessarily to be a Docker container (link).

Note of advice, Docker has a rate limit on downloading images in the free tier, so do consider other repositories for your images to avoid disruptions.

Cloud Run, There are some criteria that your project needs to be under to be a valid project to run in Cloud Run, do check the following website to check if you can run your project: https://cloud.google.com/run/docs/fit-for-run

Dart Shelf

Let me tell you an obvious secret, this section of this blog post will be the only one I need to mention Dart because to deploy to Cloud Run, it doesn't matter how you coded it but what it matters is if it is containerized. This blog post could be using any other programming language to create the server.

I will assume you have a Dart Shelf project ready to deploy, I've shared my notes about Dart Shelf in my previous blog post where I share how to create a simple API, through examples taken from the official resources with some added notes.

To create a container for your Dart project we can use the official Dart Image and the Dockerfile is simple and easy to understand.

FROM google/dart # The official Dart Image

WORKDIR /app # Sets a default folder to keep the project organized

# Do check the note.
ONBUILD ADD pubspec.* /app/
ONBUILD RUN pub get
ONBUILD ADD . /app/
ONBUILD RUN pub get --offline

CMD [] # Makes that is not possible to override parameters while running this image
ENTRYPOINT ["/usr/bin/dart", "bin/server.dart"] # Forces to run the project main server dart file.
Enter fullscreen mode Exit fullscreen mode

Note: Check the reason why I used ONBUILD and why we have duplicated actions here.

Now that we have a Dockerfile and a Dart Shelf project, we can now continue with the deployment to Cloud Run.

How to deploy to Cloud Run

As mentioned, Cloud Run relies on containers to run the project, you can build the project from the source code, deploy through a container image, or even directly from a git repository.

I am going through the process of using a deployed Image to Google's Container Registry. That is done in two steps.

  1. Upload the Container Image to the Container Registry;
  2. Deploy the image to Cloud Run;

1. Uploading a Docker image

Now that we have a container image ready to upload, first, we will need to upload it to the Google Registry. For that, we will first A. tag and then B. submit.

A. Tagging a Docker image to upload to the Google Registry

The first thing you have to do is to tag your image with the prefix gcr.io/<PROJECT_ID>/<IMAGE_NAME>. gcr.io is the registry hosted in the USA for other registers check it here.

Henceforth, our <PROJECT_ID> will be dart_project

To tag an image to be able to upload to the Registry you simple use the following Docker command (more about Docker tags here)

docker tag dart_server:latest gcr.io/dart_project/dart_server
Enter fullscreen mode Exit fullscreen mode

Note how I am tagging our image with the suffix :latest, more about it in the section "Update the project".

B. Deploying the image {#b-dot-deploying-the-image}

With the tag ready you can now upload your image to your Cloud Registry of the project

gcloud builds submit --tag gcr.io/dart_project/dart_server
Enter fullscreen mode Exit fullscreen mode

2. Deploy Image as Google run

We are now ready to deploy our dart server as a Google Run, we only need to define a SERVICE_NAME and deploy it as a Google Run

SERVICE_NAME=dart-server
Enter fullscreen mode Exit fullscreen mode

Note: The name of the services can only include alphanumeric and dashes, hence the name with dash and not the underscore as used so far.

gcloud run deploy $SERVICE_NAME \
  --image=gcr.io/dart_project/dart_server \
  --allow-unauthenticated
Enter fullscreen mode Exit fullscreen mode

Success! Your Cloud Run is now deployed and you can check the URL that the previous command outputs to try your deployed and new server!

Update the project

After the deployment to Cloud Run, it makes sense that you want to update the server with the new code, for that, all you have to do is build your docker image and re-submit your image using the gcloud builds submit since the tag we created is attached to the :latest build of the image.

To make our life easier we could use the following bash script to update our Cloud Run.

SERVICE_NAME=dart-server

gcloud builds submit --tag gcr.io/dart_project/dart_server

gcloud run deploy $SERVICE_NAME \
  --image=gcr.io/dart_project/dart_server \
  --allow-unauthenticated
Enter fullscreen mode Exit fullscreen mode

Conclusion

Cloud Run is incredible to go from code to production, be advised that in this blog post I haven't written about security, managing persistent data, or having a full-blown project running with Cloud Run. Stay tuned for future blogposts to know more.

Meanwhile, if you would like to learn more, there are some Codelabs you can learn from: https://codelabs.developers.google.com/?cat=cloudrun.

Check the Cloud Run official webpage https://cloud.google.com/run.

Top comments (0)