Start docker container at boot

Hi!
Some documentation here for the record.
I have a docker container hertzg/rtl_433 I want to start at boot.
The container application connects to a remote MQTT server over a Wireguard interface on OpenWRT.
If use the command: docker run -it --name rtl433 --restart unless-stopped hertzg/rtl_433 the container starts fine and connects to the remote MQTT server.

If I reboot, the container starts automatically at boot as it should when using the --restart unless-stopped tag but the application does not connect to the MQTT server. I check the logs via docker logs container_name and it says MQTT server cannot be reached. I stop the container and start it again, then it connects to the remote server just fine. I think it can be the Wireguard interface is not brought up yet when docker service starts?

I tried to mitigate this by first changing the docker run command to instead use the tag --restart on-failure and then adding two lines to the local startup script /etc/rc.local to look like this:

sleep 20
# I chose 20 seconds as an arbitrary number for testing purposes
docker start rtl433
exit 0

But when I reboot my system the container is still not started.

I check the system logs:

Sun Jan 22 16:18:41 2023 daemon.notice procd: /etc/rc.d/S95done: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Sun Jan 22 16:18:41 2023 daemon.notice procd: /etc/rc.d/S95done: Error: failed to start containers: rtl433

A little bit further down it says:

Sun Jan 22 16:18:42 2023 daemon.info procd: - init complete -

I figured out it must be that the /etc/rc.local script is run before system init is completed, and then dockerd is not started yet. So 20 seconds delay is probably not enough.
I changed it to sleep 60 and then it worked. :smile: :star_struck:

Here is more reading. https://docs.docker.com/config/containers/start-containers-automatically/