Deploying Django REST api using app-registry google cloud 2023
Tags: docker, gcloud, django, restapi
Published: 2023-11-14 20:35:37

This post assumes you have already signed up for google cloud and you have already installed [gcloud](https://cloud.google.com/sdk/docs/install-sdk) and [docker](https://docs.docker.com/desktop/install/mac-install/) on your computer, there are so many articles and videos to follow so this should not be an issue.

**1. Create an artifact Registiry**
Type Artifact Registry in the search bar , and you will get the see Artifact Registry in the drop down click that 
![navigating to artifact Registry on google cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7vt7ivf4iy2yt8vckwc.png)
Click "create repository" and give it a new name, select the region were the Artifact Registry will be hosted 

![Creating an artifact Registry on google cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e67krwv07bkr8edxqpd.png)

Then copy the path of your artifact registry so that you can use it to create a docker image you can find this by clicking your artifact registry and then click the copy icon

![Copy gcloud path of the artifact registry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kfap52ubb85asw62sf8.png)

**2. Build the docker image**

When building your docker image don't forget to specify the platform especially when you are using M1, M2, M3 macbook.Google cloud is expecting a linux/amd64 image if its not that your image will fail to deploy and you will get an error that looks something like `Application failed to start: failed to load /bin/sh: exec format error` in the logs.

```
docker build -t updated-sample-image . --platfrom linux/amd64
```
After that tag your docker image , so that it can have a name which google cloud expects, it should basically be the in the path of the artifact registry that you copied above,
for example here i added 

> /update-container:v1.1.0 

then adding the full path plus the name of the image becomes

> us-central1-docker.pkg.dev/poetic-inkwell-404816/step-by-step/update-container:v1.1.0

Don't forget to add a version otherwise it defauts to `lastest` tag so when you do `docker push` you will have an error here i used the `v1.1.0` tag, but it can be any version number you like.

```
docker tag updated-sample-image \us-central1-docker.pkg.dev/poetic-inkwell-404816/step-by-step/update-container:v1.1.0
```

Then push the docker image to the docker repository

```
docker push us-central1-docker.pkg.dev/poetic-inkwell-404816/step-by-step/updated-container:v1.1.0
```
**3. Deploy the docker image**

Finally lets push the docker image to the google cloud 

```
gcloud run deploy --image us-central1-docker.pkg.dev/poetic-inkwell-404816/step-by-step/updated-container:v1.1.0
```
You may get few erros here , make sure the account that you are using has permissons to push to google cloud and if you get `gcould Permission denied by location policies.` just change the server you want to deploy this to when you click gcloud run deploy.

There you go, by now it should give you a link to check out your project remember if you want this to be publically available you need to change the settings in `Cloud run` , to find `Cloud run` search cloud run and then click "cloud run"

![Navigating to Cloud run ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fei8zefupxoxzfz1y2z.png)
find the name of the container , in this case we called it "updated-container" and click security and change the settings to `Allow unauthenticated invocations`

![Changing the Authorization policy of the artifact registry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nttqsoe1m8z49ruudms6.png)