IMD SolutionComputing LibraryShop main store

Explainer

Why Node-RED in Docker cannot run a Windows shutdown script

Edge computing and IIoT Published 7 min read

Short answer

A Node-RED exec node in a Docker container launches processes inside Linux, so a Windows .bat file can never execute from it. Route the shutdown request through Windows OpenSSH with a restricted management account, and transfer ownership of the whole stop sequence to Windows before the Node-RED container exits.

On this page
  1. Why can't the exec node run a .bat file from inside the container?
  2. How does a shutdown request get from the container onto the Windows host?
  3. How should Windows authenticate and authorize the shutdown request?
  4. Who stops the Node-RED container when Windows shuts down?
  5. Should you install Node-RED natively on Windows instead?
  6. How do you test the sequence before trusting it?
  7. Questions asked

A Node-RED exec node running in a Docker container launches its command inside the container's Linux environment, never on the Windows host, so your .bat shutdown script cannot execute from the flow. The request has to cross the container boundary into Windows, and the cleanest channel is Windows OpenSSH with a restricted management account that is allowed to run exactly that script.

Why can't the exec node run a .bat file from inside the container? #

The exec node runs a command as if you were on the command line, and the command line it reaches is the one inside the Linux container. A .bat file needs the Windows command interpreter, which does not exist in that environment, and a host-mounted directory only shares files, not the ability to launch a Windows process.

Node-RED's own Docker documentation describes the container as its own system with its own /data directory and command line, and the container boundary is exactly that: a separate userspace with its own process table. When the flow calls the exec node, the process starts in Linux with the container's file system and permissions. Two things follow. First, a script that works when you double-click it on the Windows desktop proves nothing about the container path. Second, the same boundary applies to any Linux-side script: a container can run its own shell commands freely, and the working question is always which operating system will interpret the command.

If your target is a Linux host instead of Windows, the picture is different and we cover it in the article on rebooting the host PC from Node-RED running in a container, where the container and the host share the Linux kernel and a single well-chosen mechanism can reach it. Windows adds a hard boundary that that approach does not have.

How does a shutdown request get from the container onto the Windows host? #

Give Windows a command channel that the container can call: enable the Windows OpenSSH server on the host, connect from inside the container, and have Windows launch the script in its own environment. The flow then sends a request; Windows owns the execution.

Modern Windows ships an OpenSSH server as an optional feature, so the host-side listener is a documented Windows component rather than a third-party agent. The path becomes:

  1. HMI or trigger event reaches the Node-RED flow.
  2. The flow builds a fixed command, for example powershell -File C:\ops\safe-shutdown.ps1, and passes it to the exec node.
  3. The exec node runs an SSH client inside the container toward the Windows management endpoint.
  4. Windows authenticates the key, launches the script with the configured account, and the script stops Docker and its containers before shutting the PC down.

Before you debug anything else, prove the network path from inside the container, using the container's own network context. A loopback address inside the container refers to the container itself, so pick an address or hostname that resolves to the Windows host from that container. A connection that times out points to routing, firewall policy or a missing listener; a connection that is refused points to the listener port. Test this from a shell inside the running container, not from the Windows desktop, because the desktop can take a different network path than the container does.

For the container-side mechanics of building that command, the page on how the LabVIEW System Exec node launches Bash scripts directly shows the same principle in another tool: the exec-style node always executes in its own process environment.

How should Windows authenticate and authorize the shutdown request? #

Use key-based authentication for a dedicated management account, and authorize that account for exactly one operation: launching the approved shutdown script. An accepted SSH key proves identity; it grants nothing by itself.

Authentication and authorization are two separate checks. The key identifies the account Windows will run the command as; the account's effective rights decide what that command may do, such as stopping Docker, controlling services or shutting the system down. Worked example of the check, with example values you replace with your own:

  1. From a shell inside the container, run ssh with the shutdown account name, the Windows host address and whoami as the remote command. If the reply is the expected account name, the key, the listener and the path all work.
  2. Run the same ssh call with powershell -File C:\ops\safe-shutdown.ps1 -WhatIf as the remote command, against a test host. Windows executes the script locally, and any failure now is an authorization or script problem, not a network one.
  3. Only after both succeed, point the Node-RED exec node at the same fixed command and test from the Node-RED runtime context, not only from an interactive shell.

