Important Announcement

Everybody is strongly encouraged to switch to the upcoming v1.0.0 release version to prevent unforseen issues upon merge to master.

Instructions here: https://github.com/cytopia/devilbox/pull/416.

Autostarting NodeJS Apps

You can have all of your NodeJS applications spin up automtically as soon as you docker-compose up. This can be achieved by makeing use of pm2 (Node.js Process Manager) and the autostart feature.

See also

Read more about how to add scripts for autostart commands:

Table of Contents

Self-built

Simply add a script ending by .sh to the autostart/ directory that will accomplish this. The following example will make use of pm2 to spin up your NodeJS application.

Assumption

  • Path to your NodeJS project (within the Docker container): /shared/httpd/my-node/src
  • Name of the JS file to startup: index.js

The script

Add the following script to autostart/

autostart/myscript.sh
su -c "cd /shared/httpd/my-node/src; pm2 start index.js" -l devilbox
  • The whole command is wrapped into su to ensure the application will be started as the user devilbox.
  • cd tells it to you enter the directory where index.js can be found
  • And finally pm2 will take care about starting up your javascript file.

Once the Devilbox is running, you can enter the PHP container and verify with pm2 list that everything is running as expected.

Pre-built

Instead of writing multiple scripts for multiple applications, you can also make use of the pre-shipped script that allows you to start unlimitted NodeJS applications via pm2 .

The following script is provided in autostart/run-node-js-projects.sh-example and needs to be copied to a file ending by .sh

host> cd /path/to/devilbox
host> cd autostart
host> cp run-node-js-projects.sh-example run-node-js-projects.sh

In that newly created file, you can simply add the full paths (path inside the Docker containre) of your Javascript files that need to be started. There is already one example which is not commented. Change this to your path and add as many lines as you have projects to startup.

autostart/run-node-js-projects.sh
#!/usr/bin/env bash
#
# This is a generic example to startup your NodeJS projects with
# pm2 (https://github.com/Unitech/pm2)
#
# Important: As everything is run by the root user, you must explicitly direct the
#            commands to the devilbox user.
#



# Add the full paths of your Nodejs projects startup files into this array
# Each project separated by a newline and enclosed in double quotes. (No commas!)
# Paths are internal paths inside the PHP container.
NODE_PROJECTS=(
	#"/shared/httpd/my-rhost/js/index.js"
	#"/shared/httpd/my-node-hello-world/name/run.js"
	#"/shared/httpd/another-node-project/javascript/run.js"
)


# Check if any projects have been defined
if [ ${#NODE_PROJECTS[@]} -eq 0 ]; then
	echo "No projects defined. Exiting."
	exit 0
fi


# This loops over the paths, separates base directory and filename and will run it in the background
# as the user devilbox. There shouldn't be any need to change anything here.
for item in ${NODE_PROJECTS[*]}; do
	NODE_PATH="$( dirname "${item}" )"
	NODE_FILE="$( basename "${item}" )"

	if [ ! -d "${NODE_PATH}" ]; then
		>&2 echo "[Warning], skipping startup, directory does not exist: ${NODE_PATH}"
		continue;
	fi
	if [ ! -f "${NODE_PATH}/${NODE_FILE}" ]; then
		>&2 echo "[Warning], skipping startup, file does not exist: ${NODE_PATH}/${NODE_FILE}"
		continue;
	fi

	echo "su -c \"cd ${NODE_PATH}; pm2 start ${NODE_FILE}\" -l devilbox"
	su -c "cd ${NODE_PATH}; pm2 start ${NODE_FILE}" -l devilbox
done

Reverse proxy NodeJS

If you also want to know how to reverse proxy your NodeJS service and have it available via the web server including HTTPS support have a look at the following links:

Imagine you have started an application within the PHP container that creates a listening port (e.g.: NodeJS). This will now only listen on the PHP container and you would have to adjust the docker-compose.yml definition in order to have that port available outside to your host OS.

Alternatively, there is a simple way to reverse proxy it to the already running web server and even make use of the already available HTTPS feature.