我正在尝试使用docker-compose
连接运行在Docker上的两个MongoDB容器。其中一个容器使用mongoimport
向另一个容器注入一些数据。
然而,当我运行mongoimport
时,我遇到了以下错误:
Failed: error connecting to db server: no reachable servers
由于我将 ping -c 3 mongodb
添加到了 CMD
中,而容器成功解析了该主机名,因此我实际上可以从 mongo-seed
访问 mongodb
容器。
以下是我使用的文件:
docker-compose.yml
version: '2'
services:
mongodb:
image: mongo:3.2
ports:
- "27017:27017"
mongo-seed:
build: ./mongo-seed
mongo-seed/Dockerfile
FROM mongo:3.2
COPY init.json /init.json
CMD ping -c 3 mongodb && mongoimport --host mongodb --db test --collection users \
--type json --file /init.json --jsonArray
mongo-seed/init.json
[
{
"name": "Joe Smith",
"email": "jsmith@gmail.com",
"age": 40,
"admin": false
},
{
"name": "Jen Ford",
"email": "jford@gmail.com",
"age": 45,
"admin": true
}
]
这是docker-compose up
的输出结果:
mongo-seed_1 | PING mongodb (172.18.0.2): 48 data bytes
mongo-seed_1 | 56 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.116 ms
mongo-seed_1 | 56 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.141 ms
mongo-seed_1 | 56 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.114 ms
mongo-seed_1 | --- mongodb ping statistics ---
mongo-seed_1 | 3 packets transmitted, 3 packets received, 0% packet loss
mongo-seed_1 | round-trip min/avg/max/stddev = 0.114/0.124/0.141/0.000 ms
mongo-seed_1 | 2016-08-09T20:34:15.728+0000 [........................] smtt.devices 0.0 B/25.5 MB (0.0%)
mongo-seed_1 | 2016-08-09T20:34:17.992+0000 [........................] smtt.devices 0.0 B/25.5 MB (0.0%)
mongo-seed_1 | 2016-08-09T20:34:17.992+0000 Failed: error connecting to db server: no reachable servers
mongo-seed_1 | 2016-08-09T20:34:17.992+0000 imported 0 documents
mongo_mongo-seed_1 exited with code 1
有任何想法吗?我错过了什么吗?