Sub topic 13
Documenting ports and checking health
This chapter covers two instructions that describe how a running container behaves: EXPOSE and HEALTHCHECK. It assumes you can write a basic Dockerfile, build an image, and start a container with docker run -p.
EXPOSE documents a port
EXPOSE records which port the application listens on. It is metadata stored in the image. It does not open the port or make it reachable from the host.
EXPOSE 3000
EXPOSE 5353/udpThe protocol defaults to TCP. Add /udp when you need UDP.
To reach the app from the host, you still publish the port at run time with -p:
docker run --rm -p 8080:3000 myapp:1.0Here host port 8080 maps to container port 3000. EXPOSE does have two practical uses:
- It tells readers and tools which port the app uses.
docker run -P(capital P) publishes every exposed port to a random free host port.
HEALTHCHECK tests the app
A running process is not always a working one. A web server can hang or lose its database connection while the process stays alive. HEALTHCHECK tells Docker to run a command inside the container at intervals. The command's exit code decides the container's health:
0means healthy.1means unhealthy.
Add the check to the Node.js Dockerfile from earlier chapters:
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
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))"]
USER node
CMD ["node", "server.js"]This assumes your app answers GET /health with a 2xx status. The check uses node because slim images usually do not include curl or wget. Node 20 provides fetch built in.
The options
| Option | Default | Meaning |
|---|---|---|
| `--interval` | 30s | Time between checks |
| `--timeout` | 30s | A check that runs longer than this counts as a failure |
| `--start-period` | 0s | Startup grace time. Failures in this window do not count toward `--retries` |
| `--retries` | 3 | Consecutive failures needed to mark the container unhealthy |
A Dockerfile can contain only one HEALTHCHECK. If there are several, the last one wins. HEALTHCHECK NONE disables a check inherited from the base image.
Health states
A container with a health check is always in one of three states:
Watching the status
Start the container in the background and look at it:
docker run -d --name web -p 3000:3000 myapp:1.0
docker ps
docker inspect --format '{{.State.Health.Status}}' webdocker ps shows (health: starting), then (healthy) in the STATUS column. To see the output of recent checks, run docker inspect web and read State.Health.Log.
What reacts to the status
Plain docker run only reports the status. It does not restart an unhealthy container. Other tools do act on it:
- Docker Swarm replaces unhealthy tasks.
- Docker Compose can wait for a healthy service with
depends_onandcondition: service_healthy. - Kubernetes ignores
HEALTHCHECKand uses its own liveness and readiness probes.
Common errors
- The check command is missing from the image. A check that calls
curlin an image without it always fails, and the container becomes unhealthy. Use a tool the image already has. - No start period. A slow-starting app gets marked unhealthy before it is ready. Set
--start-periodlonger than the normal startup time. - Checking the wrong thing. A check that tests only that the port is open misses a broken app. Have
/healthverify the dependencies the app needs. - Expecting
EXPOSEto publish. Ifcurl localhost:3000fails from the host, check that you passed-p.
Exercise
A container built from your image shows "(health: starting)" for a long time and then becomes "unhealthy", though the app works when you open it in a browser. The health command is curl -f http://localhost:3000/health, and the base image is node:20-slim. What is the most likely cause, and how do you fix it?
Show answer
The slim image does not include curl, so every check fails with a non-zero exit code. After the retries run out, the container is marked unhealthy even though the app is fine. Fix it by using a tool the image has, such as the node -e "fetch(...)" check shown above, or by installing curl in the image.