当尝试使用docker-compose启动一个docker容器时,我得到以下错误消息:
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery
and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"
我注意到mongo-express总是在我的连接字符串末尾添加一个双引号,这是故意的还是我在docker-compose文件中搞砸了?
文件:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
networks:
- backbone
expose:
- 27017
ports:
- 27017:27017
env_file:
- ./.env
command: [--auth]
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ./data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: unless-stopped
networks:
- backbone
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=127.0.0.1
- ME_CONFIG_MONGODB_URL="mongodb://127.0.0.1:27017"
- ME_CONFIG_BASICAUTH_USERNAME=${DB_USERNAME}
- ME_CONFIG_BASICAUTH_PASSWORD=${DB_PASSWORD}
networks:
backbone:
driver: bridge
我已经尝试过更新我的docker/docker-compose和mongodb版本。
如何解决错误?或者有没有一个我可以使用的变通方法?
2条答案
按热度按时间vu8f3i0k1#
在容器中,
localhost
是指容器本身。因此,当Mongo Express尝试连接到localhost
上的数据库时,它会在Mongo Express容器中查找数据库。Docker compose允许你使用容器名来引用容器,所以你应该把你的Mongo Express环境变量改为
zhte4eai2#
我在Hans Kilian的帮助下解决了这个问题,但我必须添加一个环境变量,这是我的docker-compose文件。