Execute command on Host from HomeAssistant Docker Container

  • platform: command_line
    name: docker_installed_version
    command: docker -v

How to execute any command on Host (Not inside Docker Container)

What is the proper way to be able to access the host outside the docker container?

In this example docker -v fails because inside homeassistant container docker is not a known command

Thanks for any guidance.

Try this procedure posted by user @juan11perez:

https://hastebin.com/sojasolite.sql

but be aware that when you get to step 5 it won’t work if you have an existing authorized_keys file. You have to copy the key manually into the file. Put one key per line with no lines between them.

1 Like

Strangely nothing on this link :frowning: can u please supply a new link or just paste it here - thanks a lot

Well, isn’t that weird. I went to the site to verify that it was still a good link right before I posted it and everything was there. Now it’s blank…:thinking:

Anyway here is the procedure as I have it in my notes:

1-Need to modify the host user privileges to skip typing your password with sudo

sudo visudo

hostuser ALL=(ALL) NOPASSWD:ALL

2-create the following directory:

$ mkdir /home/finity/docker/sshkey/.ssh

Then mount this volume in HA container to preserve the sshkey generated from the HA container and used to execute shell commands. Key will then persist through reboot or upgrades.

-v /home/hass/finity/sshkey/.ssh:/root/.ssh

3-login to container via portainer or

$ sudo docker exec -it home-assistant /bin/bash

4-generate sshkey. - https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

$ ssh-keygen -t rsa (press enter, enter, enter)

5-copy the sshkey to your host ***

$ ssh-copy-id [email protected] (type password when prompted)

*** this won’t work if you have an existing authorized_keys file. You have to copy the key manually into the file. Put one key per line with no lines between them.

If I remember correctly, I think that’s all I had to do.

1 Like

Hi, I was looking for something similar and this was the first google result. Yet, please note a nice alternative with pipes is possible and well explained here. This approach doesn’t required SSH from container to host, which is not ideal on security level.