0

My service is using RabbitMQ. To work with it, I installed this package https://github.com/vyuldashev/laravel-queue-rabbitmq . To work with this package, need to configure the config file

'hosts' => [
       [
           'host' => env('RABBITMQ_HOST', '127.0.0.1'),
           'port' => env('RABBITMQ_PORT', 5672),
           'user' => env('RABBITMQ_USER', 'guest'),
           'password' => env('RABBITMQ_PASSWORD', 'guest'),
           'vhost' => env('RABBITMQ_VHOST', '/'),
       ],
   ],

As you can see, the ip is specified as 127.0.0.1, but my docker container has a dynamic ip, so it is updated every time and I have to manually fix the config

This is my docker-compose file

version: '3'
services:
#PHP Service
app:
    build:
        context: .
        dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
        SERVICE_NAME: app
        SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
        - ./:/var/www
        - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
        - postgres
#Nginx Service
webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
        - "80:80"
        - "443:443"
    volumes:
        - ./:/var/www
        - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
        - postgres
#Redis
redis:
    image: 'redis:alpine'
    ports:
        - "6379:6379"
#PostgreSQL        
postgres:
    container_name: postgres_container
    image: postgres
    hostname: postgres
    environment:
        POSTGRES_DB: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: secret
        PGDATA: /data/postgres
    volumes:
        - postgres:/data/postgres
    ports:
        - "5432:5432"
    networks:
        - postgres
    restart: unless-stopped

pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
        PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
        PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
    volumes:
        - pgadmin:/root/.pgadmin
    ports:
        - "${PGADMIN_PORT:-5050}:80"
    networks:
        - postgres
    restart: unless-stopped  
    depends_on: 
        - postgres     
#RabbitMQ
rabbit:
    image: "rabbitmq:3-management"
    hostname: "rabbit"
    environment:
        RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
        RABBITMQ_DEFAULT_USER: "rabbitmq"
        RABBITMQ_DEFAULT_PASS: "rabbitmq"
        RABBITMQ_DEFAULT_VHOST: "/"
    ports:
        - "15672:15672"
        - "5672:5672"
    labels:
        NAME: "rabbitmq"    
    networks:
        - postgres     
#ElasticSearch
elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
    ports:
        - "9200:9200"
        - "9300:9300"
    networks:
        - postgres                      
#Docker Networks
networks:
    postgres:
        driver: bridge
volumes:
    postgres:
    pgadmin:   

How can I tell the rabbitmq container to connect to the external ip of the app container?

Sergey Karp
  • 31
  • 1
  • 4
  • Does this answer your question? [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – akop Jan 21 '21 at 13:07

1 Answers1

0

Sorry about the confusion, I somehow mixed up Redis and RabbitMQ. So here is a probably better matching answer:

You can connect to a service on your host from the Docker container even with a changing IP - there is script and a nice answer in this SO post.

Some excerpt:

If you are on Mac or Windows you can set

RABBITMQ_HOST=host.docker.internal

In general more easy I would consider a separate container running RabbitMQ and then using rabbitmq as hostname to connect to.

services:
    # ... other services
    rabbitmq:
        image: rabbitmq:alpine
        environment:
            RABBITMQ_DEFAULT_USER: "defaultuser"
            RABBITMQ_DEFAULT_PASS: "defaultpass"
        ports:
            - 5672:5672
codedge
  • 4,754
  • 2
  • 22
  • 38