19

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,501
  • 1
  • 14
  • 20
idrisadetunmbi
  • 877
  • 2
  • 9
  • 22

6 Answers6

19

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
  • 32,823
  • 5
  • 47
  • 88
  • 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
  • Nice answer, thank you @DazWilkin. Anyway, @idrisadetunmbi, you can find the doc as link above or you can use for example`ENV NODE_ENV=development` in your Dockerfile to set environment variables. – maohieng Jul 08 '21 at 14:36
16

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
  • 6,311
  • 4
  • 44
  • 44
Coco
  • 211
  • 3
  • 7
  • 14
    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
  • 8
    @ZaarHai Just specify the *directory* containing the dockerfile, *not* the path to the file itself – Mattwmaster58 Jan 13 '21 at 19:34
  • 3
    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
  • 2
    this works for the case where you are OK with the build context of specified directory. However, for me it needed to be from repo root, so going with @DazWilkin answer above – Yuriy Mankovskiy Nov 03 '21 at 12:11
6

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
  • 23,241
  • 3
  • 25
  • 34
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
  • 9,152
  • 8
  • 37
  • 45
0

I tried multiple solutions but they didn't work for me. I was using the bitBucket pipeline So I used the following approach.

  1. When I run stage env

    cp ./ENV/stage/Dockerfile ./Dockerfile gcloud builds submit --tag gcr.io/your-project/your-image

  2. When I run prod env

    cp ./ENV/prod/Dockerfile ./Dockerfile gcloud builds submit --tag gcr.io/your-project/your-image

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34754577) – Ram Chander Aug 01 '23 at 10:27
-1

The gcloud build submit will detect automatically your Dockerfile if present, make sure your place the file in your root directory. If you don't know that much about docker you can use this command

gcloud beta run deploy --source=.

Execute it at your root directory. the buildpacks will create the dockerfile for you and it is able to detect the stack of your application so it can build with its relevant tools.

pmadhu
  • 3,373
  • 2
  • 11
  • 23
  • 2
    the original question is useful if there are multiple Dockerfile files in the projects, as in the case of an NX monorepo, so having the dockerfile in the root is not an option, as we need a different Dockerfile for each project. – EmmanuelB Jun 29 '22 at 11:22