How to share unix domain socket between containers without named volume

containerssocketunix-sockets

I have two containers A and B which needs to talk via unix domain socket created by A in /var/run/notif.sock.

I can't use named volume as sharing /var/run between containers is risky as it contains container's runtime data.

Bind mount requires the host to already have a file with same name.

Problem with bind mount is if I create a host file with same name as the socket file, and use bind mount like this:
–v /var/run/notif.sock : /var/run/notif.sock , it would create a plain file /var/run/notif.sock automatically by docker inside container A and when the actual process inside container A tries to open the socket, socket file creation would fail inside the container with error "address already in use" as a file with same name is already present.

Is there any other way to go about this?

Best Answer

If you can configure the directory of the socket file, you could share only that directory (e.g. /var/run/share), avoiding to share /var/run.

if you cannot change the socket directory, you could try using a different socket file in A in the share directory. That new socket would be the one to which B would write (I assume from your post that A is listening to the socket and B writting), and prepare a program in A that listens to the new file and writes to the original file. The following is only a test made with socat, you should consider managing exceptions, etc.:

socat UNIX-LISTEN:/var/run/share/notif.sock - | socat UNIX-CONNECT:/var/run/notif.sock -

Everything that is read from /var/run/share/notif.sock, is written in /var/run/notif.sock. Of course if you cannot change the directory in B, you could make use of a similar solution