Shell commands in docker container?

Hi,

Trying to make Wake On Lan plugin work with shutdown.
Asked for help in this thread, and managed to make Wake on lan work, also state works perfectly.
But shut down doesn’t work for me.

If I log in on my server via SSH and run the command
net rpc shutdown -I 192.168.1.3 -U USERNAME%PASSWORD
It works perfectly.

But when I make a shell_commands.yaml file which I put the following in:
net rpc shutdown -I 192.168.1.3 -U USERNAME%PASSWORD
Then do this in the configuration.yaml:
shell_command: !include shell_commands.yaml
switch

  • platform: wake_on_lan
    name: PC
    mac_address: “MACADRESS”
    host: 192.168.1.3
    turn_off:
    service: shell_command.turn_off_pc

It wont work.
I also tried making this inside shell_commands.yaml
test_cmd: touch /home/docker/.config/homeassistant/test.txt
And then went to Services and tried calling this, no file is created.

Is it possible to run shell commands when Home Assistant is in Docker?
I use network mode Host and everything is run with a user called docker, I have tried adding this user to the root and sudo group. No difference.

1 Like

The hassio way is to use an addon (https://home-assistant.io/addons/rpc_shutdown/). It doesn’t look like you’re using hassio, so you’ll need to recreate something similar.

I dont use hassio, I use a server running Debian and Docker, and this image:
https://hub.docker.com/r/homeassistant/home-assistant/
The latest image.

So is there no way to make this work as intended? Do you have to use an addon?

Not that I’m aware of using the stock home assistant docker image. It just doesn’t have the necessary binaries for it to work. That’s why hassio uses the addon.

When you login ssh, this is to host server or docker container?

Docker images don’t have much installed many times.
ssh into container, if script runs there it should work from ha

docker exec -it HAContainer /bin/bash
/Run/yourscript.sh

If it works HA should be able to run it unless you are running in container as non root user. (FYI…sudo won’t work as it will likely not be installed in container)

I logged in on SSH on the server itself. So i will try to SSH into Docker. If it wont work from there, what alternatives do I have? Install it outside Docker?

This is the result:
docker exec -it b1fa19ed2733a7b3be91323db4d10672bb6b388a3647b6972d16eebac81ae1da /bin/bash /home/docker/.config/homeassistant/turn_off_pc.sh
/bin/bash: /home/docker/.config/homeassistant/turn_off_pc.sh: No such file or directory

I cannot make it work, any ideas?

I think I managed to finally link it correctly inside the container.
docker exec -it b1fa19ed2733a7b3be91323db4d10672bb6b388a3647b6972d16eebac81ae1da /bin/bash /config/turn_off_pc.sh

Gives me this reply:
/config/turn_off_pc.sh: line 2: net: command not found

Which means it finds the file, this is the contents of the file:
#!/bin/bash
net rpc shutdown -f -t 0 -I 192.168.1.3 -U USERNAME%PASSWORD

Remember Should be linked according to docker container file path not host file path

Try ‘ls -all’ and verify file permissions
It basically a Linux system so I assume you just need to make it executable and run. Maybe ‘chmod +x /file.sh’ in container would work.

Every command you run will be run in the docker container, so you have to make sure that net is available in the docker container.

Is it possible to install this to the container? Will it be removed when the container is updated?
How do I install the application that supports this command?

Installing apps in running container is not the “docker way” as I understand it. Normally you would make image with needed application installed and create container for that.

To answer your question, YES you can likely install it but not sure if it will be persistent. Best way is make image with it.

I understand! Maybe i should run HA ”normally”? Instead of docker, which is the best way to install it on Debian? I can only find guides for RPi?

You should change your script maybe.

from the docker container you should ssh into host (or remote server) then execute command on host

to test
login docker container “docker exec -it yourcontainer /bin/bash”
ssh server your trying to control “ssh user@IPaddress”
have script installed there and run script there “/config/turn_off_pc.sh”

so you’d likely have script in HA Docker that ssh and run script on host(remote server)

EDIT
Docker container should be treated as separate host with its own IP assuming --net=host was not used (I believe)
I tested this (manually/no scripts) and it worked OK

I did some testing. And I ran in to a problem instantly. I found out my docker container had changed name, because Watchtower had updated it, this happens every time a new Home Assistant update is out, which makes the script useless every time?

But I did try what you said, and I managed to connect to it, with user root, but I got a few questions first:
root@nas:/usr/src/app# ssh [email protected]
The authenticity of host ‘192.168.1.2 (192.168.1.2)’ can’t be established.
ECDSA key fingerprint is XX:XX:XX:XX:XX:XX. (redacted the real key if it is important)
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes’ or ‘no’: yes
Warning: Permanently added ‘192.168.1.2’ (ECDSA) to the list of known hosts.
[email protected]’s password:

But then I was connected. So how do I solve the problem of container changing name?
And how would I write the script for all this to work?

EDIT: I could replace the container name with the name of the Docker itself, which is Homeassistant, so that will work every time!

Got some more testing done, I was confused, I don’t need the dockers name itself, it was only for testing.

What is my trouble is how to write the script itself.
“ssh user@IPaddress” asks for password. How do I put this in the script? So that it will automatically connect to ssh and run net rpc shutdown -f -t 0 -I 192.168.1.3 -U username%password?

Try setting up ssh key for the docker container to host(remote server). This way user/pass not needed.

You may be able to maintain persistence of this by mounting folder holding key to docker container

Other than that may require google (how ssh bash script).
Using "echo’ command may be possible but I think I try echo’ before with no success. Key may be best solution.

How do I setup an ssh key for the docker container?

I dont understand what you mean by mounting folder holding key to docker container, how is that done?

Login to container

Do this

Save key to location (folder) that is mounted(-v /host/key:/container/key) similar to HA config folder

Great idea, I tried it now.
I managed to make the key, did this:
ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
[email protected]’s password:
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘[email protected]’”

Tested the command above, and it worked.

So now I edited the container and added this:
For host: /root/.ssh/ and for container: /root/.ssh/ which seems to be the standard location.
And restarted the container, but no success. So I think it might now have copied over to host?