Search code examples
google-container-registrygoogle-container-builder

Container builder dockerfile with gcloud commands


I have a container builder step

steps:
- id: dockerbuild
  name: gcr.io/cloud-builders/docker
  entrypoint: 'bash'
  args:
  - -c
  - |
    docker build . -t test 
images: ['gcr.io/project/test']

The Dockerfile used to create this test image has gsutil specific commands like

FROM gcr.io/cloud-builders/gcloud
RUN gsutil ls

When I submit a docker build to container builder service using

gcloud container builds submit --config cloudbuild.yml

I see the following error

You are attempting to perform an operation that requires a project id, with none configured. Please re-run gsutil config and make sure to follow the instructions for finding and entering your default project id.
The command '/bin/sh -c gsutil ls' returned a non-zero code: 1
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: exit status 1

My question is, how do we use gcloud/gsutil commands inside the DockerFile so that I can run inside a docker build step ?


Solution

  • To invoke "gcloud commands." using the tool builder, you need Container Builder service account, because it executes your builds on your behalf.

    Here in this GitHub there is an example for cloud-builders using the gcloud command:

    Note : you have to specify $PROJECT_ID it's mandatory for your builder to work.