Docker Scripts

David (the other author on this blog) and I have been doing a lot of work with docker with respect to our daily jobs. We ended up needed to orchestrate the management of multiple docker containers. This was before we discovered projects like maestro and maetro-ng. David found an awesome command line for parsing json called jq and put it to use by creating a bash one liner to grab a container’s IP address.

docker-get-ip
1
2
# Usage: docker-get-ip (name or sha)
[ -n "$1" ] && docker inspect $1 | jq -r '.[0] | .NetworkSettings | .IPAddress'

I took it one step further and created several more one liners to get other various pieces of information from containers.

docker-get-id
1
2
# Usage: docker-get-id (friendly-name)
[ -n "$1" ] && docker inspect $1 | jq -r '.[0] | .ID'
docker-get-port
1
2
# Usage: docker-get-port (name|sha) port/protocol
[ -n "$1" ] && docker inspect $1 | jq -r ".[0] | .NetworkSettings | .Ports | .[\"$2\"] | .[0] | .HostPort"

The great news is that with jq, you can create simple one liners for just about anything with the docker inspect command and drop them in /usr/local/bin and make your administrative life a little easier. I’m sure there are more useful ones, but those are the top 3 that David and I use that make things more simple.

Comments