Reputation: 3031
I'm using docker-compose to run 2 containers, 1 for running a react application and another to run a rabbitmq server.
version: '2'
services:
node.app.local:
build:
context: ./node
container_name: app-node
hostname: node.app
domainname: local
tty: true
volumes:
- "${SOURCES_PATH}/app:/var/www/app"
ports:
- 8091:8091
expose:
- 8091
links:
- rabbitmq.app.local
working_dir: "/var/www/app"
rabbitmq.app.local:
image: rabbitmq:3.6.10
container_name: app-rabbitmq
hostname: rabbitmq.app
domainname: local
tty: true
expose:
- 5672
- 15672
- 15674
I know that the link between my node container and my rabbitmq server is working well as I can ping the server from my node container :
$ docker exec -it app-node bash
$ ping rabbitmq.app.local
64 bytes from 172.18.0.12: icmp_seq=0 ttl=64 time=0.099 ms
64 bytes from 172.18.0.12: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 172.18.0.12: icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from 172.18.0.12: icmp_seq=3 ttl=64 time=0.088 ms
In my js application, if I open a connection with the IP of the server (172.18.0.12 as exposed by the ping), it works well :
const ws = new WebSocket('ws://172.18.0.12:15674/ws');
//connected to server RabbitMQ/3.6.10
But if I try with the container name :
const ws = new WebSocket('ws://rabbitmq.app.local:15674/ws');
VM7698:35 WebSocket connection to 'ws://rabbitmq.app.local:15674/ws' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED
Does anyone knows why I can ping the server in CLI but can't access it via the ws protocol ?
Upvotes: 4
Views: 5240
Reputation: 31
I ran into the same problem when using PHP in one container connecting to a websocket service in another container.
I did some research, and found that docker compose v2 uses user-defined networks, which in terms introduced an embedded DNS server, replacing the traditional DNS inside a container in the default bridge network.
The embedded DNS server maintains the mapping between all of the container aliases and its IP address on a specific user-defined network.
That how it works, according to docker official document. But it seems that there are some problems when trying to DNS-resolve a container alias or link, said by jake-low, a contributor of docker-compose.
So, an alternative solution to this problem is to get the IP by hostname first, and then use the resolved IP to connect to the websocket service. This is how I solve the problem.
Upvotes: 3