19

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?

4 Answers 4

33

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
Sign up to request clarification or add additional context in comments.

6 Comments

if you wanted to also run another script like mongo --host mongodb /mongo-seed/createuser.js, that just contains a use admin; db.createUser({etc..}... how would you incorporate that into that import.sh file?
I am geeting /usr/local/bin/docker-entrypoint.sh: line 354: /mongo-seed/import.sh: Permission denied .any help
For Permission denied: chmod +x mongo-seed/import.sh
I got the same error that you had after I did what you suggested here. But by removing the version number 3.2.6, everything worked fine
I'm always getting: error connecting to host: could not connect to server: connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed. Even if I'm running the command manually inside the mongodb container !!
|
3

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.

Comments

3

There is a lot of info about (apparently) an "old" way of doing it. Using a bash script didn't look appealing to me either, so I decided to dig deeper and have found a "proper" solution. Here it is:

https://hub.docker.com/_/mongo (section "Initializing a fresh instance")

Also: https://stackoverflow.com/a/42917632/2761550 (this one was surprisingly hard to find using search engines)

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.