title: Docker container healthcheck stop unhealthy container
body: <p>I have a docker container that has a healthcheck running every 1 min. I read that appending &quot;|| kill 1&quot; to the healthcheck in dockerfile can stop the container after healthcheck fails, but it does not seem to be working for me and I cannot find an example that works.</p>
  <p>Does anybody know how I can stop the container after marked as unhealthy? I currently have this in my dockerfile:</p>
  <pre><code>HEALTHCHECK --start-period=30s --timeout=5s --interval=1m --retries=2 CMD bash /expressvpn/healthcheck.sh || kill 1
  </code></pre>
  <p><strong>EDIT 1</strong><br/>
  <em>Dockerfile</em></p>
  <pre><code>FROM debian:buster-slim
  
  ENV CODE=&quot;code&quot;
  ENV SERVER=&quot;smart&quot;
  
  ARG VERSION=&quot;expressvpn_2.6.0.32-1_armhf.deb&quot;
  
  COPY files/ /expressvpn/
  
   RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \
  expect curl ca-certificates iproute2 wget jq \
  &amp;&amp; wget -q https://download.expressvpn.xyz/clients/linux/${VERSION} -O /expressvpn/${VERSION} \
  &amp;&amp; dpkg -i /expressvpn/${VERSION} \
  &amp;&amp; rm -rf /expressvpn/*.deb \
  &amp;&amp; rm -rf /var/lib/apt/lists/* \
  &amp;&amp; apt-get purge --autoremove -y wget \
  &amp;&amp; rm -rf /var/log/*.log
  
  HEALTHCHECK --start-period=30s --timeout=5s --interval=1m --retries=2 CMD bash /expressvpn/healthcheck.sh || exit 1
  
  ENTRYPOINT [&quot;/bin/bash&quot;, &quot;/expressvpn/start.sh&quot;]
  </code></pre>
  <p><em>healthcheck.sh</em></p>
  <pre><code>if [[ ! -z $DDNS ]];
  then
      checkIP=$(getent hosts $DDNS | awk '{ print $1 }')
  else
      checkIP=$IP
  fi
  
  if [[ ! -z $checkIP ]];
  then
      ipinfo=$(curl -s -H &quot;Authorization: Bearer $BEARER&quot; 'ipinfo.io' | jq -r '.')
      currentIP=$(jq -r '.ip' &lt;&lt;&lt; &quot;$ipinfo&quot;)
      hostname=$(jq -r '.hostname' &lt;&lt;&lt; &quot;$ipinfo&quot;)
      if [[ $checkIP = $currentIP ]];
      then
          if [[ ! -z $HEALTHCHECK ]];
          then
              curl https://hc-ping.com/$HEALTHCHECK/fail
              expressvpn disconnect
              expressvpn connect $SERVER
              exit 1
          else
              expressvpn disconnect
              expressvpn connect $SERVER
              exit 1
         fi
      else
          if [[ ! -z $HOSTNAME_PART &amp;&amp; ! -z $hostname &amp;&amp; $hostname != *&quot;$HOSTNAME_PART&quot;* ]];
          then
              #THIS IS WHERE THE CONTAINER SHOULD STOP &lt;------------
              kill 1
          fi
  
          if [[ ! -z $HEALTHCHECK ]];
          then
              curl https://hc-ping.com/$HEALTHCHECK
              exit 0
          else
              exit 0
          fi
      fi
  else
      exit 0
  fi
  </code></pre>
  <p><em>start.sh</em></p>
  <pre><code>#!/usr/bin/bash
  cp /etc/resolv.conf /etc/resolv.conf.bak
  umount /etc/resolv.conf
  cp /etc/resolv.conf.bak /etc/resolv.conf
  rm /etc/resolv.conf.bak
  service expressvpn restart
  expect /expressvpn/activate.sh
  expressvpn connect $SERVER
  
  touch /var/log/temp.log
  tail -f /var/log/temp.log
  
  exec &quot;$@&quot;
  </code></pre>