Cannot run nodejs app and mongo within a docker container

I'm setting up a container with the following Dockerfile

# Start with project/baseline
FROM project/baseline    # => image with mongo / nodejs / sailsjs

# Create folder that will contain all the sources
RUN mkdir -p /var/project

# Load the configuration file and the deployment script
ADD init.sh /var/project/init.sh
ADD src/ /var/project/   # src contains a list of folder, each one being a sails app

# Compile the sources / run the services / run mongodb
CMD /var/project/init.sh

The init.sh script is called when the container runs. It should start a couple of webapp and mongodb.

#!/bin/bash
PROJECT_PATH=/var/project

# Start mongodb
function start_mongo {
  mongod --fork --logpath /var/log/mongodb.log  # attempt to have mongo running in daemon
}

# Start services
function start {
  for service in $(ls);do
    cd $PROJECT_PATH/$service
    npm start  # Runs sails lift on each service
   done
}

# start mongodb
start_mongo

# start web applications defined in /var/project
start

Basically, there is a couple of nodejs (sailsjs) application in /var/project.
When I run the container, I got the following message:

$ sudo docker run -t -i projects/test about to fork child process, waiting until server is ready for connections. forked process: 10

and then it remains stuck.

How can mongo and the sails processes can be started and the container to remain in a running state ?

UPDATE

I now use this supervisord.conf file

[supervisord]
nodaemon=false

[program:mongodb]
command=/usr/bin/mongod

[program:process1]
command=/bin/bash "cd /var/project/service1 && node app.js"

[program:process2]
command=/bin/bash "cd /var/project/service2 && node app.js"

it is called in the Dockerfile like:

# run the applications (mongodb + project related services)
CMD ["/usr/bin/supervisord"]

As my services are dependent upon mongo starting correctly, supervisord does not wait that long and the services are not started then. Any idea to solve that ?
By the way, it that a so best practice to use mongo in the same container ?

UPDATE 2

I went back to a service.sh script that is called when the container is running. I know this is not clean (but I'll say it's temporary so I can fix the pb I have in supervisor), but I'm doing the following:

  • run nohup mongod &
  • wait 60 sec
  • run my node (forever) processes

The thing is, the container exit right after the forever processes are ran... how can it be kept active ?

If you want to cleanly start multiple services inside a container, one option is to use a process supervisor of some sort. One option is documented here, in the official Docker documentation.

I've done something similar using runit. You can see my base runit image here, and a multi-service application image using that here.