Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 Docker 启动时支持从环境变量初始化配置文件 #176

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ COPY static static

COPY views views

#ENTRYPOINT [ "/app/PrometheusAlert" ]
COPY docker-entrypoint.sh docker-entrypoint.sh

CMD if [ ! -f /app/db/PrometheusAlertDB.db ];then cp /opt/PrometheusAlertDB.db /app/db/PrometheusAlertDB.db;echo 'init ok!';else echo 'pass!';fi && /app/PrometheusAlert
ENTRYPOINT [ "/bin/sh", "/app/docker-entrypoint.sh" ]
17 changes: 17 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ curl http://localhost:8080/health
#默认登录帐号和密码在app.conf中有配置
```

## Docker

配置文件 app.conf 的内容可以使用环境变量的方式初始化。

所设置的变量前缀必须以 `PA_` 开始,后面使用配置文件的配置项名称,特别注意的是此配置对大小写并不敏感。

示例

```
docker run -d \
-e PA_LOGIN_USER=prometheusalert
-e PA_LOGIN_PASSWORD=prometheusalert
-e PA_TITLE=PrometheusAlert
feiyu563/prometheus-alert
```

所有的配置文件内容请[点击此处](https://github.com/feiyu563/PrometheusAlert/blob/master/conf/app-example.conf)查看

<br>
<br>
Expand Down
17 changes: 17 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
if [ ! -f /app/db/PrometheusAlertDB.db ]; then
cp /opt/PrometheusAlertDB.db /app/db/PrometheusAlertDB.db
echo 'init ok!'
else
echo 'pass!'
fi

if env | grep -q '^PA_[^=].\+'; then
for VAR_NAME in $(env | grep '^PA_[^=].\+' | sed -r "s/^PA_([^=]*).*/\1/g"); do
VAL_CONTENT=$(eval echo \${PA_${VAR_NAME}})
echo "Config overridden from Environment variable, var=${VAR_NAME}=${VAL_CONTENT}"
sed -i -E "s/(${VAR_NAME})(\ *=\ *).*/\1\2${VAL_CONTENT}/i" /app/conf/app.conf
done
fi

exec /app/PrometheusAlert