How to Create a Custom Node-RED Docker Container in 4 Minutes

Jon Stopple
March 26, 2024

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:

docker run -it -p 1880:1880 -v node_red_data:/data --name mynodered nodered/node-red

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:

#1. Create your Dockerfile

Pick a directory and create a Dockerfile that looks like this.

# Dockerfile

FROM nodered/node-red

WORKDIR /data
COPY package.json /data
RUN npm install --unsafe-perm --no-update-notifier --no-fund --only=production
WORKDIR /usr/src/node-red

COPY settings.js /data/settings.js
COPY flows.json /data/flows.json

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.

#2. Create your Docker Compose

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.

version: "3.7"

name: my-stack

services:
  node-red:    
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "1880:1880"
    networks:
      - node-red-net
    volumes:
      - node-red-data:/data

volumes:
  node-red-data:

networks:
  node-red-net:

#3. Prep your Files

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.

#4. Build and Run

From the directory with your docker-compose.yaml, run the following:

docker-compose up
# use docker-compose -f {custom_file_name.yaml} for custom file names

Docker view after running docker-compose up.
Custom flow runs immediately with pre-installed packages.

Simple as that.