Search code examples
dockermakefilegoogle-cloud-builddocker-in-docker

With Google Cloud Build, how do I run a Makefile that executes docker and kubectl commands?


I received a project that is built with a number of Makefiles which run a number of docker commands. I want to build it on Cloud Build.

I know that Cloud Build handles Docker natively, but must work with the existing Makefile-structure.

I tried the make Custom Build step, but Cloud Build failed on lack of docker and kubectl. So I could add these to the Dockerfile of the make Custom Build step, but it seems like that is wrong, because then make runs Docker-in-Docker and this causes problems -- for example, gcloud and kubectl permissions and context are missing.

As a small part of one such a Makefile, here is a target that calls docker build and docker push.

build/myapp/deployer: ...

    docker build \
        --build-arg REGISTRY="$(REGISTRY)/myapp" \
        --tag "$(APP_DEPLOYER_IMAGE)" \
        -f deployer/Dockerfile \
        .

    docker push "$(APP_DEPLOYER_IMAGE)"
    @touch "$@"

How do I run these Makefiles in Cloud Build?


Solution

  • To answer to your comment: "How to run docker inside a docker container?", you have to see your custom cloud builder like an runner. You can use it like this in Cloud Build

    - steps:
      - name: gcr.io/your-project/your-custom-builder
        entrypoint: "bash"
        args: 
          - "-c"
          - |
            Makefile
            <All the script lines that you want which use binaries installed in your custom builder>
    

    Here the custom Cloud Builder run the command but the result is stored in your Cloud Build workspace, not in the custom Cloud Builder container.