Using netcat to pipe unix socket to tcp socket
Asked Answered
L

1

9

I am trying to expose a unix socket as a tcp socket using this command:

nc -lkv 44444 | nc -Uv /var/run/docker.sock

When I try to access localhost:44444/containers/json from a browser, it doesn't load anything but keeps the connection open (the loading thingy keeps spinning), but the console (because of the -v flag) shows proper http response.

Any ideas on how to get this working?

PS: I know I can use socat, or just tell docker to also listen on a tcp socket, but I am using the project atomic vm image, and it won't let me modify anything except /home.

Lonely answered 21/8, 2014 at 18:15 Comment(2)
your browser is expecting a proper http response, which is a header block followed by body content. until your stuff outputs at least one blank line, the browser will never see "body" content and just sit there waiting.Lookthrough
Pipes only go one way... the netcat on the left isn't going to get the output from the netcat on the right. The netcat on the left is reading from your original stdin (terminal?)Mehetabel
M
15

You are only redirecting incoming data, not outgoing data. try with:

mkfifo myfifo
nc -lkv 44444 <myfifo | nc -Uv /var/run/docker.sock >myfifo

See http://en.wikipedia.org/wiki/Netcat#Proxying

Edit: in a script you would want to generate the name for the fifo at random, and remove it after opening it:

FIFONAME=`mktemp -u`
mkfifo $FIFONAME
nc -lkv 44444 < $FIFONAME | nc -Uv /var/run/docker.sock > $FIFONAME &
rm $FIFONAME
fg
Metametabel answered 21/8, 2014 at 18:32 Comment(3)
Thanks! That works great. Also, I am stupid X-D. Just to add something small to your answer, I ran 2 commands: rm -f /tmp/myfifo; mkfifo /tmp/myfifo && nc -lkv 44444 </tmp/myfifo | nc -Uv /var/run/docker.sock >/tmp/myfifo .Lonely
If you are scripting this, use mktemp -u to generate a name which is guaranteed to be a non-existant fileMetametabel
nice, i use this to open my docker remote port 2375Heller

© 2022 - 2024 — McMap. All rights reserved.