Keep the private key out of the image you distribute and treat it as a deployment secret. Restrict the account to the approved function where the deployment permits it, keep the script path fixed so an HMI field can never carry an arbitrary command, and gate the flow trigger behind application-level authorization and an explicit operator confirmation. Shutting the PC down is a management action, not a substitute for placing controlled equipment in its required operating state first.

Who stops the Node-RED container when Windows shuts down? #

Windows must own the stop sequence, because once the Node-RED container exits, the flow cannot send another command, hold the SSH session open or report the result. Transfer ownership to Windows before anything stops that container.

Your .bat already has the right shape: it closes Docker and its containers, then shuts the PC down. Keep that order, and consider a .ps1 PowerShell script instead for clearer error handling and status output — changing the extension does not change the execution boundary, since Windows still has to launch it. The sequence inside Windows should be: record that the request was accepted, stop containers with a defined grace period, verify the Docker backend is down, then command the shutdown. Windows shutdown itself signals the Docker backend and asks containers to stop gracefully, but a container that exceeds the configured grace period can be terminated forcibly, so set and test the grace period for each container rather than trusting the default.

Distinguish acceptance from completion. An SSH reply proves Windows accepted the request, not that the machine powered off cleanly. Log the acceptance before the container stops, and let the host-side script write its execution log on Windows, where it survives the next startup. If the flow feeds a historian or does buffered writes, inspect the pending and buffered records the same way you would for any other historian-style logging on a restricted edge network: the question is whether data reached durable storage before the process exited.

Should you install Node-RED natively on Windows instead? #

Native installation does remove the container boundary, but it costs you the deployment consistency you chose Docker for. Installing Node-RED natively on each Windows PC means installing Node.js, restoring your nodes and flows, and keeping each machine's settings aligned by hand.

Replicating a Node-RED setup to another computer is straightforward in principle, but a native Windows install per machine defeats the reason you picked containers: one image, the same palette, the same settings everywhere. The better trade is to keep the container and add the host-side SSH channel once per machine, as a deployment step in your install script, so the difference between machines is one configuration file rather than a whole runtime. If a future host is Linux, a native install on Linux becomes reasonable, and the article on how to install Node-RED natively on a Linux edge computer walks that path.

How do you test the sequence before trusting it? #

Test four things in order, each in the environment where it runs: reachability from the container, authentication as the management account, authorization to run the script, and the full stop sequence on a test host. A pass on one branch proves nothing about the others.

A script that works from an interactive Windows desktop proves only the local script path. The branches that still need proof are container-to-host connectivity, remote authentication, the remote account's environment and effective rights, and the stop ordering of containers, Docker and Windows. Run the whole sequence against a test host, then read the host-side log and confirm each container either exited cleanly or was terminated after its grace period. Put the approved command in the flow only after every branch has passed once.

The decision: keep Node-RED in Docker, enable Windows OpenSSH on the host, authenticate a restricted management account by key, let that account run one fixed PowerShell script that stops containers and then Windows, and test reachability, authentication, authorization and the full stop sequence separately before the first production shutdown.

Questions asked

Will Windows stop my containers gracefully when it shuts down?

Windows shutdown signals the Docker backend, which asks containers to stop and allows a grace period before forcing termination. An application that needs longer than that grace period to flush buffers can be killed. Set and test the stop grace period per container, and confirm on Windows that each container received and handled the signal rather than assuming host shutdown covers it.

Should the shutdown script be a .bat or a .ps1 file?

Either works, because both must be launched by Windows itself; changing the extension does not change the execution boundary. A PowerShell script gives you clearer error handling, status checks and structured output for the host-side steps, which makes the sequence easier to audit. Keep the script path fixed and do not accept arbitrary commands from the flow.

Where should the SSH private key for the shutdown account live?

Outside the application image. Treat the key as a deployment secret: mount it at runtime or inject it through your secret mechanism, and keep it out of any image you distribute to other machines. If the image or the flow file carries the key, everyone who can read either can shut the host down.

How do I tell whether the machine actually finished shutting down?

An acknowledgement from the remote session means Windows accepted the request, not that shutdown completed. Once the Node-RED container exits, the flow cannot observe or report the result.

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

  • How-to

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

    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.

    Edge computing and IIoT

  • Explainer

    Can plant historian software run in Docker containers

    The processes will start, but historian services hold retained state and are not documented to communicate statelessly, so a started container proves nothing about collection, calculations, events, or queries surviving replacement. Keep the working deployment in production and evaluate containers only in an isolated test, with a volume mapped for every path that holds data or configuration, passing a counted query across a restart, a recreate, and a host move.

    Edge computing and IIoT