使用 Docker-Compose 在 MongoDB 上导入数据 [英] Import Data on MongoDB using Docker-Compose
问题描述
有许多站点展示了如何使用以下 docker-compose 方法将数据导入 mongodb,但我的站点没有找到 db 服务器.
如何使用 docker 为 mongo 数据库做种-撰写?https://gist.github.com/jschwarty/6f9907e2871d1ece5bde53d2559chttps://docs.codefresh.io/v1.0/docs/import-data-to-mongodb-in-composition
想知道是否发生了一些变化,使其不再起作用.
docker-compose.yml
版本:'3'服务:mongodb:图片:蒙戈命令:mongod --smallfiles端口:- 27017mongo_seed:构建:mongo-seed/.链接:- MongoDB
mongo-seed/Dockerfile
来自 mongo复制 census.json/census.jsonCMD mongoimport --host mongodb --db test --collection census --type json --file/census.json --jsonArray
mongo-seed/census.json
<预><代码>[{地理":阿拉巴马州阿比维尔市",人口普查":2688"}]docker-compose up
后出错mongodb_1 |2018-01-09T21:57:14.353+0000 I NETWORK [initandlisten] 等待端口 27017 上的连接mongo_seed_1 |2018-01-10T00:19:58.286+0000 [........................] test.census 0B/1KB (0.0%)mongo_seed_1 |2018-01-10T00:19:58.332+0000 [........................] test.census 0B/1KB (0.0%)mongo_seed_1 |2018-01-10T00:19:58.332+0000 失败:连接到数据库服务器时出错:没有可访问的服务器mongo_seed_1 |2018-01-10T00:19:58.332+0000 导入 0 个文件
在 mongoimport 语句中,我尝试使用不同的主机名:mongodb_1、127.0.0.1、localhost.
我已经读到如果设置了 replSet 它会出现这个错误但我不会设置 replSet 除非它是默认设置在这种情况下我不知道它是如何设置的禁用它.
我也试过 mongorestore 而不是 mongoimport 但得到同样的错误.
我读到 link 在 Docker 中已弃用,可能环境变量不再起作用.不确定这是真的还是发生在这里.
有人有想法吗?
我最终删除了 Dockerfile,在 bash 脚本中添加了命令,然后从 docker-compose 文件中调用了该脚本.在 docker-compose 文件中使用了一个脚本而不是一个命令,因为我正在导入几个文件,因此我的示例中没有显示几个命令.我需要使用 mongo:3.2.6 来完成这项工作.可能还有其他版本,但这个肯定有效.
docker-compose.yml
版本:'3'服务:mongodb:图像:mongo:3.2.6端口:- 27017:27017mongo_seed:图像:mongo:3.2.6链接:- MongoDB卷:- ./mongo-seed:/mongo-seed命令:/mongo-seed/import.sh
/mongo-seed/import.sh
#!/bin/bashmongoimport --host mongodb --db test --collection census --type json --file/mongo-seed/census.json --jsonArray
There are many sites that show how to import data to a mongodb using the below docker-compose method but mine is not finding the db server.
How do I seed a mongo database using docker-compose? https://gist.github.com/jschwarty/6f9907e2871d1ece5bde53d259c18d5f https://docs.codefresh.io/v1.0/docs/import-data-to-mongodb-in-composition
Wondering if something has changed making this not work any longer.
docker-compose.yml
version: '3'
services:
mongodb:
image: mongo
command: mongod --smallfiles
ports:
- 27017
mongo_seed:
build: mongo-seed/.
links:
- mongodb
mongo-seed/Dockerfile
FROM mongo
COPY census.json /census.json
CMD mongoimport --host mongodb --db test --collection census --type json --file /census.json --jsonArray
mongo-seed/census.json
[
{
"Geography": "Abbeville city, Alabama",
"Census": "2688"
}
]
ERROR after docker-compose up
mongodb_1 | 2018-01-09T21:57:14.353+0000 I NETWORK [initandlisten] waiting for connections on port 27017
mongo_seed_1 | 2018-01-10T00:19:58.286+0000 [........................] test.census 0B/1KB (0.0%)
mongo_seed_1 | 2018-01-10T00:19:58.332+0000 [........................] test.census 0B/1KB (0.0%)
mongo_seed_1 | 2018-01-10T00:19:58.332+0000 Failed: error connecting to db server: no reachable servers
mongo_seed_1 | 2018-01-10T00:19:58.332+0000 imported 0 documents
In the mongoimport statement, I have tried using different host names: mongodb_1, 127.0.0.1, localhost.
I have read that if replSet is set it can give this error but I'm not setting replSet unless it's default in which case I don't know how it disable it.
I have also tried mongorestore instead of mongoimport but get the same error.
I have read since link is deprecated in Docker that maybe environmental variables are no longer working. Not sure if that is true or happening here.
Does anyone have ideas?
I ended up removing the Dockerfile, adding the commands in a bash script, then calling the script from the docker-compose file. Used a script rather than one command in the docker-compose file because I'm importing several files thus several commands that are not shown in my example. I needed to use mongo:3.2.6 to make this work. There may be other versions but this one works for sure.
docker-compose.yml
version: '3'
services:
mongodb:
image: mongo:3.2.6
ports:
- 27017:27017
mongo_seed:
image: mongo:3.2.6
links:
- mongodb
volumes:
- ./mongo-seed:/mongo-seed
command:
/mongo-seed/import.sh
/mongo-seed/import.sh
#! /bin/bash
mongoimport --host mongodb --db test --collection census --type json --file /mongo-seed/census.json --jsonArray
这篇关于使用 Docker-Compose 在 MongoDB 上导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!