Execute script as sudo (access server via ssh command)

I have been trying for days to start/stop docker containers on a nas from a supervisor installation (RP4) via an ssh command. And I am now at a loss as to where the error is.
What works: when I log in via terminal or ssh, I am running as user ‘root’ and can control the containers via: ssh -i /config/shell/ssh/homeassistant [email protected] "docker start ...".
first i tried to execute this command directly in HA as shell_command, but without success. then i tried many variants via the following script:

#!/bin/bash
command="sudo docker stop 123ID678"
echo "start..." >> /config/shell/log.txt
name=$(whoami)
echo "$name" >> /config/shell/log.txt
ssh -i /config/shell/ssh/sshkey [email protected] "$command"
echo "stop." >> /config/shell/log.txt

The script reports root as the user and also writes to the log file. In the terminal it also controls the container, but not when the script is started via HA.

I have success in the terminal, but not when I run sh /config/shell/script.sh as shell_command (the directories are in the whitelist of configuration.yaml).
Finally, I have tried all variations in the script to use sudo (sudo ssh -i... or "sudo docker start ..." etc.).
Is it possible that it is actually impossible to execute a sudo command via ha on a remote server using ssh? Or do I have a thinking error somewhere? Thanks in advance for information and tips!

I am not quite sure about your question. Just want to say, if the installation is a HA OS, the root user of Addon is a limited user.
You may have a look at this: I can not access root access?

The ssh command is only sent to the remote server, so HA doesn’t care what it’s doing. You can redirect stderr (with “2>”) to a file to see what you’re getting there:

ssh -i /config/shell/ssh/sshkey [email protected] "$command" 2> /config/shell/ssh_err.txt

Alternatively you can redirect stderr to stdout and have that go to your same log file:

ssh -i /config/shell/ssh/sshkey [email protected] "$command" >> /config/shell/log.txt 2>&1

I was aware of the way the log is written, but only an empty line is generated, as ssh is obviously not executed in the script if the script is started by ha.
I am aware that the OS does not have the same functionality as a debiab, for example, but it is strange:

  1. ssh command in terminal: works! Even without sudo.
  2. Start script in terminal: works! also without sudo commands.
  3. Call script as shell_command through ha: does not work.
  4. Execute ssh command directly through a shell_command in ha: does not work.

I therefore wonder if the root user can have different rights. In the ssh-addon I have created a user root with which I can successfully log in via ssh. and when I call whoami via a ha shell_command, I am also shown root (see above). But apparently many things don’t work with it, although I didn’t need sudo in the terminal in the first two cases.

I found this thread with docker, but it seems to describe a different problem.

BTW: I use the SSH & Web Terminal for connecting to ssh in in advanced mode (less security, with root)