3

Does Google Cloud Build keep docker images between build steps by default?

In their docs they say built images are discarded after every step but I've seen examples in which build steps use images produced in previous build ones! so, are built images discarded on completion of every step or saved somewhere for coming steps?

Here's my cloudbuild.yaml.

steps: 
- name: gcr.io/cloud-builders/docker
  args:
  - build 
  - '-t' 
  - '${_ARTIFACT_REPO}'
  - . 

- name: gcr.io/cloud-builders/docker
  args: 
  - push 
  - '${_ARTIFACT_REPO}' 

- name: gcr.io/google.com/cloudsdktool/cloud-sdk 
  args: 
  - run 
  - deploy 
  - my-service 
  - '--image' 
  - '${_ARTIFACT_REPO}' 
  - '--region' 
  - us-central1 
  - '--allow-unauthenticated' 
  entrypoint: gcloud
Puteri
  • 3,348
  • 4
  • 12
  • 27
Rabi
  • 147
  • 2
  • 12
  • Can you share the example where the built image are reused? – guillaume blaquiere Feb 20 '22 at 09:47
  • steps: - name: gcr.io/cloud-builders/docker args: - build - '-t' - '${_ARTIFACT_REPO}' - . - name: gcr.io/cloud-builders/docker args: - push - '${_ARTIFACT_REPO}' - name: gcr.io/google.com/cloudsdktool/cloud-sdk args: - run - deploy - my-service - '--image' - '${_ARTIFACT_REPO}' - '--region' - us-central1 - '--allow-unauthenticated' entrypoint: gcloud – Rabi Feb 22 '22 at 20:02
  • @Rabi Please add it to the question, not in a comment. As well, can you please share where did you read this `In their docs they say built images are discarded after every step`? – Puteri Feb 22 '22 at 20:02
  • I'm simply asking if I build an image in step x and not push it to a repository, is that image stored somewhere so I can use it in later steps in the build or is it discarded on the completion of that step? – Rabi Feb 22 '22 at 20:11

1 Answers1

3

Yes, Cloud Build keep images between steps.

You can imagine Cloud Build like a simple VM or your local computer so when you build an image it is stored in local (like when you run docker build -t TAG .)

All the steps run in the same instance so you can re-use built images in previous steps in other steps. Your sample steps do not show this but the following do:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args:
  - build
  - -t
  - MY_TAG
  - .

- name: 'gcr.io/cloud-builders/docker'
  args:
  - run
  - MY_TAG
  - COMMAND
  - ARG

As well use the previous built image as part of an step:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args:
  - build
  - -t
  - MY_TAG
  - .

- name: 'MY_TAG'
  args:
  - COMMAND
  - ARG

All the images built are available in your workspace until the build is done (success or fail).

P.D. The reason I asked where did you read that the images are discarded after every step is because I've not read that in the docs unless I missed something, so if you have the link to that please share it with us.

Puteri
  • 3,348
  • 4
  • 12
  • 27