Docker container restart from script No such container

I’ve got something I need some help solving. It’s undoubtedly caused by a lack of basic knowledge, but I have to start somewhere :wink:
What I have is this;

I run Home Assistant on Debian 12, all is well.
To run this reasonably safe I use https to connect to it, also working fine.
As I’m using LetsEncrypt for my certs, I periodically need to renew them and place them on the HA server. Also working fine.
I now want to automate the cert update on the HA server, so I try to make a script and this doesnt go very well…
For this I have the most basic script imaginable:

scp certcopy@ /usr/share/hassio/ssl/fullchain.pem
scp certcopy@ /usr/share/hassio/ssl/privkey.pem
docker restart homeassistant

I run it as user certcopy, which is a sudo and docker group member.
The first 2 lines go as they should, copy the files from my OPNsense firewall to the server.
After this I want to restart the HA docker so it picks up the new certs and thats where things go funny.
If I run docker restart homeassistant from the command prompt the container is being restarted without problems, if I run it from the script I get Error response from daemon: No such container: homeassistant

I’m sure I’m doing something fundamentally wrong, hopefully you guys can help me out.
I guess it has to do with the difference between running something interactively and scripted, but I have no idea how to solve it.
Any help is highly appreciated :slight_smile:

Different approach: for larger installs a reverse proxy is placed in front of apps to “terminate TLS”, for a single host I like to use nginx-proxy, it’s acme companion automatically creates and updates TLS certs.

On your issue: do you use the same user to execute your script?

I already use Acme and HAProxy and they both work well. The reason for the cert copy to the HA server is that I also want to use HTTPS from HAProxy to HA, so I need to have the certs on both OPNsense and HA. To do this I need to copy them from the OPNsense server to HA, hence, the script.
As for your question; I use the ‘certcopy’ user to do this. As said, running, as certcopy, docker restart homeassistant from the command line works, running the results in No such container.
I suspect the environment the script running in differs from starting it interactively

Right, figured it out.
I made the .sh file on a Windows machine, which Linux didn’t like.
Deleted the file, made a new one on the linux machine itself et voila, it worked :see_no_evil:

1 Like

Thank you for sharing the solution. It was probably because of the different line endings which is a common problem in shell scripts, but in this case I wouldn’t have thought of that without the info that you used Windows too :slight_smile:

Yw, thats what forums are for; not only asking :wink:
I suddenly realised this when I looked at the directory copied the certificates. They were named 'fullchain.pem'$'\r' and 'privkey.pem'$'\r'

I’ve noticed that there is no ‘Solution’ button here? The Home Assistant forum runs on the same platform and they do offer this. It marks the topic [Solved] and shows a direct link to the solution. Very convenient for lengthy topics. Or is this also possible here, but I didnt see it?

You opened the topic in General Discussion which does not support marking posts as solutions.
I’m going to move the topic to the Docker Engine category so you can mark your post as solution if you want to.

Ah ok, didnt know that :wink: