IPC communication between Docker containers

2020-02-10 06:07发布

问题:

Is it possible for two separate Docker containers to communicate over a ZMQ IPC socket? If so, how can this be achieved?

For example:

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".

The following commands are used to run the applications in two separate docker containers:

// 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

After running the containers, I am not able to establish the ZMQ (IPC) connection. However, I am able to ping container 1 from container 2, and ping container 2 from container 1.

I also tried using the --ipc command, but it did not help:

// 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

UPDATE: I am able to communicate between two separate Docker containers using a ZMQ TCP socket, but am still unable to communicate using an IPC socket. Is it possible?

回答1:

Have you seen Shared Memory with Docker containers (docker version 1.4.1)? It sounds like you need to share the volume where the IPC lives and also set --ipc host. In your example, it would be something like:

# 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


标签: docker ipc