quickshiftin
quickshiftin

Reputation: 69571

Google cloud build - provide assets to docker build step

I'm trying to combine two examples from the Google Cloud Build documentation

This example where a container is built using a yaml file

And this example where a persistent volume is used

My scenario is simple, the first step in my build produces some assets I'd like to bundle into a Docker image built in a subsequent step. However, since the COPY command is relative to the build directory of the image, I'm unable to determine how to reference the assets from the first step inside the Dockerfile used for the second step.

Upvotes: 2

Views: 449

Answers (1)

quickshiftin
quickshiftin

Reputation: 69571

There are potentially multiple ways to solve this problem, but the easiest way I've found is to use the docker cloud builder and run an sh script (since Bash is not included in the docker image).

In this example, we build on the examples from the question, producing an asset file in the first build step, then using it inside the Dockerfile of the second step.

cloudbuild.yaml

steps:
- name: 'ubuntu'
  volumes:
  - name: 'vol1'
    path: '/persistent_volume'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
        echo "Hello, world!" > /persistent_volume/file
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'sh'
  volumes:
  - name: 'vol1'
    path: '/persistent_volume'
  args: [ 'docker-build.sh' ]
  env:
    - 'PROJECT=$PROJECT_ID'
images:
- 'gcr.io/$PROJECT_ID/quickstart-image'

docker-build.sh

#/bin/sh
cp -R /persistent_volume .
docker build -t gcr.io/$PROJECT/quickstart-image .

Upvotes: 1

Related Questions