ZeroMQ failing to communicate between two Docker containers
Your primary problem is that you have configured your server with the address tcp://127.0.0.1:5557
. Because it's bound to localhost (127.0.0.1
), that socket isn't going to be visible to anything outside of that container. So the first thing you need to fix is the server bind address. Consider:
address = "tcp://0.0.0.0:5557"
A second problem is that you're using the name serverd_dev_1
in the client, but it's not clear this would actually be the name of your serverd container(that would depend on the directory names in use when you run docker-compose up
).
Naming is easier to manage with a single docker-compose.yaml file. For example, I set things up like this:
version: "2"services: serverd: build: serverd command: ["python", "-u", "./serverd.py"] environment: SERVER_LISTEN_URI: tcp://0.0.0.0:5557 clientd: build: clientd command: ["python", "-u", "./clientd.py"] environment: SERVER_CONNECT_URI: tcp://serverd:5557
This will launch both containers in a dedicated network (because this is what docker-compose does by default), so you don't need to explicitly create or reference mynet
.
As you can probably infer from the above, I modified your code to get the ZMQ uri from an environment variable, because this made it easier to experiment. You can find the above docker-compose.yaml and the modified code at:
Update
In case you really want/need to have two separate docker-compose.yaml
files, I've updated the example to include per-service files. These examples use the alias
option to provide a name by which the client can contact the server, regardless of your local directory layout:
version: "2"services: serverd: build: . command: ["python", "-u", "./serverd.py"] environment: SERVER_LISTEN_URI: tcp://0.0.0.0:5557 networks: mynet: aliases: - serverdnetworks: mynet: external: True
This configuration requires that you create mynet
before bringing up the containers.