Gcloud SDK with Docker — a quick primer for developers

Kristan 'Krispy' Uccello
3 min readNov 27, 2019
Photo by Mitchell Luo on Unsplash

Docker is an excellent tool for packaging and shipping software. I won’t spend time championing the virtues of containerization as it has been covered by many others who are better wordsmiths than me.

This article will show you how to setup your OSX Catalina host (or any other *nix type system) machine to take advantage of Docker when using GCloud SDK. You should be able to adapt this approach for many other similar tools and utilities with minor modification. For context, I went down this path because my personal development machine had a lot of issues running GCloud SDK out of the box. With the upgrade to Catalina I quickly discovered that brew, pyenv and GCloud SDK don’t play nicely together. As well, I get the feeling that Apple is really trying to make developers suffer with open source — just a theory (see SIP).

Just the facts

You will need to have docker installed on your system for this to work. Also, I use ZSH as my shell but this will work with bash and likely other shells.

docker pull gcr.io/google.com/cloudsdktool/cloud-sdk:latest

With this one command you now have the docker image you need. Do not try to run the gcloud components update when you want to upgrade. Just re-pull the latest image. No mess no fuss. Also, the gcloud components update will fail.

Ok, now we have the image lets login:

docker run -ti gcr.io/google.com/cloudsdktool/cloud-sdk gcloud auth login

Follow the provided url and then paste the code you get back into the terminal prompt. You only need to do this once. Now you can get a list of your projects:

docker run --rm -ti --volumes-from gcloud-config google/cloud-sdk gcloud projects list

Great! Now you know that it is working. However, there are a lot of characters in these commands. Let’s clean that up and make our host environment feel more native while still using the container.

Dockerizing like a hacker

Download the gcloud sdk to your host from https://cloud.google.com/sdk/docs/

“Wait, what? I thought the docker image was what we were using?!” — It is, but we also want cli auto complete. More to come right below.

Unzip the file somewhere on your host system and then edit your ~/.zshrc or if you use bash then ~/.bashrc

...export MY_GCLOUD_PATH=<THE/PATH/YOU/UNZIPPED/TO>/google-cloud-sdk# The next line updates PATH for the Google Cloud SDK.if [ -f '$MY_GCLOUD_PATH/path.zsh.inc' ]; then . 'MY_GCLOUD_PATH/path.zsh.inc'; fi# The next line enables shell command completion for gcloud.if [ -f '$MY_GCLOUD_PATH/completion.zsh.inc' ]; then . 'MY_GCLOUD_PATH/completion.zsh.inc'; fi# The next line lets us simulate gcloud as if it were running as a binary on the hostalias gcloud='docker run --rm -ti -v $(pwd):/workspace -w /workspace --volumes-from gcloud-config google/cloud-sdk gcloud'...

This setup gives us two important things. The first thing we get is the shell completion for gcloud. Which is why we also have the files on our host I′m pretty sure I can figure out how to pull these in or alias in someway from the docker image but have not tried yet−please comment if you end up sorting that out.

The second thing is we get auto mounting of the current working directory when invoking gcloud — this is essential for my use case where I want to experiment with knative and deploy with gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld

I feel this approach is kind of useful for situations where your host system or rig is not playing nice with new tools. Give it a whirl and let me know what you thought of this short primer.

--

--