egg-cnode
egg-cnode copied to clipboard
使用docker-compose 启动失败 MongoError
环境:
- 操作系统: CentOS Linux release 7.2.1511 (Core)
- docker: Docker version 18.03.0-ce, build 0520e24
- docker-compose: docker-compose version 1.20.1, build 5d8c71b
描述
修改docker-compose.yml
开启 redis 和mongo 默认端口设置 6379 和 27017
redis:
image: redis:3.2-alpine
command: redis-server --appendonly yes --requirepass egg_cnode
volumes:
- egg-redis:/data
networks:
- docker_cnode
ports:
- 6379:6379
mongodb:
image: mongo:3.2
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb
- MONGO_INITDB_DATABASE=egg_cnode
volumes:
- egg-mongo:/data/db
- ./init.d/mongo:/docker-entrypoint-initdb.d
networks:
- docker_cnode
ports:
- 27017:27017
执行 docker-compose up -d 命令
[root@VM_0_11_centos egg-cnode]# docker-compose up -d
Creating network "eggcnode_docker_cnode" with driver "bridge"
Creating volume "eggcnode_egg-mongo" with default driver
Creating volume "eggcnode_egg-redis" with default driver
Creating eggcnode_mongodb_1 ... done
Creating eggcnode_redis_1 ... done
Creating eggcnode_cnode_1 ... done
查看容器eggcnode_cnode_1 日志
2018-04-08 07:07:18,978 INFO 42 [egg:core:ready_stat] end ready task /usr/src/app/node_modules/egg-core/lib/egg.js:319:10, remain ["/usr/src/app/node_modules/egg-watcher/app.js:15:7","/usr/src/app/node_modules/egg-redis/lib/redis.js:48:7","/usr/src/app/node_modules/egg-mongoose/lib/mongoose.js:74:7"]
2018-04-08 07:07:18,985 ERROR 42 [egg-mongoose] mongodb://egg_cnode:egg_cnode@mongodb:27017/egg_cnode disconnected
2018-04-08 07:07:18,987 INFO 42 [egg-redis] connect success on redis://:egg_cnode@redis:6379/0
2018-04-08 07:07:18,991 ERROR 42 nodejs.MongoError: [egg-mongoose]failed to connect to server [mongodb:27017] on first connect [MongoError: connect ECONNREFUSED 172.19.0.2:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoError'
message: '[egg-mongoose]failed to connect to server [mongodb:27017] on first connect [MongoError: connect ECONNREFUSED 172.19.0.2:27017]'
pid: 42
hostname: ee66c1c8cd4f
排查
链接mongodb数据库进行测试
[root@VM_0_11_centos egg-cnode]# mongo 172.19.0.2:27017/egg_cnode -u egg_cnode -p egg_cnode
MongoDB shell version: 3.2.19
connecting to: 172.19.0.2:27017/egg_cnode
> show collections
egg_cnode
> db.egg_cnode.find({})
{ "_id" : ObjectId("5ac9bfa4cdf6538a02662544"), "egg_cnode" : "egg-cnode" }
在centos 中可以正常访问 172.19.0.2:27017/egg_cnode 库
备注
本人 docker 和 docker-compose 知识有限,目前使用遇到如上问题,还望大神帮忙答疑解惑
Mac 本地一键拉起,暂未发现问题,明天用 CentOS 测试一下。