1

i have project in which Rabbit is launched:

services:
  rabbitmq:
    image: rabbitmq:3-management-alpine
    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

networks:
  rabbitmq_go_net:
    driver: bridge

Then I made another project that connects to this queue. Without the doker, I just used localhost as the host and port number 5672.

I wanted to run another project in docker with a database:

version: '3'

services:
    postgres:
        image: postgres:12
        restart: always
        networks:
            - rabbitmq_go_net
        ports:
            - '5432:5432'    
        volumes:
            - ./db_data:/var/lib/postgresql/data
            - ./app/internal/config/dbconfig/init.sql:/docker-entrypoint-initdb.d/create_tables.sql
        env_file:
            - ./app/internal/config/dbconfig/.env
        healthcheck:
            test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U", "postgres" ]
            timeout: 45s
            interval: 10s
            retries: 10
    
    app:
        build: app
        networks:
            - rabbitmq_go_net
        depends_on:
            postgres:
                condition: service_healthy
volumes:
    db_data:

networks:
    rabbitmq_go_net:
        driver: bridge

And now I can't connect to Rabbit.I tried to make a different network and with the same name, but every time I get the same error:

FATA[2022-07-31T13:24:09Z]ipstack/internal.(*app).startConsume() app.go:43 failed to connect to RabbitMQ due dial tcp 127.0.0.1:5672: connect: connection refused

Connect:

addr := fmt.Sprintf("amqp://%s:%s@%s:%s/", cfg.Username, cfg.Password, cfg.Host, cfg.Port)

Where host is the container name rabbitmq. Is it possible to do this, or is it necessary to put programs in a container in some other way?I will be glad to help

Omegon
  • 387
  • 2
  • 10
  • Can you move everything into a single `docker-compose.yml` file, and delete all of the manual `networks:` settings? Then you'll have a single network automatically created by Compose, and the `rabbitmq` service name will be usable as a host name. – David Maze Jul 31 '22 at 17:18

1 Answers1

1

I think the issue is that your Docker networks aren't the same despite using the same name in the two different compose files. Docker prefixes networks declared inside compose files with the project name to avoid collisions.

Everything you're looking for can be found in this SO response and corresponding thread. It shows how to use the external flag on a network so that the second compose doesn't create a new network, and also talks about how Docker uses the project name as a prefix for the network name so you can predict what the generated network name can be.

Alternatively, you can create the network in advance with docker network create and use the external flag inside both compose files so you don't need to worry about docker's naming convensions.

David T.
  • 988
  • 8
  • 12