0

As part of a big project, I need add to this project docker, with mongo and a kind of seeder for this mongo, I was following this post How do I seed a mongo database using docker-compose? but everytime when I try to launch I always get the same error Failed: error connecting to DB server: no reachable servers I put all the files for this situation here https://github.com/aasanchez/mongodb-seeder ONLY the referent to this bug... someone have any idea about how to connect, I think my problem is in the connection URL but I don't know how to fix it error

Siyu
  • 11,187
  • 4
  • 43
  • 55
aasanchez
  • 179
  • 1
  • 16

1 Answers1

1

First problem, you url in your CMD is wrong, do like

CMD mongoimport --host mongodb -p 27017 --db reach-engine --collection users --type json --file ./data.json --jsonArray

Second, when launched by docker-compose, the dependency implied by links (which is deprecated) only ensures start up order, not your MongoDB's readiness. So you need to wait a few seconds before mongoimport.

All in all

CMD sleep 10 && mongoimport --host mongodb -p 27017 --db reach-engine --collection users --type json --file ./data.json --jsonArray

Note sleep 10 is only a simple hack, consider use wait-for-it.sh for complexe dependency.

Siyu
  • 11,187
  • 4
  • 43
  • 55