Alpine Linux 的 /etc/local.d 目录用于放置那些我们需要在本地服务启动或停止后执行的脚本。

由于树莓派上运行的 docker 容器会因为直接断电导致无法重启,因此可以创建一个脚本,在每次树莓派开机后重新创建那些必须随机启动的容器。

使用说明

cat /etc/local.d/README

This directory should contain programs or scripts which are to be run
when the local service is started or stopped.
 
If a file in this directory is executable and it has a .start extension,
it will be run when the local service is started. If a file is
executable and it has a .stop extension, it will be run when the local
service is stopped.
 
All files are processed in lexical order.
 
Keep in mind that files in this directory are processed sequencially,
and the local service is not considered started or stopped until
everything is processed, so if you have a process which takes a long
time to run, it can delay your boot or shutdown processing.

创建脚本

sudo nano /etc/local.d/init_nextcloud.start

#!/bin/sh

sudo docker rm -f postgres nextcloud

sudo docker run -d --name postgres \
....

赋予脚本可执行权限

sudo chmod +x /etc/local.d/init_nextcloud.start

设置 local 服务开机启动

sudo rc-update add local

经过以上设置,在下一次树莓派启动时,系统中的本地服务启动完毕后就会执行 /etc/local.d/init_nextcloud.start 这个脚本。