Shell command (docker)


I’m trying to execute a simple bash script from home assistant but am struggling.

bash script “”:


/usr/bin/docker-compose restart zwave2mqtt

this executes fine from the linux terminal when in my docker compose directory.

home assistant part of my docker-compose.yaml:

    container_name: homeassistant
    image: homeassistant/home-assistant:stable
      - /mnt/btrfs_raid/docker/home-assistant:/config
      - /etc/localtime:/etc/localtime:ro
      - /mnt/btrfs_raid/docker/compose:/compose
      - TZ=Europe/London
    restart: always
    network_mode: host
      - mosquitto
      - zwave2mqtt

I’ve added a link to my docker compose directory as volume “/compose” as the docker-compose command needs to run in the same directory as the compose.yaml. (I started with the script in my home assistant config directory with a cd line in the script to change to the compose directory, but that didn’t work).

relevant line from my configuration.yaml:

  restart_zwave: sh /compose/

I have also tried “./compose/”, both with and without single quotes in configuration.yaml

The error I’m getting in home assistant log when I try and call shell_command.restart_zwave either from a button or developer tools > services:

ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `./compose/`, return code: 127

Any ideas where I’m going wrong?

I think it may be a permissions issue but the script has full a+x permissions so not sure about that? Docker doesn’t install as a user in linux so I can’t add the docker user to a specific group to get this working.

I’ve just found a way of being able to store the script in the home assistant config directory by changing the docker-compose command to:

/usr/bin/docker-compose -f ../compose/docker-compose.yml restart zwave2mqtt

and then changing the shell_command line in my configuration.yaml to reflect this but still no joy, however the error code is now different:

Error running command: `sh /config/`, return code: 2

You can’t execute a shell command on the host machine like that. What you are currently doing is executing the shell command inside the docker container.

Just a question, in you docker-compose, home assistant depends on zwave2mqtt, doesn’t this mean that home assistant will be restarted as well, when you restart zwave2mqtt.

I’m trying to do it the other way around, so that zwave2mqtt can be restarted from HA, as sometimes this needs to be done to refresh data.

Also when updating HA to a new version, if I forget to restart zwave2mqtt none of the sensors pass information until zwave2mqtt is also restarted. I want to be able to do this via a button away from home.

depends_on doesn’t restart zwave2mqtt when HA is updated which is what I’m wanting.

So can I achieve what I want to do or not?

and are you sure about your host/docker container statement?

Can’t HA see the files in the volume passed? That volume is mapped to a directory on the host after all.

There are multiple ways to restart a docker container from Home Assistant.

SSH into host
You could generate SSH keys in the docker container, copy them to your host, and mount them to the docker stack to persist across reboots. Then execute the command on the host through SSH.

HA Dockermon
A docker container that gives you status of the docker containers and you can start/stop/restart containers.

Monitor Docker
A custom component that gives you statistics about docker containers such as CPU, RAM etc. and also let’s you start/stop/restart containers.

1 Like

Yes, I am.

Yes, HA can see the files, but still the script will be executed inside the home assistant docker container, which doesn’t have docker compose and can’t restart docker containers outside of the container like this.

Thanks I will take a look at those last 2 recommendations, the ssh option seems a bit like a sledgehammer to crack a nut haha

OK gotcha, thanks :slightly_smiling_face:

Totally agree :smiley:

I used HA Dockermon in the past and I’m now using Docker Monitor and I like it, because it also gives you CPU, RAM etc. for the docker containers.

Have you considered using the hassio.addon_restart service call?

Go to Developer Tools > Services and select hassio.addon_restart. You will need the slug name of your zwave2mqtt add-on for entity_id.

The service can be called from a Button card.

I already use Portainer so I don’t think HA Dockermon will add anything to my setup. I expose HA via nginx but I don’t want to expose Portainer / HA Dockermon, I want to restart zwave2mqtt via HA so I’ll look at that custom component, thanks for that

He is running Home Assistant Container as far as I can tell from his docker-compose file.

1 Like

HA Dockermon allows you to restart docker containers from HA through REST calls, you shouldn’t need to expose it to NGINX. But anyway the custom component is my preferred solution :smiley:

Ouch! You’re right; I skimmed over that part. Oh well, never mind.

Well my first attempt with monitor_docker has failed, with an error parsing the manifest.json file

Looks like a weekend project…

Unless that file isn’t needed of course… it’s been a long week

I installed it 2-3 weeks ago. How did you try to install it?

I’ve recloned the files and it’s no longer complaining about the manifest.json file

However it’s not clear how to use it to begin with. I can’t see anything under states or services related to monitor_docker?

You should have a switch for each container that you configured, which starts/stops the container.

Did you add monitor_docker: and the relevant config under configuration.yaml?

OK, I’ve destroyed the container and recreated it and now I’m getting somewhere