Essential Knowledge: Healthchecks in Docker Compose Quiz

Deepen your understanding of configuring and utilizing healthchecks in Docker Compose, including syntax, behaviors, and best practices. This quiz helps users assess their skills in ensuring container health within a multi-container orchestration environment.

  1. Specifying the Healthcheck Command

    Which field is required to specify the command executed by a healthcheck in a Docker Compose service, for example: testing if a web server responds on port 80?

    1. test
    2. cmd
    3. verify
    4. probe

    Explanation: The 'test' field in the healthcheck section defines the command to run, such as 'curl' to check if a server is responding. The other options are incorrect; 'cmd' is not used in this context, 'verify' and 'probe' are not valid keywords in a Docker Compose healthcheck. Using the wrong field will cause Docker Compose to ignore or misinterpret the healthcheck configuration.

  2. Healthcheck Status Impact on Service Availability

    What is the default behavior in Docker Compose if a container's healthcheck fails and the container is marked as unhealthy?

    1. The container keeps running, but its health status changes to 'unhealthy'.
    2. The container is automatically restarted.
    3. The service is stopped entirely.
    4. The unhealthy container is deleted and recreated.

    Explanation: When a container becomes unhealthy due to a failed healthcheck, the container continues to run with a status of 'unhealthy'. The container will not be restarted, stopped, or deleted by default. Only certain orchestrators or applications may take further action based on this status; Docker Compose itself leaves the container running unless otherwise managed.

  3. Healthcheck Configuration Timing

    Which healthcheck setting defines how long Docker Compose waits before starting to perform healthchecks on a new container instance?

    1. start_period
    2. interval
    3. timeout
    4. delay

    Explanation: The 'start_period' field configures the initial waiting period before healthchecks begin, giving services like databases time to initialize. 'Interval' specifies how often to perform healthchecks, 'timeout' sets how long to wait for a check to complete, and 'delay' is not part of the standard healthcheck options. Misusing these fields may result in premature or missed healthcheck results.

  4. Disabling Healthchecks

    How can you disable a healthcheck for a specific service in Docker Compose?

    1. Set healthcheck to 'disable'.
    2. Use 'healthcheck: none'.
    3. Completely omit the 'healthcheck' section.
    4. Use 'healthcheck: false' under the service.

    Explanation: Setting healthcheck to 'disable' explicitly turns off healthchecks for that service in Docker Compose. While omitting the 'healthcheck' section means no healthcheck is configured, setting it to 'none' or 'false' are not valid syntax and will be ignored. This ensures you have control over whether healthchecking is active for each container.

  5. Health Status in Dependencies

    When using the 'depends_on' option with 'condition: service_healthy' in Docker Compose, what is the effect on service startup order?

    1. The dependent service waits until the specified service reports a 'healthy' status before starting.
    2. The dependent service starts immediately regardless of the health status.
    3. The dependent service waits only until the specified service is created.
    4. The dependent service starts after all services finish downloading images.

    Explanation: Setting 'depends_on: condition: service_healthy' ensures that the dependent service will not start until the referenced service's healthcheck reports 'healthy'. If only creation is awaited, it doesn't guarantee the service is ready, while waiting for image downloads or ignoring health status doesn't ensure startup readiness. This option helps coordinate reliable service initialization.