31

Running the following command fails:

sudo docker run -p unix:///tmp/file.sock:44444 -d image_name

Is something wrong with my port forwarding syntax or is a configuration like this not possible?

Ed Sullivan
  • 728
  • 1
  • 9
  • 23
  • You may be able to put the UNIX socket in a volume which the host can access / mount to another container. I'd recommend you simply listen on a port rather than a UNIX socket. – Marcus Hughes Jul 25 '14 at 13:24
  • 11
    The idea behind it was to increase speed, because with unix sockets I would be able to avoid the network overhead that is inherent in ports – Ed Sullivan Jul 25 '14 at 15:58

2 Answers2

31

Docker's -p syntax won't take a unix socket:

-p=[]      : Publish a container᾿s port to the host (format:
             ip:hostPort:containerPort | ip::containerPort |
             hostPort:containerPort)

One solution would be to:

  • Run your container without any -p specification, we'll name it "cont1" (--name cont1)
  • Run a second container which:
    • Bind mounts the unix socket (-v /tmp/file.sock:/tmp/file.sock) to have it accessible from within the container
    • Links to the first container (--link cont1:cont1) to be able to connect to it
    • Runs a tool such as socat to route traffic from the unix socket to the "cont1:4444" endpoint

I'm not a socat expert, but the address specification you'll need should look like this: UNIX-LISTEN:/tmp/file.sock,fork,reuseaddr TCP4:cont1:4444

icecrime
  • 74,451
  • 13
  • 99
  • 111
  • What if the unix socket on the host is not mapped on a file, that is an abstract namespace socket, such as (as reported by lsof): ```aesm_serv 26473 aesmd 6u unix 0xffff88021c494b00 0t0 291424 @sgx_aesm_socket_base type=STREAM``` – Valerio Schiavoni Jun 14 '16 at 17:56
  • Someone was nice enough to build a container that does the above: https://hub.docker.com/r/alpine/socat/ – Byebye Jun 11 '18 at 09:50
10

The accepted answer is partially correct however since you can only link directories, which means you need to link the directory of the socket instead of the socket itself.

The following did it for me when I wanted to connect a postgres socket.

docker run -p 5432:5432 -v /run/postgresql:/run/postgresql -d --name postgres postgres

What this does is link the postgres socket file to your host system.

Vasspilka
  • 1,135
  • 1
  • 10
  • 22
  • 2
    Still not supported for mac? https://github.com/docker/for-mac/issues/483 – holmberd Dec 07 '17 at 23:00
  • I have use the -v option to link non-directory files (regular files). However because the server creates the socket, you need to bind a directory. (check that the server has permission to write to this directory. – ctrl-alt-delor Jan 07 '19 at 20:12