0

I know there is some sort of trick like use a separate container to seed the real container like in this answer. I like this approach, but I'd like to know if I can do this as soon as container start. Simply creating one Dockerfile. Using previous answer as starting point I tried various solution, this is one:

FROM mongo:3.0.15

COPY ./init.json /init.json

CMD (mongod &) && (sleep 70 && mongoimport -v --db reach-engine --collection MyDummyCollection --type json --file /init.json --jsonArray)

EXPOSE 27017

Alas this solutio it's not elegant, nor it's working cause: since mongod is in background, as soon mongoimport completes the container stops and so even if import is succcessfull it completly unusefull.

And first of all: sleep 70 is awful since it's not clear what happen if mongod takes more than 70 seconds.

Ideas?

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110
  • You would need to use docker compose, – Raman Sailopal Aug 29 '17 at 09:59
  • I wouldn't. I would do anything inside a single Dockerfile (if it's possible)! – BAD_SEED Aug 29 '17 at 10:02
  • It is quite clear what happens if `mongod` takes more than 70 seconds to start up - `mongoimport` will give up trying to connect to the db if there is nobody listening on the port. One of the option you have is plain old `cp` of the database files: https://docs.mongodb.com/manual/core/backups/#back-up-with-cp-or-rsync before starting mongod. `mongoimport` it once, stop the db, copy files to the host, add `COPY` to the dockerfile. Unless you change `init.json` often. – Alex Blex Aug 29 '17 at 10:08

1 Answers1

1

I prefer the separate container approach always, but that doesn't mean this is not achievable. Your issue is that you are launching mongod in background and then running script. Which you should be running your script in background and then start mongod as main process.

There is a nice shell script at https://github.com/vishnubob/wait-for-it , Which can do all the waiting part for you. But you need netcat for that

So here is an update Dockerfile

Dockerfile

FROM mongo:3.0.15
RUN apt-get update -y && apt-get install -y netcat && apt-get clean
COPY ./init.json /init.json
COPY ./init.sh /init.sh
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /wait-for-it.sh
RUN chmod +x /init.sh /wait-for-it.sh
CMD /init.sh
EXPOSE 27017

init.sh

#!/bin/sh

bash -c "/wait-for-it.sh 127.0.0.1:3306 -t 120 -- mongoimport -v --db reach-engine --collection MyDummyCollection --type json --file /init.json" &
exec mongod

Now when you run the container, you will see below output

2017-08-29T11:54:33.357+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2017-08-29T11:54:34.286+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:33070 #1 (1 connection now open)
2017-08-29T11:54:34.287+0000 I NETWORK  [conn1] end connection 127.0.0.1:33070 (0 connections now open)
wait-for-it.sh: 127.0.0.1:27017 is available after 1 seconds
2017-08-29T11:54:34.295+0000    filesize: 556 bytes
2017-08-29T11:54:34.295+0000    using fields:
2017-08-29T11:54:34.296+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:33072 #2 (1 connection now open)
2017-08-29T11:54:34.297+0000    connected to: localhost
2017-08-29T11:54:34.297+0000    ns: reach-engine.MyDummyCollection
2017-08-29T11:54:34.297+0000    connected to node type: standalone
2017-08-29T11:54:34.297+0000    using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-08-29T11:54:34.297+0000    using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-08-29T11:54:34.298+0000 I INDEX    [conn2] allocating new ns file /data/db/reach-engine.ns, filling with zeroes...
2017-08-29T11:54:34.338+0000 I STORAGE  [FileAllocator] allocating new datafile /data/db/reach-engine.0, filling with zeroes...
2017-08-29T11:54:34.340+0000 I STORAGE  [FileAllocator] done allocating datafile /data/db/reach-engine.0, size: 64MB,  took 0.001 secs
2017-08-29T11:54:34.342+0000    imported 4 documents
2017-08-29T11:54:34.342+0000 I NETWORK  [conn2] end connection 127.0.0.1:33072 (0 connections now open)

As you can see there not even 1 sec delay between server getting up and records being inserted

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265