Docker Community Forums

Share and learn in the Docker community.

Unhealthy container does not restart

This is my Docker Compose YAML file.

version: "3.9"

      - .env
    image: repo/image:latest
      - 4000:4000
      test: ["CMD", "/nodejs/bin/node", "/app/health/index.js"]
      interval: 10s
      timeout: 20s
      start_period: 5s
      retries: 3
    restart: on-failure

The health check CMD works well - I can see the container status to change to unhealthy in the docker ps output. However, this does not trigger a restart of this computer. How do I get the container restarted?


Its because the healthchecks is for docker swarm, if you’re running it just, native docker, nothing will happend if a container gues unhealthy.

But you can look into this:

which will restart the container if it goes unhealthy.

Bonus info, even in docker swarm it wont do anyting, but it will stop routing traffic to that container.

1 Like

So, with Swarm too, we use willfarrell/autoheal ?

I am actually using willfarrell/autoheal to achieve the same goal, however I am still running into trouble:
the request to engine API /containers/{id}/restart endpoint always results in:

The requested URL returned error: 500 Internal Server Error

note that this also only happens for unhealthy containers. I can restart any other healthy container this way just fine.

any ideas? API Version is 1.41.

edit: nevermind. turns out I have a more special usecase: the container in question is attached to its “parent container” via network_mode: service - as soon as parent container is recreated (not restarted, restarting works fine with autoheal), the container will become unhealthy, lose any network connections whatsoever (because network_mode prevents defining other networks) and thus the API cant reach the container anymore. I can’t switch to another network_mode, because the parent container is a vpn app where multiple “child” containers use it as a service to tunnel all their networking through it. From what I read on various topics there does not seem to be a solution to automate the restart of the depending containers if parent is recreated, sadly.