# How to package node-red subflows into a dedicated Node-Red Docker Container?

**URL:** https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745
**Category:** General
**Tags:** docker
**Created:** [25 April 2022 09:29 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745 "2022-04-25T09:29:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Shan](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shan](https://discourse.nodered.org/u/Shan)
#### Post date: [25 April 2022 09:29 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745/1 "2022-04-25T09:29:04Z")

</div>

I have created a subflow for the standard `mqtt in` and `mqtt out` nodes which sets the broker address accordingly (docker container names of the MQTT broker) and is made available for the use in the node pallete.

The node pallete looks as follows:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c218f1ba94cae3edaa7b3a0fffdcdcd55db0a13e.png)

The subflow is very simple, it is just a wrapper around the `mqtt in` and `mqtt out` nodes but the mqtt properties are configured to the broker address instead of `localhost`

**Internal MQTT In Node**

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08b73293cbce14d1d1d486faddc3bdf1b3109021.png)

**Internal MQTT Out Node**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f62ac9642bd53853092132cf8539309c7e1a91c.png)

## Documentation for Subflows

Based on the documentation provided by [Packaging a Subflow as a module : Node-RED](https://nodered.org/docs/creating-nodes/subflow-modules)  
I did the following steps:

1. export the subflow as JSON file (example only of **Internal MQTT Out Node** )

2. As the documentation suggests, I packed the dedicate nodes namely, `"type": mqtt-out` and `"type": "mqtt-broker` into to `"type": "subflow"` under an array called `flow: []`

**Altered JSON File after Step 2**

```auto
[
    {
        "id": "be86292c6d08f0da",
        "type": "subflow",
        "name": "Internal MQTT Out",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 340,
                "y": 140,
                "wires": [
                    {
                        "id": "b122f18e514d14f9"
                    }
                ]
            }
        ],
        "out": [],
        "env": [],
        "meta": {},
        "color": "#C0DEED",
        "icon": "node-red/bridge.svg",
        "flow": [
            {
                "id": "b122f18e514d14f9",
                "type": "mqtt out",
                "z": "be86292c6d08f0da",
                "name": "Internal MQTT Broker",
                "topic": "",
                "qos": "",
                "retain": "",
                "respTopic": "",
                "contentType": "",
                "userProps": "",
                "correl": "",
                "expiry": "",
                "broker": "dbd22c0713b05ef6",
                "x": 560,
                "y": 140,
                "wires": []
            },
            {
                "id": "dbd22c0713b05ef6",
                "type": "mqtt-broker",
                "z": "be86292c6d08f0da",
                "name": "Internal MQTT Broker",
                "broker": "internal-mqtt-broker",
                "port": "1883",
                "clientid": "node-red-mqtt-internal-out",
                "autoConnect": true,
                "usetls": false,
                "protocolVersion": "4",
                "keepalive": "60",
                "cleansession": true,
                "birthTopic": "",
                "birthQos": "0",
                "birthPayload": "",
                "birthMsg": {},
                "closeTopic": "",
                "closeQos": "0",
                "closePayload": "",
                "closeMsg": {},
                "willTopic": "",
                "willQos": "0",
                "willPayload": "",
                "willMsg": {},
                "sessionExpiry": ""
            }
        ]
    }
]

```

## Project file structure

```bash
.
├── Dockerfile
├── README.md
├── package.json
└── subflows
    └── mqtt
        ├── custom-mqtt-internal-in.js
        ├── custom-mqtt-internal-in.json
        ├── custom-mqtt-internal-out.js
        └── custom-mqtt-internal-out.json

```

### `package.json`

```auto
{
    "name": "node-red-custom-subflow",
    "version": "0.1.0",
    "description": "Node-RED package developed with subflows",
    "dependencies": {
        "node-red": ">=2.2.0"
    },
    "keywords": ["node-red", "mqtt"],
    "node-red": {
        "version": ">=2.2.0",
        "nodes": {
            "custom-mqtt-internal-in": "subflows/mqtt/custom-mqtt-internal-in.js",
            "custom-mqtt-internal-out": "subflows/mqtt/custom-mqtt-internal-out.js",
        }
    },
    "scripts": {
        "start": "node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS --userDir=/data"
    }
}

```

## Question

I wish to create a custom node-red Docker image with these subflows available in the node pallete. I am unable to figure out where should the `subflows` directory copied in the Container image to make it available for the final image?

### `Dockerfile`

```docker
FROM nodered/node-red:2.2.2

WORKDIR /usr/src/node-red

## Maybe already create a directory in `node_modules`?
RUN mkdir -p /usr/src/node-red/node_modules/node-red-custom-subflow/subflows/mqtt

COPY subflows/* /usr/src/node-red/node_modules/node-red-custom-subflows/subflows/mqtt

## Do I need to copy it also in /data?
COPY subflows /data

COPY package.json .

RUN npm install --unsafe-perm \
                --no-update-notifier \
                --no-fund \
                --only=production

CMD ["npm", "start"]

```

I tried building this image using `docker build -t custom-node-red-subflow:latest .` and `docker run -p 1990:1880 custom-node-red-subflow:latest` however the nodes are not available in the palette.

Help would be appreciated.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 April 2022 12:56 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745/2 "2022-04-25T12:56:13Z")

</div>

> [@Shan](#):
>
> Altered JSON File after Step 2

Your subflow JSON is not quite right - you still have the `subflow` node wrapped in an array.

It should be:

```auto
{
    "id": "be86292c6d08f0da",
    "type": "subflow",
    ...
    "flow": [
        { ... },
        { ... },
        { ... }
    ]
}

```

---

<div class="post-metadata">

### Author: ![Shan](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shan](https://discourse.nodered.org/u/Shan)
#### Post date: [25 April 2022 13:01 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745/3 "2022-04-25T13:01:40Z")

</div>

Ah! That was not clear to me. I will keep you updated on the post. Any chance the docs team might be open for a better Documentation of the development workflow for subflows?

I have been able to figure out now how to provide subflows in Docker, (if you might not know I also posted a tutorial [[Tutorial] How to create a custom Node-Red Docker Image and conduct Integration Testing using Docker-Compose](https://discourse.nodered.org/t/tutorial-how-to-create-a-custom-node-red-docker-image-and-conduct-integration-testing-using-docker-compose/61059) previously)

I could do a simple Subflow and provide step by step implementation and maybe it might make things a tad-bit clearer.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [25 April 2022 13:03 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745/4 "2022-04-25T13:03:30Z")

</div>

As an open source project, we are reliant on contributions from the community to help improve things. There is no dedicated docs team. PRs are always welcome. The page in question is here:

> <https://github.com/node-red/node-red.github.io/blob/master/docs/creating-nodes/packaging-subflows.md>

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [9 May 2022 13:04 UTC](https://discourse.nodered.org/t/how-to-package-node-red-subflows-into-a-dedicated-node-red-docker-container/61745/5 "2022-05-09T13:04:21Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
