Docker Compose Healthcheck Start_Period
Legacy context
This site began as an independent educational reference for developers and architects working with native XML and document-centric data systems. Its early archives focused on practical schema design, validation strategies, and the trade-offs of flexible XML storage—topics that remain relevant for anyone managing evolving data formats.
Later materials expanded into advanced query techniques, including detailed examples of SQL/XML functions and XQuery expressions, with an emphasis on real-world application scenarios. The preserved excerpts reflect a period of active technical writing, but this archive is presented as a historical record only. No ongoing services, certifications, or current product support are implied.
For contemporary infrastructure questions—such as configuring health checks with `start_period` in Docker Compose—readers are encouraged to consult current official documentation. This archive serves solely as a neutral, non-commercial reference for understanding past approaches and foundational concepts.
What `start_period` Actually Does. In Docker Compose, the `healthcheck` instruction controls how Docker determines whether a container is healthy. The `start_period` field is a grace period that gives your application time to initialize before Docker starts counting failed health checks against the container's `retries` limit.
The key distinction: during `start_period`, a failing health check does not increment the retry counter. It also does not mark the container as `unhealthy`. The container remains in the `starting` state until either:
- The first successful health check occurs (container becomes `healthy`), or
- The `start_period` expires and subsequent failures begin counting toward `retries`.
This is not a "wait forever" mechanism. It is a bounded window. If your application never becomes healthy within `start_period`, Docker will begin applying the normal failure logic after that window ends.
Syntax and Placement. In a `docker-compose.yml` file, the healthcheck is defined under a service. The `start_period` field is a duration string, using the same format as other Docker durations (e.g., `30s`, `1m30s`, `500ms`).
services: web: image: my-web-app:latest. healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"]. interval: 10s. timeout: 5s. retries: 3
start_period: 40s
In this example:
- `interval`: how often the health check runs (every 10 seconds).
- `timeout`: how long Docker waits for the check command to return before considering it failed (5 seconds).
- `retries`: how many consecutive failures after `start_period` are needed to mark the container `unhealthy` (3).
- `start_period`: the grace window (40 seconds).
The `test` command can be a shell command, a `CMD` array, or `CMD-SHELL`. For HTTP checks, `curl` or `wget` are common, but you must ensure those tools exist inside the container image.
Decision Criteria: How to Choose a Value: Selecting `start_period` requires understanding your application's actual startup behavior, not guessing. Use these criteria:
- Measure cold-start time. Run the container without a healthcheck and time how long it takes until the service responds to a real request. Add a 20–30% buffer. If your app takes 25 seconds to accept traffic, set `start_period` to 35–40 seconds.
- Account for dependency initialization. If your app connects to a database, cache, or external API at boot, include the time those connections are established. A slow database can push startup to 60 seconds even if the process itself starts in 10.
- Consider image pull and volume mount time. On a fresh deployment, pulling a large image or mounting a slow network volume can delay container start. `start_period` begins when the container process starts, not when the image is pulled, so this matters less—but if your entrypoint script does heavy file copying, include that.
- Match `interval` and `retries` to your tolerance. A common pattern is `interval: 10s`, `retries: 3`, meaning after `start_period`, the container becomes `unhealthy` after 30 seconds of consecutive failures. If your app has brief hiccups (e.g., a 15-second garbage collection pause), increase `retries` rather than `start_period`.
- Do not use `start_period` to mask a broken health check. If your health check command itself is wrong (e.g., wrong port, missing `curl`), the container will never become healthy regardless of the grace period. Verify the check works manually inside the container first.
Mistake 1: Setting `start_period` Too Low. If `start_period` is shorter than the actual startup time, the container will accumulate failures immediately after the grace period ends. With `retries: 3` and `interval: 10s`, a 20-second startup with a 10-second `start_period` will result in `unhealthy` status after roughly 30 seconds of runtime. This causes orchestration tools (like Docker Swarm or Compose-based deployment scripts) to restart or kill the container prematurely.
This independent educational reference summarizes general technical concepts. Verify current standards, dimensions, and manufacturer specifications before making a procurement or engineering decision.