Docker 个容器之间的 IPC 通信

IPC communication between Docker containers

两个单独的 Docker 容器是否可以通过 ZMQ IPC 套接字进行通信?如果可以,如何实现?

例如:

Docker Container #1 executes an application that creates a ZMQ Response socket and binds to "ipc://tmp/service_name".

Docker Container #2 executes an application that creates a ZMQ Request socket and connects to "ipc://tmp/service_name".

以下命令用于 运行 两个单独的 docker 容器中的应用程序:

// Run container #1 (binds to "ipc://tmp/service_name")
docker run --name c1 -it container1

// Run container #2 (connects to "ipc://tmp/service_name")
docker run -it --link c1:container1 --name c2 container2

运行连接容器后,我无法建立 ZMQ (IPC) 连接。但是,我能够从容器 2 ping 容器 1,并从容器 1 ping 容器 2。

我也尝试使用 --ipc 命令,但没有帮助:

// Run container #1 (binds to "ipc://tmp/service_name")
docker run --name c1 --ipc=host -it container1

// Run container #2 (connects to "ipc://tmp/service_name")
docker run -it --link c1:container1 --ipc=container:c1 --name c2 container2

更新: 我可以使用 ZMQ TCP 套接字在两个单独的 Docker 容器之间进行通信,但仍然无法使用 IPC 套接字进行通信。可能吗?

你见过吗?听起来你需要共享IPC所在的卷并设置--ipc host。在您的示例中,它将类似于:

# Container #1
docker run -it --name c1 -v /tmp:/tmp --ipc=host container1

# Container #2
docker run -it --name c2 -v /tmp:/tmp --ipc=host container2

我有 3 个容器,2 个容器向另一个容器共享数据,对我有用的是:

# container 1
docker run -it --ipc=shareable -v ///tmp --network=host --name node container1

# container 2
docker run -it --ipc=container:node --volumes-from node --network=host --name mdns container2

# container 3
docker run -it --ipc=container:node --volumes-from node --network=host --name connection container3

container1可以在container2container3中得到pid个进程运行从而得到数据来自容器。