Import Data on MongoDB using Docker-Compose
In my case, I put my json file into the data folder
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
You are seeing that Permission denied because you didn't mark the .sh
file as executable. Just run chmod +x file.sh
and then run again the whole docker-compose up.
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