无法连接到docker0(MongoDB图表)

时间:2018-11-10 15:48:53

标签: mongodb docker

我对docker完全陌生。我按照this的说明安装了mongodb chart和docker。

当我连接到172.17.0.1时,它说

Unable to connect to MongoDB using the specified URI.

The following error was returned while attempting to connect: 
MongoNetworkError: failed to connect to server [172.17.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.17.0.1:27017]

The result from pinging the specified server "172.17.0.1" from within the container is:
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.050 ms

--- 172.17.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.050/0.050/0.050/0.000 ms

mongodb在本地计算机上运行。我认为它未在容器中运行(不确定),因为我在安装docker之前在机器中安装了mongodb。

我还使用docker network inspect bridge

检查了设置
"Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]

这是yml文件:

version: "3.3"

services:
  charts:
    image: quay.io/mongodb/charts:v0.10.0
    hostname: charts
    ports:
      # host:container port mapping. If you want MongoDB Charts to be
      # reachable on a different port on the docker host, change this
      # to <port>:80, e.g. 8888:80.
      - 80:80
      - 443:443
    volumes:
      - keys:/mongodb-charts/volumes/keys
      - logs:/mongodb-charts/volumes/logs
      - db-certs:/mongodb-charts/volumes/db-certs
      - web-certs:/mongodb-charts/volumes/web-certs
    environment:
      # The presence of following 2 environment variables will enable HTTPS on Charts server.
      # All HTTP requests will be redirected to HTTPS as well.
      # To enable HTTPS, upload your certificate and key file to the web-certs volume,
      # uncomment the following lines and replace with the names of your certificate and key file.
      # CHARTS_HTTPS_CERTIFICATE_FILE: charts-https.crt
      # CHARTS_HTTPS_CERTIFICATE_KEY_FILE: charts-https.key

      # This environment variable controls the built-in support widget and
      # metrics collection in MongoDB Charts. To disable both, set the value
      # to "off". The default is "on".
      CHARTS_SUPPORT_WIDGET_AND_METRICS: "on"
      # Directory where you can upload SSL certificates (.pem format) which
      # should be considered trusted self-signed or root certificates when
      # Charts is accessing MongoDB servers with ?ssl=true
      SSL_CERT_DIR: /mongodb-charts/volumes/db-certs
    networks:
      - backend
    secrets:
      - charts-mongodb-uri

networks:
  backend:

volumes:
  keys:
  logs:
  db-certs:
  web-certs:

secrets:
  charts-mongodb-uri:
    external: true

如何连接到mongodb?

2 个答案:

答案 0 :(得分:1)

默认情况下,mongodb默认配置为仅接受来自localhost 127.0.0.1的结果,并且当图表图像通过docker连接到它时,它被视为来自docker0的外部连接,因此被mongod [MongoNetworkError: connect ECONNREFUSED 172.17.0.1:27017]拒绝。 >

要解决此问题,请编辑mongo配置sudo vim /etc/mongod.conf并将您的docker0 ip添加到bindIp配置中,对于默认docker安装,应将带有bindIp: 127.0.0.1的行更改为bindIp: 127.0.0.1,172.17.0.1

这可能是一个古老的问题,但我认为这可能是一个常见的问题,在实际更彻底地阅读错误消息并将其真正实现之前,我不得不花了一段时间才能解决这个问题。

另一个问题是,在首次安装时,您可以不使用用户名或密码即可连接到mongo,因此,如果未配置安全性,则应从uri中删除这两个,使其成为 mongodb://172.17.0.1 :27017

答案 1 :(得分:0)

假设您知道如何使用echo "mongodb://<username>:<password>@myhost.com/" | docker secret create charts-mongodb-uri -创建URL的连接。

问题实际上是如何从Docker容器连接到主机上运行的外部服务。您可以从From inside of a Docker container, how do I connect to the localhost of the machine?

之类的许多问题中获得一些帮助

基本上,如果您在Mac或Windows上使用docker,请使用echo "mongodb://host.docker.internal" | docker secret create charts-mongodb-uri -之类的文件,对于Linux,请参见https://docs.mongodb.com/charts/master/installation/RUNNING METADATA DATABASE ON LOCALHOST中的文档,或仅使用主机模式(删除端口)部分)

version: "3.3"
services:
  charts:
    image: quay.io/mongodb/charts:v0.10.0
    hostname: charts
    network_mode: "host"
    ...