Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Last active January 25, 2024 04:37
Show Gist options
  • Star 80 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save nuxlli/7553996 to your computer and use it in GitHub Desktop.
Save nuxlli/7553996 to your computer and use it in GitHub Desktop.
Examples of http request in unix domain socket with shell, using socat, netcat or curl
# References
# Socat (tested: Linux and macOS)
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | socat unix-connect:/var/run/docker.sock STDIO
# netcat (Linux) (netcat-freebsd or netcat-openbsd for Debian)
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | nc -U /var/run/docker.sock
# netcat (macOS) (gnu netcat from homebrew is not supported)
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | /usr/bin/nc -U /var/run/docker.sock
# curl version
curl --silent --unix-socket /var/run/docker.sock http://v1.18/containers/json
# bonus round: relay socket (unix sock -> tcp)
socat -d -d TCP-L:8080,fork UNIX:/var/run/docker.sock
# (tcp -> unix sock)
socat -d -d UNIX-L:/var/run/httpbin.sock,fork
Copy link

echo -e "GET /services HTTP/1.0\r\n" | sudo nc -U /var/run/docker.sock | tail -n 1 | jq

Copy link

pauldraper commented Sep 3, 2017

(1) You need an extra \r\n to indicate end of headers.
(2) HTTP RFCs don't consider half-closed connections, and many server implementations don't support them. So you may need to keep the socket open.

printf 'GET /images/json HTTP/1.0\r\n\r\n' | nc -q 2 -U /var/run/docker.sock

Copy link

rija commented Aug 11, 2018

To get nc -U /var/run/docker.sock working on Debian Jessie, you need to install the netcat-openbsd package because the default netcat package automatically install netcat-traditional that doesnt support Unix Socket.

Copy link

Zibri commented Dec 12, 2018

I get
HTTP/1.1 400 Bad Request: missing required Host header
Content-Type: text/plain; charset=utf-8
Connection: close

but printf 'GET /images/json HTTP/1.0\r\n\r\n' | nc -q 2 -U /var/run/docker.sock

HTTP/1.0 200 OK
Api-Version: 1.37
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/18.03.1-ce (linux)
Date: Wed, 12 Dec 2018 08:43:45 GMT
Content-Length: 3

Copy link

stokito commented Aug 13, 2023

FYI: you can also use curl like: curl --silent --unix-socket /var/run/docker.sock http://v1.41/containers/json.

If you have a payload you can read it first from stdin:

body=$(cat -)
curl --silent --unix-socket /var/run/docker.sock 
 -X POST http://v1.41/containers/create
   -H 'Content-Type: application/json'
   -d "$body" 

But on the OpenWrt prior to Mar 27 2023 the curl is compiled without UNIX sockets. To check it use curl -V.
On Ubuntu it will have Features: UnixSockets but on the OpenWrt you won't see it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment