A few helpful CLI snippets when developing with Docker, Git, & Google Cloud Platform

Preface
I’m mainly jotting these down for myself, but also to share with the community. I‘ll organize, document, and move them to a Github repo over time. Would love to collect any you use also, leave a comment and I’ll add them in.

These are random CLI snippets I’ve used while developing dockerized apps with Node.js, deployed on Kubernetes, typically on Google Cloud Platform. You can assume they are all single line CLI commands.

Some of the more frequent commands I’ll usually add an alias (CLI shortcut) to in my .zshrc (or .bashrc) file.

For example, to add an alias for a common Kubernetes command for checking on your pods:

# original CLI command:
$ kubectl get pods
# shorter alias
$ kp

To create this, open ~/.zshrc and add the below line

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias kp="kubectl get pods"

Then you only need to type kp to check your pods.


Get the current github project name from the current folder

export DEPLOYMENT=$(basename -s .git `git config --get remote.origin.url`)

Get the current Google Cloud project

export PROJECT_ID=$(gcloud config get-value project)

Build a docker image in the current git enabled folder tagged withe project-id, git repo name and current git HEAD

export IMAGE_NAME=gcr.io/$(gcloud config get-value project)/$DEPLOYMENT:$(git rev-parse HEAD)

Build a Docker container from the above custom image name you just made

docker build -t $IMAGE_NAME .

Run a Docker container from the custom image name, automatically named from current folder github repo name

export DPORT=5000 && docker run --rm -p $DPORT:$DPORT --name $(basename -s .git `git config --get remote.origin.url`) $IMAGE_NAME

or if you have the deployment env set:

export DPORT=5000 && docker run --rm -p $DPORT:$DPORT --name $DEPLOYMENT $IMAGE_NAME

Go one further and open the browser automatically to http://localhost:5000 (or the port you set). Refresh browser after docker starts if it doesn’t automatically.

export DPORT=5000 && open http://localhost:$DPORT && docker run --rm -p $DPORT:$DPORT --name $(basename -s .git `git config --get remote.origin.url`) $IMAGE_NAME

Push a new Docker image to Google Cloud container registry

gcloud docker -- push $IMAGE_NAME

Update a running deployment with a new version of a container

kubectl set image deployment/$DEPLOYEMENT $DEPLOYMENT=$IMAGE_NAME

See a cleaned up summary of the current allocated resources for nodes

kubectl describe nodes | grep -A 4 "Allocated resources"

Watch logs from a certain pod. If the pod has multiple containers within, you’ll need to add the container name at the end.

kubectl logs -f pod-name-here

Use Captain to easily start/stop docker containers. Provides a menu of recent containers in the taskbar on MacOS.(I’m not affiliated or sponsored)


Delete all node_modules directories recursively from your current path. This one is good when you need to backup your projects, or move to a new laptop etc. Backing up thousands of small files over usb takes forever. This won’t search your entire computer, only below the current path, so run it from a single project root to only delete that project’s node_modules, or up higher to do all of your projects.

find . -name “node_modules” -type d -prune -exec rm -rf ‘{}’ +

Delete all those annoying .DS_Store files on MacOS. This will search your entire hard drive, not only the current path. It won’t stop them from being recreated over time.

sudo find / -name ".DS_Store" -depth -exec rm {} \;

This is just a random Yarn (npm) tip for graphically checking for package updates in your app’s package.json. Add the --latest flag to force checking for the newest major versions.

Side tip (or bug?) — It won’t list beta/@next versions when you run this command, but if you have a beta version of a package currently installed, and there is an update to that beta version, it will pop up in the list, but it will offer to upgrade to the latest major (non beta) version. If you see this, don’t update from there, but you’ll know a new beta version is out. Check the tip below about npm dist-tags to see how you can check the latest beta version.
yarn upgrade-interactive --latest

This one is helpful (npm/Node.js) when checking for all of a libraries release tags, including beta etc. Since the yarn upgrade-interactive command won’t show beta tags. Swap <package name> for the actual name, example below:

npm dist-tags ls <package name> 

To check the release tags for typescript

npm dist-tags ls typescript
# output:
beta: 2.0.0
insiders: 2.9.1-insiders.20180525
latest: 2.9.1
next: 3.0.0-dev.20180608
rc: 2.9.0-rc

You can also use the below yarn info command, but the output is slightly ridiculous compared to npm dist-tags ls

# for version specific info:
yarn info typescript versions
# or all package info:
yarn info typescript

Easily deploy new versions of an app on Kubernetes/GCP. Too long for this post, see this article.


Please post any additional in the comments!