简体   繁体   English

ConnectionException 尝试使用 ZBAEDB53E845AE71F13945FCCZ057 从另一个 docker 容器连接到 docker 中的 mongoDB

[英]ConnectionException trying to connect to mongoDB in docker from another docker container using docker-compose

I am trying to connect to mongoDB in a container from a service in another container but getting connection exceptions我正在尝试从另一个容器中的服务连接到容器中的 mongoDB 但出现连接异常

I have created a docker-compose.yml我创建了一个 docker-compose.yml

version: "3"
services:
  mongo-container:
    image: mongo:latest
    container_name: "mongo-container"
    environment:
      - MONGO_INITDB_DATABASE=transactions

    ports:
      - 27017:27017
    networks:
      - banking-network
    volumes:
      - mongodb_data_container:/data/db

  transaction-service:
    image: transaction-service
    container_name: transaction-service
    build:
      context: transaction-service
      dockerfile: local.dockerfile
    ports:
      - "8083:8083"
    depends_on:
      - mongo-container
    environment:
      - MONGO_URL=mongodb://mongo-container:27017/transactions
    networks:
      - banking-network
networks:
  banking-network:
    external:
      name: banking-network

volumes:
  mongodb_data_container:

And in my service application.yml在我的服务 application.yml

spring:
  application:
    name: transaction-service
server:
  port: '8083'

mongodb:
  connection:
    string: mongodb://mongo-container:27017/transactions
  database: transactions

So I have a service in one container, and mongoDb in another, both on the same network所以我在一个容器中有一个服务,在另一个容器中有 mongoDb,两者都在同一个网络上

d666b7a242fc   transaction-service   "java -jar /app/tran…"   18 minutes ago   Up 18 minutes   0.0.0.0:8083->8083/tcp     transaction-service
516d539b29c7   mongo:latest          "docker-entrypoint.s…"   18 minutes ago   Up 18 minutes   0.0.0.0:27017->27017/tcp   mongo-container

The following is MONGO_URL=mongodb://name of container:27017/name of database下面是 MONGO_URL=mongodb://name of container:27017/name of database

- MONGO_URL=mongodb://mongo-container:27017/transactions

Now I can connect to this mongodb from a rest service running on my local host without needing a password and username so I have not added it to my docker-compose.现在我可以从在我的本地主机上运行的 rest 服务连接到这个 mongodb,而不需要密码和用户名,所以我没有将它添加到我的 docker-compose。 I have seen other examples not using it.我见过其他不使用它的例子。

But I am still getting an exception.但我仍然遇到一个例外。

It seems this should be straightforward, it has the docker container name, the port and its on the same network.看起来这应该很简单,它具有 docker 容器名称、端口及其在同一网络上。 What am I missing.我错过了什么。

Thanks for any help.谢谢你的帮助。

I found wiring up that does allow one container to talk to another.我发现接线确实允许一个容器与另一个容器通信。 The confusing thing, is its totally different to how the docs say to do it.令人困惑的是,它与文档所说的完全不同。

in my application.yml I have在我的 application.yml 我有

spring:
  application:
    name: transaction-service
  data:
    mongodb:
      database: Transaction
      host: mongo-container
      port: 27017
server:
  port: '8083'

And in my docker-compose在我的 docker-compose

services:
  mongo-container:
    image: mongo:latest
    container_name: "mongo-container"

    ports:
      - 27017:27017
    networks:
      - banking-network
    volumes:
      - mongodb_data_container:/data/db

  transaction-service:
    image: transaction-service
    container_name: transaction-service
    build:
      context: transaction-service
      dockerfile: local.dockerfile
    ports:
      - "8083:8083"
    depends_on:
      - mongo-container
    links:
      - mongo-container
    networks:
      - banking-network
networks:
  banking-network:
    external:
      name: banking-network

volumes:
  mongodb_data_container:

Its confusing that, what should work, what the documents detail does not work.令人困惑的是,什么应该起作用,什么文件细节不起作用。 But this does.但这确实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM