How to Deploy a Container Image to Artifact Registry and Cloud Run

Kristoff, TutorialGoogle Cloud
Back
cloud-run

Introduction

Google Cloud Platform (GCP) offers a wide range of services for deploying and managing containerized applications. In this tutorial, I'll walk you through the process of deploying a container image to Artifact Registry and Cloud Run. By the end of this tutorial, you will have a fully functional containerized application running on Cloud Run.

Cloning the API Project

First, clone the API project we'll be using as an example:

git clone git@github.com:chrystalio/movie-api.git

or if you prefer HTTPS:

git clone https://github.com/chrystalio/movie-api.git

Modifying the Dockerfile

Once the project is successfully cloned, change to the project directory. The next step is to check the Dockerfile. We need to modify the exposed port in the Dockerfile from 3000 to 8080.

Type this command :

cd movie-api

Open the Dockerfile using a text editor. For example, using nano:

nano Dockerfile

Modify the exposed port from 3000 to 8080:

FROM node:14

# Create app directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy app source code
COPY . .

#Expose port and start application (Replace the existing port to 8080)
EXPOSE 8080 

CMD ["npm", "start"]

After finishing the port modification, save the file by using the keyboard shortcut:

Ctrl + x, then type Y, and press Enter.

Then, verify that your Dockerfile configuration is correct by running the command:

cat Dockerfile
cloud-run

Creating the Docker Repository inside Artifact Registry

Next, we need to create a Docker repository inside Artifact Registry. This repository will store the container image that we'll deploy to Cloud Run.

To create a Docker repository, run the following command:

gcloud artifacts repositories create movie-api-repo --repository-format=docker --location=asia-southeast2 --description="Movie-API Docker Repository" --project=dummy-419003
cloud-run

After the process is finished, we can verify that our repository was successfully created by running this command:

gcloud artifacts repositories list

Next step, we need to authenticate Docker to use the Artifact Registry repository. Run the following command:

gcloud auth configure-docker asia-southeast2-docker.pkg.dev

In this case, I'm using "asia-southeast2" as my region, you can adapt this based on your selected region.

Building and Pushing the Docker Image to Artifact Registry

Now, we need to build the Docker image and push it to the Artifact Registry repository. Run the following command:

docker build -t asia-southeast2-docker.pkg.dev/dummy-419003/movie-api-repo/movie-api:v1 .
cloud-run

After the build process is complete, push the Docker image to the Artifact Registry repository:

docker push asia-southeast2-docker.pkg.dev/dummy-419003/movie-api-repo/movie-api:v1
cloud-run

As you can see, the Docker image has been successfully pushed to the Artifact Registry repository.

cloud-run

Deploying the Container Image to Cloud Run

Now that we have the Docker image in the Artifact Registry repository, we can deploy it to Cloud Run. Navigate to the Cloud Run in the Google Cloud Console and click on "Create Service".

cloud-run

Inside the "Create Service" menu, ensure you select the option to deploy one revision from an existing container image and click to select the button inside the container image URL.

cloud-run

Select the container image from the Artifact Registry and navigate to your uploaded container image repository. Then, select the version you want to use and click the "Select" button.

cloud-run

After selecting the container image, the container image URL will be displayed. Ensure that you select the Allow unauthenticated invocations option under the Authentication section to allow public access to your API.

cloud-run

Leave the rest of the options as default and click "Create." Wait a few seconds for the service to be created.

cloud-run

After the service is created, you can access the service URL : https://movie-api-t75iv37twq-et.a.run.app/

Or if you prefer to deploy using the command line, you can use the following command:

gcloud run deploy movie-api2 --image=asia-southeast2-docker.pkg.dev/dummy-419003/movie-api-repo/movie-api:v1 --region=asia-southeast2 --allow-unauthenticated

Since I already deployed the service with the name "movie-api", I used "movie-api2" as the service name.

cloud-run

As you can see, the service has been successfully deployed to Cloud Run. Here's the service URL: https://movie-api2-t75iv37twq-et.a.run.app/

Conclusion

Now that you've successfully deployed your Docker container to the Artifact Repository and your application is up and running on Cloud Run, you're ready to take your skills to the next level. Why not apply what you've learned to your own projects? Practice makes perfect, so dive into your next project with confidence and keep building amazing things!

© Kristoff