119

I have a HTTP server running at /var/run/my-server.sock, and I want to test it by sending a simple request using cURL. Can this be done using cURL? Can it be done at all, or must there be a reverse proxy in place?

I'm imagining something like this:

curl socket:/var/run/my-server.sock:/test/path
tshepang
  • 3,311
  • 4
  • 22
  • 30
Hubro
  • 5,716
  • 14
  • 61
  • 87

2 Answers2

173

The feature was added in curl 7.40.

curl --unix-socket /var/run/docker.sock http:/images/json

Another example:

curl --no-buffer -XGET --unix-socket /docker.sock http:/events

Which specifies the GET explicitly (rather than assumed). And will not buffer (for tail -f realtime update).

(The first Ubuntu release to ship with curl 7.40 or newer was 15.10).

cURL 7.50 and up requires a valid URL to be provided, including a hostname, so to run the above examples with cURL 7.50, a "dummy" hostname has to be added, for example:

curl --unix-socket /var/run/docker.sock http://localhost/images/json

and

curl --no-buffer -XGET --unix-socket /docker.sock http://localhost/events
Dreamcat4
  • 1,889
  • 1
  • 11
  • 5
  • 1
    And in this example I see the very socket I was going to `curl` into after all. Long live Docker! – spacediver Aug 12 '15 at 20:09
  • By the way, the netcat and socat examples are no longer *above* :] – Hubro Aug 25 '15 at 15:20
  • This is really useful. However, I'm trying to do this `curl --no-buffer -XGET --unix-socket tcp://192.168.99.102:2376 http://events` but curl cannot connect to the server. I tried using the ruby docker library and it connects fine using this – user1513388 Mar 09 '16 at 10:29
  • 1
    @user1513388 I'm pretty sure you have to provide the path to a socket as the argument to `--unix-socket`, not a URI. – Hubro Mar 29 '16 at 11:21
  • For ubuntu 14.04 users, there seems to be a PPA that has backports of curl 7.4x - https://launchpad.net/~mnordhoff/+archive/ubuntu/curl-backport – Ganesh Hegde Nov 10 '16 at 06:35
  • 1
    If the socket file is owned by `root`, these return `Couldn't connect to server` until you run `sudo curl`. – OneCricketeer Jul 18 '18 at 02:29
  • 1
    On CentOS 7 this was backported even to the 7.29 `curl` version. – Ivan Shatsky Jun 15 '22 at 15:51
13

Not sure, but as per this ticket:

it doesn't seem to be the case.

Per this:

seems like socat or nc can do it, snip from the above snip:

# Socat version
echo -e "GET /images/json HTTP/1.1\r\n" | socat unix-connect:/var/run/docker.sock STDIO

# nc version (netcat-freebsd)
echo -e "GET /images/json HTTP/1.0\r\n" | nc -U /var/run/docker.sock

Haven't tried either myself.

steady rain
  • 578
  • 3
  • 8
  • 6
    it works with the exception that you can to provide `\r\n\r\n` at the end, not `\r\n` `echo -e "GET / HTTP/1.1\r\n\r\n"` – Dmitry Ishkov Feb 08 '17 at 13:27