No messing around, let’s dive in. I’m assuming you know the basics of Node-RED and what Docker is or you wouldn’t be here.
Note that Node-RED has a good guide on all things Node-RED and Docker, but I found the extra information made it more confusing to follow how to generate a custom container with your packages, settings, and flows ready to go upon build. So, I’ll aim to make that part super simple in this guide.
Creating a generic Node-RED container with a volume is simple but not quite what we want:
We want to deploy a working flow with custom settings immediately.
Let’s see how in four simple steps and code that you can actually copy and paste without modification:
Pick a directory and create a Dockerfile that looks like this.
As you can see, this uses the base Node-RED image, copies in the package.json file, installs the packages, then copies your settings and flows over into the image. Easy enough.
This step is just as easy. Create your docker_compose.yaml file and paste the code below.
The ports go [HOST]:[CONTAINER]. Both are configurable for Node-RED but the easiest way to run multiple Node-RED instances is to leave the container port as 1880 (deafult per the settings.js file) and adjust the host port from 1880 as necessary. This way you can access the instance at http://{HOST_IP}:{HOST_PORT}.
If you have multiple Node-RED configurations that will run on this server you’d want to provide a unique name for each.
Of course, this is the start of a docker compose that you can add more services to, like a Mosquitto MQTT broker, InfluxDB , Grafana, Redis, and so on.
I’m assuming you have a predefined Node-RED configuration you’re copying here. If you already know where your package.json, settings.js and flows.json files are, great. Just copy them into the same folder as your docker-compose.yaml and Dockerfile.
If you need to locate them from a local instance of Node-RED, navigate to C:\Users\{PC_USER}\.node-red (if a Windows user). You’ll find your files here. Copy them into your docker folder.
From the directory with your docker-compose.yaml, run the following:
Simple as that.