Sub topic 1
Why a running container may still be broken
A container can show as running while the application inside it is stuck, crashed internally, or unable to serve requests. This chapter explains why that gap exists and why Docker needs a separate mechanism to close it. The rest of the tutorial covers that mechanism, the HEALTHCHECK instruction.
Prerequisites
This chapter assumes you can do the following:
- Run basic Docker commands such as
docker build,docker runanddocker ps. - Read a simple Dockerfile.
- Understand that a shell command returns an exit code, where 0 means success.
- Understand that an HTTP service listens on a port and answers requests.
What "running" means to Docker
Docker tracks a container's main process, the command named by CMD or ENTRYPOINT. While that process exists, docker ps reports the container as Up. When the process exits, the container stops.
Docker does not look at what the process is doing. It only knows whether the process is alive. That is a weak signal about the application.
Ways a running container can be broken
Each of these failures leaves the main process alive, so the container still shows as Up:
- Deadlock. Threads wait on each other forever, and no request is ever answered.
- Exhausted resources. The application has used every database connection or file descriptor and rejects new work.
- Lost dependency. The database or a message queue is unreachable, so every request fails with an error.
- Stuck startup. The process started but is still loading data, or it hung before it began listening on its port.
- Overload. The request queue is full, and responses take far longer than any client will wait.
In every case, a client sees timeouts or errors. A monitoring view that only reads Up shows nothing wrong.
The gap in practice
The following sequence shows how the gap appears to the people involved.
Docker's question, "is the process alive?", gets a true answer. The client's question, "can I get a response?", gets a different answer. Nothing connects the two.
A reproducible example
You can see the gap with a tiny server that stops answering while its process stays alive. Save this as app.py:
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
# Simulate a hang: the process lives, but no response is sent.
time.sleep(3600)
HTTPServer(("0.0.0.0", 8000), Handler).serve_forever()Build and run it with this Dockerfile and these commands:
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
EXPOSE 8000
CMD ["python", "app.py"]docker build -t hang-demo .
docker run -d --name hang-demo -p 8000:8000 hang-demo
docker ps --filter name=hang-demo
curl --max-time 5 http://localhost:8000/docker ps shows Up in the STATUS column. The curl call fails after five seconds with a timeout. The container is running and the application is useless for any client.
What a health check adds
A health check is a command that Docker runs inside the container on a schedule. The command tests the application directly, for example by requesting an HTTP endpoint. Its exit code tells Docker whether the application is working.
With a health check, Docker keeps a second status next to the process state: starting, healthy or unhealthy. Later chapters cover the syntax, the timing options and what Docker does with that status.
Exercise
A container shows Up 2 hours in docker ps, but every request to its web service times out. Name two causes that fit this description, and explain why Docker reports Up.
Show answer
Possible causes include a deadlocked application, exhausted database connections, a lost dependency, or a hang during startup. Docker reports Up because it only checks that the container's main process is still alive, not whether the application inside it can respond.