Skip to content

Instantly share code, notes, and snippets.

@osule
Last active August 23, 2017 10:28
Show Gist options
  • Save osule/8b9de6a0d275a4497122d4446e0e9e5f to your computer and use it in GitHub Desktop.
Save osule/8b9de6a0d275a4497122d4446e0e9e5f to your computer and use it in GitHub Desktop.
Docker formatting using Go templates

Do you grep?

Docker exposes formatting for most commands using Go templates so that it is possible to programmatically refine/format results of docker commands.

Lets explore some use cases.

Checking for containers created from a particular image.

docker ps --format '{{ if eq (js $.Image ) "<image_name>"}} \
{{ .ID }} {{ else }} --- {{ end }}'

Retrieving host ip.

(Peeps running docker without docker-machine or boot2docker will find this very helpful)

docker inspect --format '{{ json (index \
.NetworkSettings.Ports "8080/tcp" 0 "HostIp") }}' <container_id>

Get Container Ids of all processes

docker ps --format "{{ .ID }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment