Container builder dockerfile with gcloud commands

1.3k Views Asked by At

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 ?

2

There are 2 best solutions below

0
Alioua On BEST ANSWER

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.

1
David Bendory On

To do this, your Dockerfile either needs to start from a base image that has the cloud SDK installed already (like FROM gcr.io/cloud-builders/gcloud) or you would need to install it. Here's a Dockerfile that installs it: https://github.com/GoogleCloudPlatform/cloud-builders/blob/master/gcloud/Dockerfile.slim