Set system timezone from node-RED

Hi,

Im trying to set timezone inside the Docker container from node-RED this way:


The idea is to inject timezone so I can easily set system timezone. Im running v4.0.5 on docker.

This method throws an error because the Node-RED user isn’t root, so I can’t modify /etc/localtime .
Any idea how I could work around this?

Thanks!

codeHere
[{"id":"eecaed65c5c38d92","type":"inject","z":"fbf8a7aab69c85cc","name":"timezone EUROPE CENTRAL","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime","payloadType":"str","x":192,"y":112,"wires":[["d6e42ee8f919fa9d"]],"icon":"font-awesome/fa-clock-o"},{"id":"631286a30962de4b","type":"exec","z":"fbf8a7aab69c85cc","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"execTimezone","x":512,"y":128,"wires":[["893ac4a1dba6c1a8"],[],[]]},{"id":"b616b072dfe7098c","type":"inject","z":"fbf8a7aab69c85cc","name":"timezone MEXICO","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ln -s /usr/share/zoneinfo/America/Mexico_City /etc/localtime","payloadType":"str","x":162,"y":152,"wires":[["b1533089fe82692e"]],"icon":"font-awesome/fa-clock-o"},{"id":"15fbd12eee3ea874","type":"debug","z":"fbf8a7aab69c85cc","name":"debug 90","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":673,"y":128,"wires":[],"l":false},{"id":"041ab52e33d9f71d","type":"inject","z":"fbf8a7aab69c85cc","name":"timezone CHINA","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime","payloadType":"str","x":152,"y":72,"wires":[["74557fbc1d3ab1cc"]],"icon":"font-awesome/fa-clock-o"},{"id":"893ac4a1dba6c1a8","type":"change","z":"fbf8a7aab69c85cc","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"RESTART CONTAINER","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":623,"y":128,"wires":[["15fbd12eee3ea874"]],"icon":"node-red/arrow-in.svg","l":false},{"id":"5391b1db258b29e0","type":"inject","z":"fbf8a7aab69c85cc","name":"timeNow","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":580,"y":176,"wires":[["15fbd12eee3ea874"]],"icon":"font-awesome/fa-question-circle"},{"id":"d6e42ee8f919fa9d","type":"junction","z":"fbf8a7aab69c85cc","x":384,"y":112,"wires":[["631286a30962de4b"]]},{"id":"b1533089fe82692e","type":"junction","z":"fbf8a7aab69c85cc","x":384,"y":152,"wires":[["631286a30962de4b"]]},{"id":"74557fbc1d3ab1cc","type":"junction","z":"fbf8a7aab69c85cc","x":384,"y":72,"wires":[["631286a30962de4b"]]}]

That makes it even more difficult as one of the reasons for using Docker is to keep the system safe from applications trying to mess with the OS settings.

There is of course a command to change the timezone:

sudo timedatectl set-timezone Asia/Shanghai

To use it you need

  • To be able to see the timedatectl command from within docker.
  • Passwordless sudo access for the Node-red user.

If you can do that I've no idea if it would change the timezone outside of the docker container - which as @Colin says is designed to prevent such jiggery-pokery.

1 Like

I believe I miscommunicated earlier. :sweat_smile:

What I actually need is to change the timezone inside the Docker container, not the host system's timezone. Also, using sudo isn't an option because Alpine-based Docker images (like the one used by Node-RED) typically don’t include sudo by default.

If you open a terminal window inside the docker container, what command would you use to do what you want?

I'm using the same commands (ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime) , but I tried to simplify the process by triggering them through the inject node. I suspect the issue is related to permissions—when I run the commands directly from the terminal, they execute with root privileges, which might be why they work there but not through the inject node.

The correct way is to pass the TZ environment variable into the container.

e.g

docker run -e TZ=Europe/London -p 1880:1880 nodered/node-red:latest
2 Likes

Ok! Thanks