1

I'am trying to connect web- application, deployed from one container, to rabbitmq, deployed at another container.

By relying on this Connecting to RabbitMQ container with docker-compose , I created the following docker-compose:

version: '3'

services:
 webapp:
    container_name: chat
    build:
      context: .
    depends_on:
      - broker
    ports:
      - "8080:8080"

broker:
   container_name: rabbit_chat
   image: rabbitmq
   command: rabbitmq-server
   expose:
     - 5672
     - 15672
   healthcheck:
     test: ["CMD", "curl", "-f", "http://broker:5672"]
     interval: 30s
     timeout: 10s
     retries: 5

webapp is a service of web-application, at which I prescribe following rabbitmq-properties:

host = broker
port = 15672
login = guest
password = guest

I selected 'broker' as a host, as in documentation recommended to connect containers by default-net, using the name of service in docker-compose.

And this does not work.Also I tried to use "localhost" as host for connection.

Additionally, inspecting container "rabbit_chat" I see in output:

  "Log": [
            {
                "Start": "2020-04-11T14:54:25.0988242Z",
                "End": "2020-04-11T14:54:25.2920557Z",
                "ExitCode": -1,
                "Output": "OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused \"exec: \\\"curl\\\": executable file not found in $PATH\": unknown"
            }

2 Answers 2

0

Try a network:

services:
  webapp:
    ...
    networks:
    - mynetwork

 broker:
   ...
    networks:
    - mynetwork

networks:
  mynetwork:

This also sets up internal DNS, so you actually can use broker as a hostname in webapp to refer to the other container.

4
  • Thank you! I tried. But it still does not work. Also I tried to connect them by bridge-network. In this case I used IP of broker-container as host for requests. And it does not work to. When I connect my app with rabbitMQ on host (without containers) it works properly... So, I confused...
    – Jelly
    Apr 12, 2020 at 8:18
  • So debug it: docker exec into your webapp container, and see if you can reach the broker container (e.g. with curl). I cannot do remote debugging for you ... and my crystall ball says it's some stupid mistake somwhere in some configuration file.
    – dirkt
    Apr 12, 2020 at 10:21
  • I entered I my containers. Inside rabbit_chat-container I send request (webapp:8080) successfully. But when I send "curl broker:5672 --output - " inside chat-container, I received: "curl: (56) Recv failure: Connection reset by peer". I suppose, the problem in access-rights. I inspected content of etc-folder of rabbit_chat-container and found access.conf. And content of this file completely commented. Should I "switch on" some configurations to provide an access?
    – Jelly
    Apr 12, 2020 at 11:17
  • Inside rabbit_chat-container I run "ncat -v --listen 5672". And receive "Ncat: bind to :::5672: Address already in use. QUITTING."
    – Jelly
    Apr 12, 2020 at 12:08
0

From your comments it looks like access authorizations issues, rather then docker port problems.
First, try to curl from inside the rabbit-mq container, From the log it looks like you don't have curl installed on the rabbit container, so install it and try.
Then try from outside (from your machine):
Just map the ports in the rabbit container: "5672:5672" and "15672:15672".
curl localhost:5672 (or do docker inspect and curl directly to the container ip-address).

Are the images built from the same base image? maybe you have ufw in the rabbit image.
Also try to shutdown the ufw on your machine and check (if you have it)

The best way to communicate among containers in docker-compose is via the network bridge, so eventually you'll have to use it.

2
  • amir, thank you. I tried to connect to container: from host-machine by. curl localhost:5672. It returns "AMQP." Ip of my container in net is 172.26.0.3. I tried from host: curl 172.26.0.3:5672. It returns "Failed to connect to 172.26.0.3 port 5672: Operation timed out". When I run above curl-requests inside container it returns it both cases: "curl: (56) Recv failure: Connection reset by peer AMQP". My host and containers dont have any ufw.
    – Jelly
    Apr 14, 2020 at 18:24
  • Andrew, I've noticed that the "rabbitmq" image doesn't reply to port 15672 although it exposes it, try to use image "rabbitmq:3.6-management-alpine" which has a web portal and you can browse to localhost:15672 or curl -u guest:guest http://[your-container-ip]:15672/api/vhosts , maybe then you'll be able to reach it from your web-app Apr 16, 2020 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.