IMD SolutionComputing LibraryShop main store

How-to

Rebooting the host PC from Node-RED running in a container

Edge computing and IIoT Published 7 min read

Short answer

Write a request file from Node-RED into a single bind-mounted directory; a host-side systemd path unit consumes it and runs one fixed reboot command. The container never holds a password, a shell, or the Docker socket — the worst a compromised container can do is force one reboot. Enable the units, test from the host first, then prove the reboot with uptime arithmetic.

On this page
  1. Why the container should not log in to the host
  2. The pattern: one request file, one fixed action
  3. Host side: a watcher unit and a oneshot service
  4. Container side: one mount, one write
  5. The test that proves one reboot
  6. MQTT trigger as the alternative
  7. Limits worth stating
  8. Questions asked

A Node-RED flow inside a Docker container cannot run anything on its own host: the sandbox ends at the container boundary, and docker exec only starts processes inside the container itself. The workaround found in older how-tos — an SSH session from the container to the host, driven by the root password — hands the container a root shell and is the design to retire. The replacement is a host-side systemd watcher: the flow writes one file into one bind-mounted directory, the watcher consumes it, and the host runs one fixed command, systemctl reboot. No password, no key and no shell ever lives in the container. Below are the two unit files, the mount, the Node-RED write, and the test that proves the host rebooted exactly once.

Why the container should not log in to the host #

A container has no route to a host shell: docker exec starts a new process inside the running container, so anything that must touch the host has to run on the host itself. The SSH workaround crosses that boundary with the host's root credential, and getting it through costs you the checks that make SSH safe: host-key verification gets switched off so the container accepts whatever answers, the password travels as a command-line argument into shell history and error output, and the flow ends up storing or relaying that password. Whoever controls the container then controls the host as root — a far bigger grant than a reboot. The published sshpass-based SSH approach is exactly that pattern, built for a test rig. The decision: give the container no credential, no shell and no network path to the host — only a writable directory.

The pattern: one request file, one fixed action #

Node-RED writes a single file into a single directory bind-mounted into the container. On the host, a systemd path unit watches that directory and starts a oneshot service whenever it holds at least one file; the service deletes the request, then reboots. Three properties make this safe enough to ship. The trigger is the file's presence, not its contents: nothing in the file is read or executed. The service consumes the file before rebooting, so the watcher re-arms empty and one file is one reboot. The only action in the system is the command hard-coded into the unit file. The accepted tradeoff, stated plainly: a compromised container can force reboots — a denial of service — but it cannot run commands, read host files or reach credentials. That is a reasonable bargain for a reboot; it would not be for anything destructive.

Host side: a watcher unit and a oneshot service #

The steps below use /srv/host-requests as the request directory and host-reboot-request as the unit name; both are illustrative, so keep whatever you choose consistent across the files.

  1. Create the directory on the host and keep nothing but the request file in it:
mkdir -p /srv/host-requests

Then give the directory to the UID your Node-RED process uses inside the container: run id in the container and apply that UID and group with a chown on the host. If the ownership is wrong, the write from Node-RED fails with a permission error.

  1. Create /etc/systemd/system/host-reboot-request.service — a oneshot that removes the request first, then reboots:
[Unit]
Description=Consume one Node-RED reboot request

[Service]
Type=oneshot
ExecStartPre=/bin/rm -f /srv/host-requests/reboot.request
ExecStart=/usr/bin/systemctl reboot

On distributions without a merged /usr, systemctl lives at /bin/systemctl instead.

  1. Create /etc/systemd/system/host-reboot-request.path — a watcher that fires while the directory holds at least one file, with Unit= naming the service to start:
[Unit]
Description=Watch the Node-RED request directory

[Path]
DirectoryNotEmpty=/srv/host-requests
Unit=host-reboot-request.service

[Install]
WantedBy=multi-user.target

The delete step in the service is what re-arms the watcher: after the request is removed the directory is empty, so a consumed request cannot fire again.

  1. Load and start the watcher:
systemctl daemon-reload
systemctl enable --now host-reboot-request.path
  1. Test the host half on its own:
touch /srv/host-requests/reboot.request

The host must shut down and come back, and the directory must be empty afterwards. Rebooting stops whatever this PC serves — broker, historian, SCADA node — so run this first test in a maintenance window. If the host does not reboot here, fix the units before touching Node-RED.

Container side: one mount, one write #

The container needs exactly one extra mount and nothing else. A mount is fixed when the container is created, so recreate the container with the added flag rather than expecting a running one to pick it up. On the docker command line that is -v /srv/host-requests:/host-requests; in a compose file:

volumes:
  - "/srv/host-requests:/host-requests"

Mount this one directory and nothing more — not /, not /etc, and never the Docker socket (the FAQ below covers why the socket is the worst possible mount).

In Node-RED, drive the trigger with an inject node feeding a write file node: set the filename to /host-requests/reboot.request, the action to overwrite, and the payload to one short line such as reboot — the host never reads it. An exec node performing that same single write works identically. Remember Node-RED sees the container-side path /host-requests, not /srv/host-requests.

The test that proves one reboot #

Run the flow once and watch the host drop and return. Then prove what happened with numbers instead of impressions:

  1. Before the test, read the first field of /proc/uptime, which is seconds since boot.
  2. Fire the inject node; the host shuts down and returns.
  3. Read /proc/uptime again, then once more after five minutes.

