I'm building a go app which sends a message to a RabbitMQ queue when accessing an endpoint. The problem is that, when I'm accessing this endpoint, I receive this error and the application closes.
2021/04/12 21:30:06 Failed to connect to RabbitMQ: dial tcp: lookup rabbitmq on 127.0.0.11:53: read udp 127.0.0.1:48796->127.0.0.11:53: i/o timeout
This is how I'm connecting:
conn, err := amqp.Dial("amqp://guest:guest@rabbitmq:5672")
This is my docker-compose.yml file:
services:
postgres:
build:
context: .
dockerfile: Dockerfile.postgres
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_DB: "postgres"
ports:
- "5432:5432"
rabbitmq:
image: rabbitmq
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
plant-service:
build: .
ports:
- 8080:8080
depends_on:
- postgres
- rabbitmq
environment:
RABBITMQ_SERVER: rabbitmq
networks:
rabbitmq_go_net:
driver: bridge
It's possible to send messages from another non dockerized application I have, which is connected this way:
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
Any suggestion is welcome. Thank you
Edit
The problem was that rabbitmq was on a different network, here is the fixed docker-compose.yml
services:
postgres:
build:
context: .
dockerfile: Dockerfile.postgres
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_DB: "postgres"
ports:
- "5432:5432"
rabbitmq:
image: rabbitmq
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
plant-service:
build: .
ports:
- "8080:8080"
depends_on:
- postgres
- rabbitmq
environment:
RABBITMQ_SERVER: rabbitmq