Docker Community Forums

Share and learn in the Docker community.

Import data on mongodb from docker-compose

Hi everyone :slight_smile: ,

I have a little problem with docker-compose.
I want to import data in my mongodb database with a docker-compose but i donโ€™t found a solution. This is an idea that i get somewhere but that does not work. If someone see a better solution or just fix this solution , i take :).

Thank you in advance :smiley:

./docker-compose.yml

db:
  image: mongo
  ports:
   - 27018:27017

dbseed:
  build: ./dbseed
  links:
    - db

app:
  build: .
  links:
   - db:db
  ports:
   -8080:8080
   -8181:8181

dbseed/new_climat_final.json

[
  {
    "name": "Joe Smith",
    "email": "jsmith@gmail.com",
    "age": 40,
    "admin": false
  },
  {
    "name": "Jen Ford",
    "email": "jford@gmail.com",
    "age": 45,
    "admin": true
  }
]

dbseed/Dockerfile

FROM mongo

COPY new_climat_final.json /new_climat_final.json

CMD mongoimport --host db --db hpApiDev --collection coll --type json --file /new_climat_final.json --jsonArray

No error but no data in my mongodb database :confused:

As i know, you only need to use mongorestore,

ex : mongorestore -h mongodb:27017 --db db_name /path_to_file/db_name

which contain json and bson.