Is there any way to pull image from docker hub and deploy to cloud run using cloudbuild.yaml

Viewed 772

In documentation they say we can build and deploy container from cloud container registry to cloud run using cloudbuild.yaml file:

steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/PROJECT_ID/IMAGE', '.']
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/PROJECT_ID/IMAGE']
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args: ['run', 'deploy', 'SERVICE-NAME', '--image', 'gcr.io/PROJECT_ID/IMAGE', '--region', 'REGION', '--platform', 'managed']
images:
- gcr.io/PROJECT_ID/IMAGE

And we cal also pull image from docker hub in cloudbuild.yaml file like this :

steps:
- name: "maven"
  args: ["mvn", "--version"]

I want to pull image from docker hub and build and deploy this image to cloud run using cloudbuil.yaml file but I don't know how to do that as I am new to docker and cloud run.

2 Answers

I suspect this question is slightly too broad for Stackoverflow.

You would probably benefit from reading the documentation and learning more about these technologies.

The answer also depends on security constraints.

IIRC, Cloud Run requires that you deploy images from Google Container Registry (GCR) so a key step is in transferring the image from DockerHub to GCR (docker pull from DockerHub; docker tag for GCR; docker push to GCR).

If DockerHub requires authentication, you'll need to login to DockerHub before you can docker pull from it.

If GCR requires authentication (probably), you'll need to login to GCR before you can docker push to it. Commonly, this is effected by granting the Cloud Build's account write permission to the storage bucket that underpins GCR.

All of this is possible using Cloud Build steps (see: cloud-builders)

Once the image is in GCR, you can use the gcloud step to deploy it

These steps can be effected using Cloud Build (cloudbuild.yaml), something of the form:

steps:
- name: "docker"
  args:
  - "login"
  - "--username=[[username]]"
  - "--password=[[password-or-token]]"
- name: "docker"
  args:
  - "pull"
  - "[[image]]:[[tag]]"
- name: "docker"
  args:
  - "tag"
  - "[[image]]:[[tag]]"
  - "gcr.io/${PROJECT_ID}/[[image]]:[[tag]]"
- name: "docker"
  args:
  - "push"
  - "gcr.io/${PROJECT_ID}/[[image]]:[[tag]]"
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args:
  - "run"
  - "deploy"
  - "[[service]]"
  - "--image=gcr.io/${PROJECT_ID}/[[image]]:[[tag]]"
  - "--region=[[REGION]]"
  - "--platform=managed"

You should spend some hands-on time with docker. You can pull an image and push it to a different place like this:

docker pull ubuntu
docker tag ubuntu gcr.io/.../ubuntu
docker push gcr.io/.../ubuntu

I'm not sure how and why Maven is involved here.

Related