Search code examples
dockergoogle-cloud-platformgcloudgoogle-container-registry

How to stop gcloud docker -a overwriting long-lived credentials?


We are using the Google Container Registry to store our Docker images.

To authorize our build instances we place long-lived access tokens in .docker/config.json as described in the docs.

This works perfectly fine until someone (i.e. some Makefile) uses gcloud docker -- push ... to push to the registry (instead of e.g. docker push ...). gcloud will replace the existing, long-lived credentials with short-lived ones that expire after some time. Thus subsequent builds may fail, depending on the exact timing.

My Question: How can I prevent gcloud docker ... from messing with my provisioned credentials?

I've tried chattr +i .docker/config.json, but this just makes gcloud complain.


Solution

  • From https://cloud.google.com/sdk/gcloud/reference/docker:

    The gcloud docker command group wraps docker commands, so that gcloud can inject the appropriate fresh authentication token into requests that interact with the docker registry.

    The only thing that gcloud docker does is change these credentials, then invoke the docker CLI. If you don't want it to change the credentials, there's no reason not to just call docker directly.

    One workaround might be to use an alternate configuration file location for your long-lived credentials; per https://docs.docker.com/engine/reference/commandline/cli/:

    Options:
          --config string      Location of client config files (default "/root/.docker")