docker compose mongodb 容器工作,但 UI 无法连接它(超时)

问题描述 投票:0回答:1
  1. docker compose bulid mongodb 容器并且它工作了。
  2. 创建数据库和集合并插入数据完成。
  3. 然后,进入这个容器就可以找到我提到的那些数据了。 但是当我使用 Vscode 扩展时(MongoDB for VS Code 或应用程序:MongoDBCompass 或 DB 扩展) 他们无法连接这个容器。

这是我的yml:

mongodb:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    ports:
      - "27017:27017"
    healthcheck:
      test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      retries: 5
      start_period: 5s
    restart: always
    volumes:
      - mongodb-data:/data/db

容器中的mongosh:

admin> show dbs
JobDB   432.00 KiB << this is my testing db, there're some collections and data
admin   100.00 KiB
config  108.00 KiB
local    72.00 KiB

docker ps

CONTAINER ID   IMAGE                  COMMAND                   CREATED          STATUS                      PORTS                               NAMES
fb43b9cbfb63   apache/airflow:2.7.3   "/usr/bin/dumb-init …"   48 minutes ago   Up 48 minutes (healthy)     8080/tcp                            automation-airflow-triggerer-1
52af82cb0b13   apache/airflow:2.7.3   "/usr/bin/dumb-init …"   48 minutes ago   Up 48 minutes (healthy)     0.0.0.0:8080->8080/tcp              automation-airflow-webserver-1
28f084bff929   apache/airflow:2.7.3   "/usr/bin/dumb-init …"   48 minutes ago   Up 48 minutes (healthy)     8080/tcp                            automation-airflow-scheduler-1
da474b892d84   apache/airflow:2.7.3   "/usr/bin/dumb-init …"   48 minutes ago   Up 48 minutes (healthy)     8080/tcp                            automation-airflow-worker-1
e27505eb04e9   redis:latest           "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes (healthy)     6379/tcp                            automation-redis-1
ee686330e99d   mysql:8.0              "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes               33060/tcp, 0.0.0.0:3308->3306/tcp   automation-mysqldb-1
d8b4971d9b86   postgres:13            "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes (healthy)     5432/tcp                            automation-postgres-1
f4ad04c6de18   mongo:latest           "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes (unhealthy)   0.0.0.0:27017->27017/tcp            automation-mongodb-1

docker网络ls

NETWORK ID     NAME                         DRIVER    SCOPE
f45e66a2be2e   automation_default           bridge    local

连接扩展:

enter image description here

enter image description here

enter image description here

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' automation-mongodb-1
带上这个容器的IP仍然没有与UI连接

mongodb docker containers connection
1个回答
0
投票

您遇到超时错误,因为您尝试使用未知的 URI 连接 MongoDB 服务器。您在 URI 中使用了

mongodb
,这是不正确的。仅当您尝试从同一 MongoDB 容器网络中的其他服务连接服务器时,才应该这样做。

由于您似乎是从主机连接,因此您需要使用此 URI:

mongodb://root:[email protected]:27017
。您可以这样做,因为您在 MongoDB 服务的 docker compose 配置中导出了端口
27017
,因此该端口在您的主机上可用。

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