9

I understand gcloud uses the Dockerfile specified in the root directory of the source (.) as in the command: gcloud builds submit --tag gcr.io/[PROJECT_ID]/quickstart-image .

but I am trying to specify the Dockerfile to use to build the image which I have not found any resource on how to do that, I don't know if that is possible.

siamsot
  • 1,062
  • 1
  • 7
  • 16
idrisadetunmbi
  • 562
  • 5
  • 16

4 Answers4

7

The only way to specify a Dockerfile (i.e. other than ./Dockerfile) would be to create a cloudbuild.yaml per techtabu@. This config could then use the docker builder and provide the specific Dockerfile, i.e.:

steps:
- name: "gcr.io/cloud-builders/docker"
  args:
  - build
  - "--tag=gcr.io/$PROJECT_ID/quickstart-image"
  - "--file=./path/to/YourDockerFile"
  - .
...
images:
- "gcr.io/$PROJECT_ID/quickstart-image"

If you wish, you also get to specify an alternative name than cloudbuild.yaml.

The ./Dockerfile assumption is presumably to ease the transition to Cloud Build.

I recommend you switch to using cloudbuild.yaml for the flexibility it provides.

DazWilkin
  • 12,847
  • 5
  • 24
  • 48
  • How do I specify variables to the commands though? For example, adding a commit SHA (say SHA is available as $CI_COMMIT_SHA) to the image name in the tag? – idrisadetunmbi Oct 12 '19 at 07:34
  • See: https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values – DazWilkin Oct 23 '19 at 17:33
  • And: https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg – DazWilkin Oct 23 '19 at 17:34
4

You can very easily do this by substituting the . by ./path/to/YourDockerFile, so the gcloud command will be:

gcloud builds submit --tag gcr.io/[PROJECT_ID]/quickstart-image ./path/to/YourDockerFile

So you don't have to use a cloudbuild.yaml for this.

Drew
  • 5,258
  • 4
  • 39
  • 43
Coco
  • 81
  • 6
  • 4
    This didn't work for me: `ERROR: (gcloud.builds.submit) Local file [{src}] is none of .zip, .tgz, .gz` – Zaar Hai Sep 15 '20 at 08:35
  • @ZaarHai Just specify the *directory* containing the dockerfile, *not* the path to the file itself – Mattwmaster58 Jan 13 '21 at 19:34
  • That doesn't really help in my use case - I want to have one folder with two dockerfiles - one for dev (i.e. running unittests) and one for prod. Ended up using tar --transoform. See below. – Zaar Hai Jan 21 '21 at 05:56
1

I am not sure if you can specify Dockerfile, but you can use cloudbuild.yaml file. Check gcloud documentation. If you want to rename this file, you can use config option.

    gcloud builds submit --config cloudbuild.yaml .

A sample cloudbuild.yaml file look like this,

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/quickstart-image', '.' ]
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'
techtabu
  • 12,803
  • 2
  • 21
  • 27
0

Eventually the following hack works for me quite well. If you have e.g. Dockerfile.prod and Dockerfile.dev use the following to build the latter.

tar --exclude-vcs-ignores \  # sort-of .dockerignore support
    --transform='s|^\./Dockerfile.dev|./Dockerfile|' -zcf /tmp/togo.tgz . && \
            gcloud builds submit --tag=gcr.io/my-project/foo:latest /tmp/togo.tgz
Zaar Hai
  • 7,011
  • 7
  • 31
  • 42