Deploying a Shopify Node App Docker Image to GCP
Tags: docker, node, googlecloud, shopify
Published: 2021-09-10 15:36:58

In [my last post](https://dev.to/deanandreakis/dockerizing-a-shopify-node-app-1djf) I showed how to take a Shopify node app and dockerize it. In this post I will show you how to take that docker image and deploy it to GCP using Google Artifact Registry and Google Cloud Run services. I will also show the Shopify app configuration, installation and execution in a Shopify development store used for testing.

## Prerequisites
We will assume that:
1. A GCP account has been created at console.cloud.google.com
2. A project has been created in GCP for your Shopify app
3. The [gcloud CLI](https://cloud.google.com/sdk/gcloud) has been installed
4. The [docker CLI](https://docs.docker.com/engine/reference/commandline/cli/) has been installed

## Setup Google Cloud Platform (GCP)
We will first setup GCP to accept and store the docker container image and then to deploy and run that image on Google Cloud Run.

An overview of these steps can be found [here](https://cloud.google.com/artifact-registry/docs/transition/changes-docker) but I will show the specific commands for my dockerized Shopify node app named "shnode":

* In IAM add the Artifact Registry Administrator role to your GCP user.
* Go to the Artifact Registry service and make sure the API is enabled.
* In the Artifact Registry select the "create a repository" button. Make sure you select type "Docker" for the repository type.
* Authenticate to the repository as follows:
```
gcloud auth configure-docker us-west4-docker.pkg.dev
```
where us-west4-docker.pkg.dev is the location of the repository we created. We can see this information in the list of repositories:
![Screen Shot 2021-09-09 at 4.59.13 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ujr3xjtc6uz69nhrc6o.png)
* Tag your docker image as follows:
```
docker tag shnode us-west4-docker.pkg.dev/shopify-319117/shnode/shnode:1.0
```
where the format is us-west4-docker.pkg.dev/my-gcp-project/my-repo/my-image:tag1
* Push the container to GCP Artifact Registry:
```
docker push us-west4-docker.pkg.dev/shopify-319117/shnode/shnode:1.0
```
* Create a GCP Cloud Run service: Go to GCP Cloud Run and select the "create a service" button. Once you create a service you will be able to see the URL of the service. We will refer to this URL as **HOST** in the next section where we setup the Shopify app itself:
![Screen Shot 2021-09-09 at 5.33.46 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j17w0q0ryguhux4fbq60.png)
  
* Deploy the image to GCP Cloud Run and start it up:
```
gcloud run deploy --image us-west4-docker.pkg.dev/shopify-319117/shnode/shnode:1.0
```

## Setup Shopify App
The Shopify node app must be setup properly in order for it to be installed and executed in a Shopify store. We will assume that the Shopify app has already been created in your Shopify partner account. 

* Go to the App setup page for your app in your Shopify partner account and make sure the URL's in the URLs section match the **HOST** URL from above when we created the GCP Cloud Run service:
![Screen Shot 2021-09-09 at 5.23.46 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pwjvj29xza0vv9vt2it.png)
* In your Shopify partner account install the app to your development store using the "test your app" section of the app details page:
![Screen Shot 2021-09-09 at 5.44.01 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gx22kb5bvlwjykap9b6p.png)
* The Shopify app source tree itself includes a .env file that has a HOST environment variable. Make sure this matches the **HOST** URL from above when we created the GCP Cloud Run service. If you update this .env file then you will have to rebuild your docker image and re-deploy. To rebuild go to the root of your Shopify app source tree and issue the following command:
```
docker build -t shnode .
```
where "shnode" is the image name. At this point you can continue the deployment from the above section "Setup Google Cloud Platform (GCP)" where we tag the docker image.