Deploy a Dart Shelf server to Google Run
Tags: dart, backend, gcp, cloudrun
Published: 2021-11-16 22:05:40

---
title: Deploy a Dart Shelf server to Google Run
published: true
date: 2021-11-13 00:00:00 UTC
tags : ["dart", "backend", "gcp", "cloudrun"]
canonical_url : https://filipebarroso.com/articles/dart_shelf_gcp_google_run/
---

_Photo by <a href="https://unsplash.com/@guillepozzi?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">guille pozzi</a> on <a href="https://unsplash.com/@guillepozzi?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mac0gp01s3cvbabh4htq.png)


  

Let us learn how to deploy your Dart project to Google Run. I will assume you already have a [GCP project](https://developers.google.com/workspace/marketplace/create-gcp-project) running, have [gcloud installed](https://cloud.google.com/sdk/docs/install), [Docker installed](https://docs.docker.com/get-docker/), 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](https://knative.dev/) published in 2018, which just recently released [version 1.0](https://knative.dev/blog/releases/announcing-knative-v1-0-release/). 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](https://cloud.google.com/run/docs/developing#containerizing)).

_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](https://cloud.google.com/run/docs/fit-for-run%20)


## 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](https://filipebarroso.com/articles/dart%5Fshelf%5Fbackend/) 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](https://hub.docker.com/%5F/dart) and the Dockerfile is simple and easy to understand.

```Dockerfile
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.
```

Note: _Check the reason why I used [ONBUILD](https://docs.docker.com/engine/reference/builder/#onbuild) and why we have duplicated actions [here](https://github.com/dart-lang/dart%5Fdocker/blob/abba0f926187f52977735bc2f9e0c6630ab07b30/runtime/Dockerfile.template#L11)._

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](https://cloud.google.com/run/docs/deploying-source-code), deploy through a [container image](https://cloud.google.com/run/docs/deploying), or even directly from a [git repository](https://cloud.google.com/run/docs/continuous-deployment-with-cloud-build).

I am going through the process of using a deployed Image to Google's [Container Registry](https://cloud.google.com/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](https://cloud.google.com/container-registry/docs/overview#registries).

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](https://docs.docker.com/engine/reference/commandline/tag/))

```nil
docker tag dart_server:latest gcr.io/dart_project/dart_server
```

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

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


### 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

```nil
SERVICE_NAME=dart-server
```

_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._

```nil
gcloud run deploy $SERVICE_NAME \
  --image=gcr.io/dart_project/dart_server \
  --allow-unauthenticated
```

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.

```bash
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
```

## 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>.