Sikau

DevOps and deployment · Day 8 · about 30 min

Health checks and logs

HEALTHCHECK, healthy vs running, and reading container logs.

Running isn't the same as working

A process can be alive but useless — stuck, or unable to reach its database. A health check is a command Docker runs regularly inside the container; orchestrators use the result to route traffic, restart containers and wait before starting dependants.

HEALTHCHECK

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
HEALTHCHECK --interval=30s CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
  • docker ps shows (healthy) or (unhealthy).
  • docker logs NAME (--tail N, -f to follow) — apps should log to stdout/stderr.
  • --restart unless-stopped restarts crashed containers.

The rest of this lesson

  • What a health endpoint should checkexplain
  • HEALTHCHECK optionsexplain
  • Health commandpredict
  • HEALTHCHECK optionmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 14 lessons of DevOps and deployment