Search code examples
dockergoogle-cloud-platformbuilddockerfilegcloud

gcloud submit : specify Dockerfile with --file parameter doesn't seem to work


In my project, Dockerfiles are not stored in the same place that the files I want to COPY in my image. Like :

root
     Dockerfiles:
          app_name1:
              dockerfile1
          ...
    folder
          app1
          app2
          ...
cloudbuild

I have a cloudbuild.yaml like that :

steps:
- name: gcr.io/cloud-builders/docker
  args:
  - build
  - '--tag=eu.gcr.io/$PROJECT_ID/${_IMAGE_NAME}:${_STAGE}'
  - **'-f ./Dockerfiles/${_IMAGE_NAME}/Dockerfile'**
  - .
images: [
    'eu.gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
]

and I build the image from root like that :

gcloud builds submit .
      --config=cloudbuild.yaml
      --substitutions [...]
      .

I checked inside my GCP Bucket, the source specified in gcloud submit (.) is successfuly uploaded and so should be accessible in ./workspace/.

But the build fails with the following error :

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /workspace/ .: no such file or directory

It seems like he doesn't want to use the Dockerfile specified in the --file argument. And I can't manage to find an exemple of cloudbuild.yaml using this argument. Any idea on what's happening ?


Solution

  • It's possibly that, if your file structure is accurate, you're referencing the Dockerfile path incorrectly.

    A sanity check would be to run the docker build locally first:

    PROJECT_ID="..."
    _IMAGE_NAME="app1"
    _STAGE="dev"
    
    docker build \
    --tag=gcr.io/$PROJECT_ID/${_IMAGE_NAME}:${_STAGE} \
    --file=./Dockerfiles/${_IMAGE_NAME}/Dockerfile \
    .
    

    The following works for me:

    .
    ├── cloudbuild.yaml
    ├── Dockerfiles
    │   └── app1
    │       └── Dockerfile
    └── folder
        └── app1
            └── greeting.txt
    

    And:

    ./Dockerfiles/app1/Dockerfile:

    FROM busybox
    
    WORKDIR /tmp
    
    COPY ./folder/app1/greeting.txt ./greeting.txt
    
    ENTRYPOINT ["cat","/tmp/greeting.txt"]
    

    And:

    cloudbuild.yaml:

    steps:
      - name: docker
        args:
          - build
          - --tag=gcr.io/$PROJECT_ID/${_IMAGE_NAME}:${_STAGE}
          - --file=./Dockerfiles/${_IMAGE_NAME}/Dockerfile
          - .
    images:
      - gcr.io/$PROJECT_ID/${_IMAGE_NAME}
    

    And:

    PROJECT="..."
    QUESTION="64972175"
    gcloud builds submit . \
    --config=./cloudbuild.yaml \
    --substitutions=_IMAGE_NAME=app1,_STAGE="dev" \
    --project=${PROJECT}
    

    Yields:

    BUILD
    Pulling image: docker
    Using default tag: latest
    latest: Pulling from library/docker
    Digest: sha256:9170b902404a630a982a2a6473442d3e2cc2342b66345f7a9cf148f8affcf5d3
    Status: Downloaded newer image for docker:latest
    docker.io/library/docker:latest
    Sending build context to Docker daemon  6.144kB
    Step 1/4 : FROM busybox
    latest: Pulling from library/busybox
    Digest: sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
    Status: Downloaded newer image for busybox:latest
     ---> f0b02e9d092d
    Step 2/4 : WORKDIR /tmp
     ---> Running in 6ffe8d2b3f56
    Removing intermediate container 6ffe8d2b3f56
     ---> 51dcdc2e141a
    Step 3/4 : COPY ./folder/app1/greeting.txt ./greeting.txt
     ---> 7459813216f3
    Step 4/4 : ENTRYPOINT ["cat","/tmp/greeting.txt"]
     ---> Running in 25362a24391b
    Removing intermediate container 25362a24391b
     ---> 398b1f4a60e2
    Successfully built 398b1f4a60e2
    Successfully tagged gcr.io/[[PROJECT]]/app1:dev
    PUSH
    Pushing gcr.io/[[PROJECT]]/app1
    The push refers to repository [gcr.io/[[PROJECT]]/app1]
    8b5d68988e85: Preparing
    d2421964bad1: Preparing
    d2421964bad1: Layer already exists
    8b5d68988e85: Pushed
    dev: digest: sha256:d33bd458b8df930fc3338110f8369743ac74068b8a527c5f59958ee1ce1182ff size: 734
    DONE