我正在使用的堆栈:
NAME STATE VERSION
* Ubuntu-18.04 Running 2
Ubuntu-20.04 Stopped 2
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:36 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:01:06 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
对于 MongoDB,我使用官方图像:
plonca@KAT-IT-1012:~$ docker run -d -p 27017-27019:27017-27019 --name mongodb_408 mongo:4.0.8
我可以登录容器并检查 mongo 是否正在运行且可访问:
plonca@KAT-IT-1012:~$ docker exec -it mongodb_408 bash
root@5b622e89bfef:/# mongo
MongoDB shell version v4.0.8
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0ae27473-4cc7-4d0d-9976-c922ea58b936") }
MongoDB server version: 4.0.8
Server has startup warnings:
2020-11-26T09:09:56.410+0000 I STORAGE [initandlisten]
2020-11-26T09:09:56.410+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-11-26T09:09:56.410+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten]
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten]
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten]
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-11-26T09:09:57.140+0000 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
Docker 似乎正在监听端口 27017:
plonca@KAT-IT-1012:~$ sudo netstat -tulpn | grep :27017
tcp6 0 0 :::27017 :::* LISTEN 2114/docker-proxy
但是,当尝试从 Mongo Compass(安装在 Windows 上)连接到
mongodb://127.0.0.1:27017
时,我收到此错误:
connect ECONNREFUSED 127.0.0.1:27017
带有容器 IP 地址 (
mongodb://172.17.0.2:27017
) 的连接字符串会导致超时。如何通过 Windows 上安装的 Mongo Compass 连接在容器中运行的 MongoDB?
我的 Windows 10 机器上安装了 docker。 我拉了最新的mongo镜像 我使用默认选项运行图像(无可选设置)
我遇到了同样的错误:连接 ECONNREFUSED 127.0.0.1:27017
当我检查我的容器时,我注意到没有分配端口。 然后我尝试通过添加主机端口来使用可选设置运行映像。 (另外,明智的做法是添加所需的容器名称和卷路径) 通过此可选设置,我可以毫无问题地连接 MongoDB Compass 或任何其他 MongoDB 客户端。希望这对遇到同样问题的人有所帮助。
version: '3.8'
services:
api:
build:
context: .
dockerfile: Dockerfile.apiParser
ports:
- 8090:8090
depends_on:
- mongo
volumes:
- ./src:/rm-mafia/src
- ./.env.local:/rm-mafia/.env
- ./node_modules:/rm-mafia/node_modules
networks:
- rm-mafia
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASS}
- MONGO_INITDB_DATABASE=${MONGO_DB}
build:
context: .
dockerfile: Dockerfile.mongo
ports:
- 27018:27017
networks:
- rm-mafia
networks:
rm-mafia:
driver: bridge
然后在您的实际连接字符串(api 端 mongo url)上它将是:
mongodb://mongo:27017/your_db_name
对于完整的上下文,我的 Dockerfile.mongo 是:
FROM mongo
CMD ["mongod"]
EXPOSE 27017
EXPOSE 28017
希望有帮助。祝你有美好的一天