Search code examples
dockerdocker-composehealth-monitoring

Docker container not restarting on health check failures


I have the following docker-compose file

version: "3.7"
services:
  myservice:
    restart: always
    image: myimage
    hostname: myhost
    env_file:
      - ./env/common.env
    healthcheck:
      test: ["CMD-SHELL", "curl --silent --fail localhost:9091/status || exit 1"]
      interval: 5s
      timeout: 1s
      retries: 1
      start_period: 5s   
    ports:
      - 27018:27018
    volumes: 
      - type: bind
        source: ./scripts/myservice
        target: /scripts  
    entrypoint: ["/bin/sh", "-c", "apk add bash && chmod a+x /scripts/init.sh && /scripts/init.sh"]

On running docker-compose run myservice, my container starts correctly (it doesn't crash). However, the health check is failing and when I do a docker ps, I can see that the container is unhealthy.

I want the container to restart automatically on when the container is unhealthy, however the container throws an error in the logs and continues to run when the curl call is made.

How can I make sure that my container is restarted when the health check fails?


Solution

  • You may want to have a look for this, its functions as next:

    Monitor and restart unhealthy docker containers. This functionality was proposed to be included with the addition of HEALTHCHECK, however didn't make the cut. This container is a stand-in till there is native support for --exit-on-unhealthy https://github.com/docker/docker/pull/22719.

    For your case, you could setup a monitor container with above support like next, this container could monitor all unhealthy container and restart them for you.

    docker run -d \
        --name autoheal \
        --restart=always \
        -e AUTOHEAL_CONTAINER_LABEL=all \
        -v /var/run/docker.sock:/var/run/docker.sock \
        willfarrell/autoheal