CodePark/ questions / Healthchecks, restarts & logs
START PRACTICINGPRACTICE
Backend question · Docker › Healthchecks, restarts & logs

What does this HEALTHCHECK do, why does it use node instead of curl, and what does Docker do when the container becomes unhealthy?

FROM node:22-slim
WORKDIR /app
COPY . .
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD node -e "fetch('http://localhost:3000/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
CMD ["node", "server.js"]

Docker runs the command inside the container every 30 seconds to check if the app is healthy.

How the result is read:

  • Exit code 0 means healthy. Exit code 1 means unhealthy.
  • A check that takes more than 3 seconds counts as a failure (the timeout).
  • Failures during the first 10 seconds don't count (the start period), because the app is still starting.
  • After 3 failures in a row (the retries), the status becomes unhealthy.

You can see the status in docker ps and docker inspect.

Why node and not curl? Slim images don't include curl. Node 18 and later have a global fetch, so the check needs no extra package.

What happens when it is unhealthy? Docker Engine on its own only reports the status. It does not restart an unhealthy container. Other tools use the status:

  • Compose uses it for depends_on conditions.
  • Swarm replaces unhealthy tasks.
  • Kubernetes ignores HEALTHCHECK and uses its own probes.
  • Exit code 0 means healthy and 1 means unhealthy.
  • You tune it with interval, timeout, start-period and retries.
  • Slim images have no curl, so use node or a small binary.
  • Plain Docker does not restart unhealthy containers.
  • Kubernetes ignores HEALTHCHECK and uses its own probes.