基于日志的Docker健康检查?

pu3pd22g  于 12个月前  发布在  Docker
关注(0)|答案(1)|浏览(229)

我想知道是否有可能在runghcr.io/synesthesiam/homeassistant-satellite上使用的docker-compose中添加健康检查。特别是,有时容器停止工作,我可以在日志中看到以下内容:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054979' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054981' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054983' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054985' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054987' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054989' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054991' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054993' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054995' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3054997' coro=<ClientWebSocketResponse.send_bytes() done, defined at /usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py:153> exception=ConnectionResetError('Cannot write to closing transport')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/client_ws.py", line 156, in send_bytes
    await self._writer.send(data, binary=True, compress=compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 688, in send
    await self._send_frame(message, WSMsgType.BINARY, compress)
  File "/usr/local/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 601, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

字符串
我尝试添加以下内容,但不起作用:

healthcheck:
      test: ["CMD-SHELL", "docker logs homeassistant-satellite 2>&1 | grep -i 'ERROR' || exit 1"]
      interval: 1m # How often to run the check
      timeout: 10s # Maximum time allowed for the check
      retries: 1 # Number of failures before marking unhealthy
      # Add this line to trigger restarts on failures
      start_period: 60s # Delay before restarting after failure


你能给我指个方向吗?

kmpatx3s

kmpatx3s1#

命令在容器内执行。容器内是否有dockerdocker.sock是否已挂载?
你可以用一个进程来启动容器,该进程将日志写入文件。看看https://github.com/synesthesiam/homeassistant-satellite/blob/master/Dockerfile

command: bash -c "python -m homeassistant_satellite | tee /logfile"

字符串
然后检查日志文件是否有错误。我会截断日志文件,所以只考虑新行。

grep -qi 'ERROR' /logfile; ret=$((!$?)); truncate -s 0 /logfile; exit "$ret"

相关问题