Time-saving tricks for Kubernetes and Docker commands

Ingkwan
Ingkwan’s Knowledge Hub
2 min readJun 16, 2020

--

I have been working with Kubernetes for a while then I found that I keep entering the same commands several times and often use them in combination. Therefore I have made some useful commands which can save my time a lot during development work.

(With right arrow key)
(With up arrow key)

Besides, I also use Oh My Zsh with zsh-autosuggestions plugin to make it even easier.

This plugin will help you to complete a command automatically from the command history once you hit right arrow key, or use up arrow key to quickly access previously entered commands that begin with words you just typed.

You can find more details from the links provided above.

So below are a few commands I would like to share which may be convenient for people who use them frequently. I will keep updating this blog as I find new ones.

# Remove exited containers
docker rm $(docker ps -a -f status=exited -q)
# Remove all containers
docker rm $(docker ps -a -q)
# Stop and remove all containers
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)
# Remove a specific image
docker rmi $(docker images -q {{your-image-name}})
# Remove all dangling images
docker rmi $(docker images -f "dangling=true" -q)
# Tail pod logs with partial name (if you are using Job)
e.g. your-app-name-2jfda80la-asf1c
kubectl logs -f $(kubectl get po | grep {{your-app-name}} | tr -s ' ' | cut -d\ -f 1)
# Tail pod logs from deployment name
kubectl logs deployment/{{deployment-name}}

--

--