[英]Cannot access mongodb inside docker container
我目前正在 docker 中將我的應用程序容器化。 我有兩個圖像,一個用於我的節點應用程序,另一個用於 mongodb。 我有兩個使用 docker-compose 的鏈接。 但是每當我嘗試使用端口 27017 訪問容器內的 mongodb 時。但它顯示如下:**
看起來您正試圖在本機驅動程序端口上通過 HTTP 訪問 MongoDB。
** 我有 mongodb 配置為:
"url": "mongodb://mongoDB/astroDB"
我的 docker-compose 文件如下:
version: '3.0'
services:
web:
image: astrobot-node
build: .
command: "yarn start"
ports:
- "80:3601"
depends_on:
- "mongo"
mongo:
image: mongo
ports:
- "27017:27017"
誰能告訴我實際上出了什么問題。 提前致謝。
標准 mangodb 連接 url 應該是這樣的
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
https://docs.Z685A5F7CC703手動/4796F68C.com.連接字符串/
在您的情況下, url字符串中的 mongoDB 應指示數據庫主機,要使其指向 mangodb 容器,您應將其更改為"url": "mongodb://mongo/astroDB"
,導致您的服務名為mango而不是mangoDB 。
you can also achieve that by provide a static IP to your container and write this IP directly, you can also find you container network IP by using the docker container inspect <container-IP>
to test it manually.
編輯
這是一個可以幫助您調試問題的線程。 https://forums.docker.com/t/how-mongodb-work-in-docker-how-to-connect-with-mongodb/44763/8
但是每當我嘗試使用端口 27017 訪問容器內的 mongodb 時。但它顯示如下:**
It looks like you are trying to access MongoDB over HTTP on the native driver port.
如果您嘗試將 HTTP 請求發送到 MongoDB 端口,這是預期的行為。 這對我說的是你已經弄清楚了你的網絡連接並且那部分工作正常。
接下來,您只需要使用 MongoDB 驅動程序從應用程序與數據庫對話,而不是使用curl
或類似的東西來訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.