最近用到 s6-overlay 记录一下踩到的坑
定位
容器中的 supervisor
https://github.com/just-containers/s6-overlay
上手
以 alpine 为例
1 2 3 4 5 6 7 8 9 10 11 12 13
| FROM alpine
RUN apk update && apk add bash ENV S6_KEEP_ENV=1
RUN apk add --no-cache redis s6-overlay
COPY s6-overlay /etc/s6-overlay
ENTRYPOINT ["/init"]
CMD ["bash"]
|
官方推荐用新格式启服务,即使用 s6-rc.d 目录
默认安装 s6 后,会自带 user, user2 目录
user 目录的 type 为 bundle
用法为,在 s6-rc.d 下新建服务定义目录,然后在 user/contents.d 下 touch 一下同名空文件
1 2 3 4 5 6 7 8 9
| 0 B ┌─ redis-server - ┌─ contents.d 4.0 KiB ├─ type 4.0 KiB ┌─ user 4.0 KiB │ ┌─ type 4.0 KiB │ ├─ run 8.0 KiB ├─ redis-server 12.0 KiB ┌─ s6-rc.d 12.0 KiB s6-overlay
|
redis-server/type 为 longrun
redis-server/run 为
1 2 3
| #!/usr/bin/execlineb -P s6-setuidgid redis redis-server /etc/redis.conf
|
- 官方文档中都是
#!/command/execlineb -P
,但没办法,alpine 中没有把 execlineb 放在 /command 下 - s6-setuidgid 让 redis-server 以 redis 的用户跑
- 显示指定 conf 文件,主要为了确保 dump.rdb 的生成目录对 redis 用户可写,默认 /var/lib/redis
可能遇到的问题
- dockerfile COPY 一行报错,找不到文件
想想有没有 .dockerignore
- cd 或 composer 报找不到 HOME 的定义
s6-overlay 默认会接手环境变量,可以使用 with-contenv
使单个命令继承原 env,with-contenv env
或指定 S6_KEEP_ENV=1 (default = 0),方便开发环境