使用Docker Compose在MongoDB上导入数据
有许多网站展示了如何使用下面的docker compose方法将数据导入mongodb,但我的网站没有找到db服务器 想知道是否有什么东西改变了,使它不再有效 docker compose.yml使用Docker Compose在MongoDB上导入数据,mongodb,docker,docker-compose,dockerfile,Mongodb,Docker,Docker Compose,Dockerfile,有许多网站展示了如何使用下面的docker compose方法将数据导入mongodb,但我的网站没有找到db服务器 想知道是否有什么东西改变了,使它不再有效 docker compose.yml version: '3' services: mongodb: image: mongo command: mongod --smallfiles ports: - 27017 mongo_seed: build: mongo-seed/.
version: '3'
services:
mongodb:
image: mongo
command: mongod --smallfiles
ports:
- 27017
mongo_seed:
build: mongo-seed/.
links:
- mongodb
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/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"
}
]
docker编写后出错
在mongoimport语句中,我尝试使用不同的主机名:mongodb_1、127.0.0.1、localhost
我已经读到,如果设置了replSet,可能会出现这个错误,但我不会设置replSet,除非它是默认值,在这种情况下,我不知道它如何禁用它
我也尝试了mongorestore而不是mongoimport,但得到了相同的错误
自从link在Docker中被弃用后,我就知道环境变量可能不再起作用了。不确定这是真的还是发生在这里
有人有想法吗?我最终删除了docker文件,在bash脚本中添加了命令,然后从docker compose文件中调用了脚本。在docker compose文件中使用了一个脚本而不是一个命令,因为我要导入多个文件,因此在我的示例中没有显示多个命令。我需要使用mongo:3.2.6来实现这一点。可能还有其他版本,但这一个肯定有效 docker compose.yml
version: '3'
services:
mongodb:
image: mongo
command: mongod --smallfiles
ports:
- 27017
mongo_seed:
build: mongo-seed/.
links:
- mongodb
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
您看到该权限被拒绝,因为您没有将
.sh
文件标记为可执行文件。只需运行chmod+x file.sh
,然后再次运行整个docker compose。在我的例子中,我将json文件放入数据文件夹
mongoimport:
image: library/mongo:latest
container_name: my-import
volumes:
- ./data/orders.json:/src/data/orders.json
command: mongoimport --host mongodb --db my-mongo --collection orders --file /src/data/orders.json
mongodb:
image: library/mongo:latest
container_name: my-mongo
ports:
- 27017:27017
depends_on:
- mongoimport
restart: always
如果您还想运行另一个脚本,比如mongo——host mongodb/mongo seed/createuser.js,它只包含一个use admin;db.createUser({etc..}…如何将其合并到import.sh文件中?我正在搜索
/usr/local/bin/docker-entrypoint.sh:第354行:/mongo seed/import.sh:权限被拒绝
。有任何帮助吗