<p>by using <code>docker stack</code> you can achieve a health check.
you can add the below lines in <code>docker-compose.yaml</code>.</p>
<pre><code>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
</code></pre>
<p>Reference:
<a href="https://howchoo.com/devops/how-to-add-a-health-check-to-your-docker-container" rel="nofollow noreferrer">https://howchoo.com/devops/how-to-add-a-health-check-to-your-docker-container</a></p>

CC BY-SA 4.0
2021-08-30T07:08:06.240Z
2021-08-30T07:08:06.240Z
Arvind Rawat
<p>Create health endpoint inside application and call it from outside and if no response restart docker</p>

CC BY-SA 4.0
2021-08-28T03:25:59.513Z
2021-08-28T03:25:59.513Z
Siddique Ahmad
<p>I have a critical container application which task consist of polling sensors and taking consequent action on some external hardware.</p>
<p>If the application of the container crashes it's fine. <code>restart: always</code> will take care of this.</p>
<p>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.</p>
<p>I'd like my container application to periodically communicate with something and if it stops doing so, the container gets restarted.</p>
<p>Does this kind of mechanism exists?</p>
<p>I'm running the container with Docker.</p>

CC BY-SA 4.0
2021-08-29T12:04:04.363Z
Are you considering to run a CaaS like k8s as well? If the container is run in k8s then one could define a liveness check. If it fails then k8s will restart the container.
030
<docker><containers>
Restart container if its application hangs
Francois
# Restart container if its application hangs

(Asked by: username_0 on 2021-08-28)

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.

**Tags:** docker, containers

> **Comments:**
> **username_1**: Are you considering to run a CaaS like k8s as well? If the container is run in k8s then one could define a liveness check. If it fails then k8s will restart the container.  

---

## 2 answers

### Answer by username_2 on 2021-08-30. Score: 2

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


---

### Answer by username_3 on 2021-08-28. Score: 1

Create health endpoint inside application and call it from outside and if no response restart docker


---

