Search code examples
node.jsmongodbimportdockerfilehost

Dockerfile, Nodejs, mongodb import


I am doing my Dockerfile, and when I want to import my bbd.json inside mongodb I have an error. I have searched about this error, and I found this : add --host localhost or add --host=localhost (or 127.0.0.0) I have tried both but none of them is working. Here my Dockerfile:

FROM node:argon

RUN apt-get update
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
#RUN echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
RUN apt-get update
RUN apt-get -y install vim-tiny
#RUN vim /etc/apt/sources.list.d/mongodb-org-3.2.list
RUN echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
RUN apt-get update
RUN apt-get install -y mongodb-org=3.2.10 mongodb-org-server=3.2.10 mongodb-org-shell=3.2.10 mongodb-org-mongos=3.2.10 mongodb-org-tools=3.2.10
RUN npm install mongoose
RUN npm install body-parser

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

RUN mongod&
RUN mkdir /usr/test
RUN mkdir -p /data/db
RUN mongod --dbpath /usr/test --fork --syslog
RUN mongoimport --db bdd --collection users --file bdd.json
RUN cp /usr/test/* .

EXPOSE 3000
CMD ["npm", "start"]

And here the error :

Step 17/19 : RUN mongoimport --host localhost --db apirest --collection users --file bdd.json
 ---> Running in 028f35e6e2ab
2017-04-03T11:40:57.406+0000    [........................] apirest.users        0B/23.9KB (0.0%)
2017-04-03T11:40:57.910+0000    Failed: error connecting to db server: no reachable servers0.0%)
2017-04-03T11:40:57.911+0000    imported 0 documents
The command '/bin/sh -c mongoimport --host localhost --db apirest --collection users --file bdd.json' returned a non-zero code: 1

Have you any ideas ? Thank you.


Solution

  • You were right, I had to add RUN mongod& but the error was still here after. So finally, I changed this two lines :

    RUN mongod --dbpath /usr/test --fork --syslog
    RUN mongoimport --db bdd --collection users --file bdd.json
    

    In one :

    RUN mongod --dbpath /usr/test --fork --syslog && mongoimport --db apirest --collection users --file bdd.json --jsonArray
    

    And it works perfectly ! Thank you everybody !