2

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.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214

2 Answers2

4

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"
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Yes after going through all the docs I know I have to follow above steps but how in cloudbuild.yaml commands ? as its my first time using these commands that's why can't figure out how to write those commands – Faiza Zahoor Jul 14 '21 at 16:29
  • I want someone to write (docker pull from DockerHub; docker tag for GCR; docker push to GCR)) commands for cloudbuild.yaml file – Faiza Zahoor Jul 14 '21 at 16:30
2

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.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
  • Maven is just example how they pulled maven image from docker hub in cloudbuild.yaml – Faiza Zahoor Jul 14 '21 at 17:04
  • just like they pulled maven, we can pull other images too, than after that how should I push that to cloud run usig cloudbuild.yaml, thats a question – Faiza Zahoor Jul 14 '21 at 17:05
  • 1
    I don't think you need to publish maven (your build tool) to Container Registry. You just need to build an image of your application and push that. I think you need some hands on time with Docker, and maybe follow Cloud Build tutorials to grasp the concepts and how they fit together. – ahmet alp balkan Jul 14 '21 at 17:26