16

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?

milenko
  • 163
  • 1
  • 1
  • 6
  • 1
    What did you investigate yourself. Did you read for example [this](https://torusware.com/blog/2015/04/optimizing-communications-between-html/) article? – Jeroen Heier May 17 '17 at 16:22
  • Why don't you try it yourself and report back on what the error is so we actually have something to help with? – Andy Shinn May 17 '17 at 17:03
  • Yes, I did read that article @JeroenHeier . I am going to re-read it and try again to see if there is something that I am missing. – milenko May 31 '17 at 17:40
  • I updated the original question to show what I tried so far, @AndyShinn – milenko May 31 '17 at 17:40

2 Answers2

13

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
Eldad Assis
  • 10,464
  • 11
  • 52
  • 78
Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
  • 2
    I guess the implementation just needs a shared /tmp. There's no need for `--ipc=host`. I wouldn't share the host's /tmp nor IPC for security reasons... – Ricardo Branco May 31 '17 at 19:30
  • 2
    Agreed @RicardoBranco. The following also works: `docker run -it --name c1 -v /tmp container1 docker run -it --name c2 --ipc=container:c1 --volumes-from c1 container2` – milenko May 31 '17 at 19:51
  • 4
    Then I guess there's no need for `--ipc=...` at all. – Ricardo Branco May 31 '17 at 20:20
  • That's true, I didn't need the --ipc-... at all. The /tmp mount is all that was needed... – milenko May 31 '17 at 21:29
  • It looks like the Docker [ipc setting](https://docs.docker.com/engine/reference/run/#ipc-settings-ipc) is for shared memory, not Unix sockets, which is what [ZMQ uses](http://api.zeromq.org/4-1:zmq-ipc). – orodbhen Oct 06 '17 at 18:56
  • If `--ipc=host` is not required and ZMQ only uses Unix sockets, then how does having only the shared `/tmp` work? – cogitoergosum Jan 20 '20 at 11:40
1

I have 3 containers, 2 containers share data to another container, what worked for me is:

# 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 can get pid of processes running in container2 and container3 and hence get data from the containers.