Worked example (illustrative numbers — read your own from /proc/uptime): before the trigger, /proc/uptime prints 5231.42 as its first field, which is 5231.42 s ÷ 3600 s/h = 1.45 h, about 1 h 27 min of uptime. After the host returns, the same file reads 42.10. Uptime fell by 5231.42 s − 42.10 s = 5189.32 s, and that counter only resets when the machine boots — so the host rebooted. Five minutes later it reads 342.55: an increase of 342.55 s − 42.10 s = 300.45 s over five minutes with no second drop, and ls /srv/host-requests shows an empty directory because the service consumed the request. On a host with a persistent journal, journalctl -u host-reboot-request.service -b -1 shows the previous boot's single service start. Empty directory, uptime falling exactly once, then climbing: the trigger fired exactly once.

MQTT trigger as the alternative #

Use MQTT instead of the file when the request may come from another machine on an existing broker. The host runs one small service that stays subscribed to a single topic and reboots on the first message:

[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/bin/mosquitto_sub -h 127.0.0.1 -p 8883 -t edge/pc1/cmd/reboot -C 1 && /usr/bin/systemctl reboot'

Type=simple keeps the service attached while mosquitto_sub waits; -C 1 makes the client exit after one message, which runs the reboot. Add the usual [Unit] and [Install] sections, enable the unit, and it re-subscribes at every boot. The topic edge/pc1/cmd/reboot and the broker address are illustrative — use your own naming. The security now lives in the broker: restrict publishing to that exact topic to the one client allowed to request a reboot, and run TLS as you would for any plant broker — the broker-side setup is the same one described for a secure Mosquitto MQTT broker with Node-RED. Decision: default to the file trigger, which works with no broker and no network; take MQTT when the broker is already your OT data path or the request must originate off-box.

Limits worth stating #

  • Never put the write in a retry loop. A second write that survives shutdown boots into a second reboot. Gate the trigger with a manual inject or latched flow logic.
  • The mount is the permission. Anyone who can create a file in the request directory can force reboots, so mount it only into containers that need it and keep nothing else in the directory.
  • One directory, one action. A second action gets its own directory, path unit and service with its own fixed command — never a command passed through the mount.
  • If Node-RED runs directly on the host there is nothing to tunnel: the flow can call systemctl reboot itself — see installing Node-RED natively on a Linux edge computer — and the effort goes into the editor's own login and HTTPS settings instead.
  • The watcher survives reboots because the unit is enabled; if it ever stops firing, systemctl status host-reboot-request.path and the journal are the first two places to look.

Questions asked

Does the content of the request file matter?

No. The path unit fires on the file's presence, and the service deletes the file without reading it, so nothing inside the file is ever executed. Write one short line so a person can still tell what the file was. If you want a soft guard, have the flow write a fixed string and add a check for it before the reboot, but treat the mounted directory itself as the real permission: anyone who can create files there can force a reboot.

What happens if the flow sends a second request while the host is shutting down?

A write that lands during shutdown can survive into the next boot, and the watcher fires again, so the host reboots a second time. The service removes the file before rebooting, which is why requests written early usually get consumed, but that ordering is not a guarantee. Gate the trigger in the flow: send it once from a manual inject or latched logic, and never from an automatic retry loop.

Why is mounting the Docker socket worse than mounting the request directory?

The Docker socket is a root-equivalent interface on the host: anything that can talk to it can start a new container with the host's root filesystem mounted inside, which is a full escape from the container sandbox. The request-file mount grants exactly one capability: creating a file in an empty directory that triggers one hard-coded reboot. One is code execution on the host; the other is at most a forced reboot.

Can the same pattern restart a service or run another host action?

Yes, but keep one action per pair of units. Add a second directory and a second path unit whose service runs that other fixed command, and mount only the matching directory into the container that needs it. Do not pass a command name or arguments through the mount — the moment file contents reach a shell, the design degrades into the remote-execution problem this pattern exists to avoid.

Keep reading

  • How-to

    Run Python and Node-RED beside PLC code on an edge PC

    Keep the IEC 61131-3 program in the controller runtime and run Python and Node-RED as separate Docker containers on the same edge PC. Exchange data over one MQTT interface with a fixed publish period, and declare data stale after three missed periods. One container per workload, and nothing from Python or Node-RED inside the control project.

    Edge computing and IIoT

  • Troubleshooting

    Fixing a Docker shared library error on an edge PC

    A container on an edge PC that exits with an error while loading shared libraries was built for a different controller firmware generation than the one installed. Pull the image revision rebuilt for your firmware, recreate the container, and verify with ldd, the linker cache and a restart-count check. Never symlink across major library versions: on a machine driving physical outputs, a mismatched library produces wrong I/O data instead of a clean error.

    Edge computing and IIoT

  • How-to

    Install licensed software on an industrial edge PC

    Install vendor-licensed container software on an industrial edge PC by passing four gates in order: a purchased license with the installer obtained only from the vendor, a verified Docker engine, an installer build that matches the CPU, and a proven open-internet path before the pull. Vendor guidance budgets the install command at 30 minutes to 1 hour; at a 5 Mbit/s link a 2 GB payload alone needs about 53 of them.

    Edge computing and IIoT