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