无法连接到docker0(MongoDB图表)

无法连接到docker0(MongoDB图表),mongodb,docker,Mongodb,Docker,我对docker完全陌生。我按照说明安装mongodb图表和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

我对docker完全陌生。我按照说明安装mongodb图表和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
版本:“3.3”
服务:
图表:
图片:quay.io/mongodb/charts:v0.10.0
主机名:图表
端口:
#主机:容器端口映射。如果你想让MongoDB图表
#可在docker主机上的其他端口上访问,请更改此选项
#至:80,例如8888:80。
- 80:80
- 443:443
卷数:
-关键点:/mongodb图表/卷/关键点
-日志:/mongodb图表/卷/日志
-数据库证书:/mongodb图表/卷/db证书
-网络证书:/mongodb图表/卷/网络证书
环境:
#以下2个环境变量的存在将在Charts服务器上启用HTTPS。
#所有HTTP请求也将重定向到HTTPS。
#要启用HTTPS,请将您的证书和密钥文件上载到web certs卷,
#取消注释以下行并替换为证书和密钥文件的名称。
#图表\u HTTPS\u证书\u文件:CHARTS-HTTPS.crt
#CHARTS\u HTTPS\u证书\u密钥\u文件:CHARTS-HTTPS.KEY
#此环境变量控制内置的支持小部件和
#MongoDB图表中的度量集合。要同时禁用这两个选项,请设置该值
#“关”。默认值为“开”。
图表\u支持\u小部件\u和\u指标:“打开”
#可以上载SSL证书(.pem格式)的目录,其中
#在以下情况下,应将其视为受信任的自签名证书或根证书:
#Charts正在使用?ssl=true访问MongoDB服务器
SSL证书目录:/mongodb图表/卷/db证书
网络:
-后端
秘密:
-图表mongodb uri
网络:
后端:
卷数:
钥匙:
日志:
db证书:
网络证书:
秘密:
图表mongodb uri:
外部:正确

如何连接到mongodb?

假设您知道如何使用
echo“mongodb://:@myhost.com/”| docker secret create charts mongodb uri-
创建url连接

问题实际上是如何从docker容器连接到主机上运行的外部服务。你可以从很多问题中得到帮助,比如

基本上,如果您正在使用docker for mac或windows,请使用类似于
echo的东西mongodb://host.docker.internal“| docker secret create charts mongodb uri-
,对于linux,请参阅在本地主机上运行元数据数据库一节,或仅使用主机模式(删除端口一节)


默认情况下,mongodb配置为仅接受本地主机127.0.0.1的结果,当charts image通过docker连接到它时,它被视为来自docker0的外部连接,因此被mongod拒绝
[MongoneWorkError:connect EconRefuse172.17.0.1:27017]

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

这可能是一个老问题,但我认为这可能是一个常见的问题,我不得不与之斗争一段时间,然后才真正更彻底地阅读错误消息,并意识到这确实是一个简单的问题


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

MongoDB在哪里运行?(直接在主机上?)?(其他码头集装箱?)你是怎么开始的;您能否共享您的
docker run
命令或
docker compose.yml
文件?@DavidMaze已更新。谢谢我已经尝试过:docker运行--rm quay.io/mongodb/charts:v0.10.0 charts cli测试连接mongodb://172.17.0.1 在linux中,但不工作。我需要设置bindIP=0.0.0.0我还需要执行sudo chmod a+rwx/var/run/docker.sock sudo chmod a+rwx/var/run/docker.pid来获得权限
version: "3.3"
services:
  charts:
    image: quay.io/mongodb/charts:v0.10.0
    hostname: charts
    network_mode: "host"
    ...