HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Restart container if its application hangs

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
containerrestartapplicationitshangs

Problem

I have a critical container application which task consist of polling sensors and taking consequent action on some external hardware.

If the application of the container crashes it's fine. restart: always will take care of this.

However if the application hangs, possibly blocked on IO or another unexpected event, this is where things can go wrong as the external hardware will get stuck in it's state possibly indefinitely.

I'd like my container application to periodically communicate with something and if it stops doing so, the container gets restarted.

Does this kind of mechanism exists?

I'm running the container with Docker.

Solution

by using docker stack you can achieve a health check.
you can add the below lines in docker-compose.yaml.

version: '3.1'

services:
  web:
    image: docker-flask
    ports:
      - '5000:5000'
    healthcheck:
      test: curl --fail -s http://localhost:5000/ || exit 1
      interval: 1m30s
      timeout: 10s
      retries: 3


Reference:
https://howchoo.com/devops/how-to-add-a-health-check-to-your-docker-container

Code Snippets

version: '3.1'

services:
  web:
    image: docker-flask
    ports:
      - '5000:5000'
    healthcheck:
      test: curl --fail -s http://localhost:5000/ || exit 1
      interval: 1m30s
      timeout: 10s
      retries: 3

Context

StackExchange DevOps Q#14586, answer score: 2

Revisions (0)

No revisions yet.