我有这个工作,但想知道是否有任何潜在的副作用,甚至更好的方式来做到这一点。下面的例子是通用的。反馈请求 - 重新启动文件更改的Docker容器
我有一个有两个容器(container_1
和container_2
)的docker-compose文件。
container_1
公开包含用于运行已安装服务的各种配置文件的卷。
container_2
从container_1
挂载卷,并定期运行一个脚本,该脚本会拉取文件并更新在container_1
中运行的服务的配置。
每次更新配置时,我都想重新启动container_1
中的服务,而不必使用cron
或我已经讨论过的一些其他方法。
我的解决办法:
我把剧本上container_1
来检查,如果配置文件已经更新(该文件最初是空的,并且的md5sum被储存在一个单独的文件),如果该文件已经改变基于md5sum它会更新当前散列并杀死进程。
在撰写文件中,我有一个healthcheck
定期运行脚本,restart
设置为always
。当container_2
中的脚本运行并更新container_1
monitor_configs.sh
中的配置文件时,container_1
上的脚本将终止服务进程,容器将重新启动并重新加载配置。
monitor_config.sh
# current_hash contains md5sum of empty file initially
#!/bin/sh
echo "Checking if config has updated"
config_hash=$(md5sum /path/to/config_file)
current_hash=$(cat /path/to/current_file_hash)
if [ "$rules_hash" != "$current_hash" ]
then
echo "config has been updated, restarting service"
md5sum /path/to/config_file > /path/to/current_file_hash
kill $(pgrep service)
else
echo "config unchanged"
fi
泊坞窗,compose.yml
version: '3.2'
services:
service_1:
build:
context: /path/to/Dockerfile1
healthcheck:
test: ["CMD-SHELL", "/usr/bin/monitor_config.sh"]
interval: 1m30s
timeout: 10s
retries: 1
restart: always
volumes:
- type: volume
source: conf_volume
target: /etc/dir_from_1
service_2:
build:
context: /path/to/Dockerfile2
depends_on:
- service_1
volumes:
- type: volume
source: conf_volume
target: /etc/dir_from_1
volumes:
conf_volume:
我知道这是不是打算利用healthcheck
但它似乎得到最彻底的方法期望的效果,同时仍然在每个容器中仅保持一个运行过程。
我尝试过,没有tini
在container_1
,它似乎在两种情况下都按预期工作。
我计划将healthcheck
的interval
延长至24小时,因为container_2
中的脚本每天只运行一次。
使用情况
我跑Suricata在container_1
和手撕猪肉在container_2
以更新Suricata规则。我想每天运行一次pullpork,如果规则已更新,请重新启动Suricata以加载新规则。
谢谢......我一定要看看confd,看起来它可以帮助我处理其他一些事情。 –