I recently found myself in a situation where I wanted to query the Docker socket to query their API for information, and I found myself wishing with every passing moment that I could just query a unix socket using a tool as simple and straight-forward as curl. Imagine my joy when I discovered that curl now has unix socket support in curl 7.40 and later. Discovering this new feature made my day, and I’m thrilled to share this with you.
Let’s take a look at how this works by using curl’s unix socket support to find out what images our local Docker daemon has available.
That’s pretty easy, huh? Simply use the –unix-socket flag and make sure to throw http: before your endpoint (omitting the protocol in 7.46 DEV will result in a malformed url error). You can skip specifying the HTTP method (-X) if you’re okay with curl’s default of using the HTTP GET method.
How much effort does using curl save? Here’s an equivalent example using OpenBSD’s netcat (also available on Ubuntu Server 15.04):
How useful is using curl over netcat? Using curl can potentially provide benefits in scripting and ease of use, but anyone who already has the muscle memory to hammer out something familiar to the netcat line above might not be as easily persuaded by the convenience. curl 7.40 may also not be available in some software repositories, so knowledge of using netcat still may be useful if a newer version of curl isn’t available.
You’ll be ready to work with a unix socket next time you find yourself in a similar position. Go forth and curl!