Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can docker port forward to a unix file socket on the host container?

Tags:

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?

like image 247
Ed Sullivan Avatar asked Jul 25 '14 12:07

Ed Sullivan


People also ask

Can Docker container access ports on host?

When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route. This would permit access to any ports on the host from Docker containers.

What is network port and Unix socket in Docker?

Unix Sockets use the local filesystem for communication, while IP Sockets use the network. The Docker daemon can listen for Docker Engine API requests via three different types of Socket: unix, tcp, and fd . By default, a unix domain socket (or IPC socket) is created at /var/run/docker.sock.

How does Docker map a port on a container to a port on the host?

We open a host port to give us access to a corresponding open port inside the Docker container. Then all the requests that are made to the host port can be redirected into the Docker container. Port mapping makes the processes inside the container available from the outside.

What is Docker Unix socket?

A socket is an endpoint in a network that passes data between software. Docker. sock is a Unix socket that enables the Docker server-side daemon, dockerd, to communicate with its command-line interface via a REST API. The socket appears as the /var/run/docker. sock file.


2 Answers

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

like image 171
icecrime Avatar answered Sep 21 '22 17:09

icecrime


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.

like image 45
Vasspilka Avatar answered Sep 22 '22 17:09

Vasspilka