Over the last series of posts I’ve outlined how to build all the components that are needed for a Multi Tenant Node-RED service. What’s missing is a way to automate the spinning up of new instances.
One option would be to do this with Node-RED it’s self, you can drive docker using the node-red-contrib-dockerode, but for this I’ve decided to create a dedicated application.
I’ve written a small Express app that uses dockerode directly and also will populate the Users collection in the MongoDB with the admin password for the editor and then spin up a new instance.
docker.createContainer({ Image: "custom-node-red", name: req.body.appname, Env: [ "VIRTUAL_HOST=" + req.body.appname + "." + settings.rootDomain, "APP_NAME="+ req.body.appname, "MONGO_URL=mongodb://mongodb/nodered" ], AttachStdin: false, AttachStdout: false, AttachStderr: false, HostConfig: { Links: [ "mongodb:mongodb" ] } }) .then(container => { console.log("created"); cont = container; return container.start() }) .then(() => { res.status(201).send({started: true, url: "http://" + req.body.appname + "." + settings.rootDomain}); })
It’s pretty basic but it does just enough to get started. It is being exposed using the same nginx-proxy that exposes the Node-RED instances, so the management interface is available on the manger.docker-pi.local domain. If it was being deployed in a production environment it should probably not be internet facing and should have some basic access control to prevent anybody from being able to stand up a new Node-RED instance.

When the app has completed creating a new instance a link to that instance is displayed.
You can also Start/Stop/Remove the instance as well as streaming the logs via a websocket.

Thanks to Dave Conway-Jones (@ceejay) for help with making my very utilitarian UI look a lot better.
The code for the management app is on github here
One thought on “Managing Multi Tenant Node-RED Instances”