Commands to execute background process in Docker CMD Commands to execute background process in Docker CMD docker docker

Commands to execute background process in Docker CMD


Besides the comments on your question that already pointed out a few things about Docker best practices you could anyway start a background process from within your start.sh script and keep that start.sh script itself in foreground using the nohup command and the ampersand (&). I did not try it with mongod but something like the following in your start.sh script could work:

#!/bin/sh...nohup sh -c mongod --dbpath /test &...


Of course there is also the official Docker documentation of how to start multiple services, again using a script file not the CMD. The docker documentation also states how to use supervisord as a process manager:

FROM ubuntu:latestRUN apt-get update && apt-get install -y supervisorRUN mkdir -p /var/log/supervisorCOPY supervisord.conf /etc/supervisor/conf.d/supervisord.confCOPY my_first_process my_first_processCOPY my_second_process my_second_processCMD ["/usr/bin/supervisord"]

If it is an option you could use a phusion base images which allows running multiple processes in one container. Thus you can run system services such as cron or other processes using a service supervisor like runit.

More information about whether or not a phusion base image is a good choice in your use case can be found here

A ruby focused description of how to avoid running more processes in your container except for your app you can find here. The elaborations are too detailed to repeat on SO.