前提
監視するほどのものでもなかったので特にCloudWatch等での監視は行っていない。
気づき
rsyslog・httpd・nginx・mysqld・php-fpm・memcached等はsystemdでの自動起動の設定の一般的ですが、Dockerコンテナの癖はついていなかった。
監視するほどのものでもなくてもDockerでアプリケーションを作る場合はコンテナ自体の自動起動の設定は必要。
事象
ある日アプリケーションが停止していた。
Dockerのコンテナを見たら停止していた。
システムログ
# view /var/log/messages
容量等も問題なかった。
Dockerのlogも問題なかった。
直近のreboot
# last reboot
何故かrebootが行われていた。
AWSマネジメントコンソールでインスタンスの完了したイベントを確認してみると、rebootが行われた時間にsystem-rebootがスケジュールされていた。
AWSで勝手にインスタンスが再起動されていた様子。
監視するリソースが無いので、Dockerコンテナが自動起動するようにしなければいけない。
Dockerコンテナの自動起動を設定する
動作確認はローカル環境にAmazonLinux2を作成して確認を行った。
Running Docker Containers with Systemd
vi /etc/systemd/system/docker.app.service
[Unit]
Description=App Container
After=docker.service
Requires=docker.service
[Service]
Restart=always
ExecStart=-/usr/bin/docker start app
[Install]
WantedBy=multi-user.target
# systemctl enable docker.app.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.app.service to /etc/systemd/system/docker.app.service.
これを設定後rebootするとappコンテナが起動していたのでとりあえずこれでOK。
disableにしたらコンテナが起動しないことも確認してみた。
# systemctl disable docker.app.service
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.app.service.
当然ですが、rebootするとappコンテナは起動しない。