无法将rabbitmq和redis从容器连接到同一网络中的另一个容器

问题描述 投票:0回答:1

我正在使用 docker-compose 文件在本地处理我的服务和数据库,因此我准备了一个如下所示的 docker-compose 文件

version: '3'

networks:
  sosyal:
    driver: bridge
services:
  db:
    container_name: sql
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      SA_PASSWORD: 514Emir229
      ACCEPT_EULA: Y
      MSSQL_DATABASE: TestDB
    volumes:
      - mssql-data:/var/opt/mssql
    ports:
      - "1433:1433"
    networks:
      - sosyal

  rabbitmq-docker:
    container_name: rabbitmq-docker
    image: rabbitmq:3-management
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
    volumes:
      - rabbitmq_data:/data
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - sosyal
  redis-docker:
    container_name: redis-docker
    image: redis/redis-stack:latest

    ports:
      - "6379:6379"
    networks:
      - sosyal
  sosyal-auth:
    build:
      context: sosyal-auth
      dockerfile: Dockerfile
    env_file:
      - /Users/emirkutlugun/projects/sosyal-yeni/sosyal-auth/.development.docker.env
    networks:
      - sosyal
    depends_on:
      - db
      - rabbitmq-docker
      - redis-docker
  sosyal-account:
    build:
      context: sosyal-account
      dockerfile: Dockerfile
    env_file:
      - /Users/emirkutlugun/projects/sosyal-yeni/sosyal-account/.development.docker.env
    networks:
      - sosyal
    depends_on:
      - db
      - rabbitmq-docker
      - redis-docker

  sosyal-gateway:
    build:
      context: sosyal-gateway
      dockerfile: Dockerfile
    environment:
      - NODE_ENV=development
    ports:
      - "3000:3000"
    depends_on:
      - sosyal-auth
      - sosyal-account
    networks:
      - sosyal


volumes:
  mssql-data:
  rabbitmq_data:

之后我将rabbit mq的连接字符串更改为

    const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
        transport: Transport.RMQ,
        options: {
            urls: [`amqp://guest:guest@rabbitmq-docker:5672`],
            queue: 'user_queue',
            queueOptions: {
                durable: false
            },
        },
    })

但是当我尝试连接时出现以下错误

sosyal-account-1  | [Nest] 1  - 02/08/2024, 7:10:43 AM   ERROR [Server] Error: connect ECONNREFUSED 127.0.0.1:5672

我真的不明白为什么它尝试连接到

127.0.0.1:5672

当我检查网络时,它们都在同一个网络中。那么如何从同一个docker-compose中的另一个容器连接rabbitmq呢?

docker docker-compose rabbitmq nest
1个回答
0
投票

您的应用程序似乎没有获取您提供的 Rabbitmq url。

docker exec -it yourBackendContainerName /bin/sh

之后你应该尝试 pingrabbitmq 容器。

  ping rabbitmq-docker

如果它解决了 127.0.0.1 以外的正确主机问题,那么问题可能出在您的代码中

© www.soinside.com 2019 - 2024. All rights reserved.