[英]Can't connect to MongoDB inside docker container
I'm trying to connect to a mongodb container inside docker but I'm getting this error:我正在尝试连接到 docker 内的 mongodb 容器,但出现此错误:
getaddrinfo ENOTFOUND mongo.ks.local
The database works just fine when I access it from docker but I'm not able to access it from MongoDBCompass当我从 docker 访问数据库时,数据库工作得很好,但我无法从 MongoDBCompass 访问它
This is my docker-compose.yml file:这是我的 docker-compose.yml 文件:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
ports:
- 27017:27017
volumes:
- ks_mongodb:/data/db
I'm quite new to docker and will appreciate any help I can get我对 docker 很陌生,我将不胜感激
You need the ip address of the container:您需要容器的 ip 地址:
try:尝试:
docker inspect <CONTAINER NAME>
And search for the ip address.并搜索 ip 地址。 Then use the ip address to connect to mongodb
然后使用ip地址连接到mongodb
I have tried connecting to the container using mongo compass.我尝试使用 mongo compass 连接到容器。 It failed but when I removed the line of mounting the volume and tried again it connects to the database.
它失败了,但是当我删除安装卷的行并再次尝试时,它连接到数据库。 Then tried to mount the volume like below it worked.
然后尝试像下面那样安装卷,它起作用了。 I have changed the username and password and specified the INITDB env variable.
我更改了用户名和密码并指定了 INITDB 环境变量。
version: "3.8"
services:
mongo:
image: mongo:latest
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1234
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- /data/db
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.