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

我只是一个虾纸丫 提交于 2019-11-30 08:19:24

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!