nerdexam
Cisco

300-910 · Question #66

Refer to the exhibit. A docker-compose.yml file implements a postgres database container. Which .gitlab-ci.yml code block checks the health status of the container and stops the pipeline if the contai

The correct answer is C. Validate Application Infrastructure: stage: Validate_Infrastructure before_script: - apk add --no-cache docker-compose script: - docker-compose up -d - sleep 15s - health_state=$(docker container inspect postgres_data --format='{{.State.Health.Status}}') - If [[ "$health_state" != "healthy" ]]; then exit 1; fi. To check a Docker container's health status in a GitLab CI pipeline, docker container inspect should be used with a format flag to retrieve the health status, and then the pipeline should exit if the container is unhealthy.

CI/CD Pipelines

Question

Refer to the exhibit. A docker-compose.yml file implements a postgres database container. Which .gitlab-ci.yml code block checks the health status of the container and stops the pipeline if the container is unhealthy?

Exhibit

300-910 question #66 exhibit

Options

  • AValidate Application Infrastructure: stage: Validate_Infrastructure before_script:
    • apk add --no-cache docker-compose script:
    • docker-compose up
    • sleep 15s
    • health_state = $(docker container logs postgres_data | grep healthcheck)
    • health_state == 'healthy'
  • BValidate Application Infrastructure: stage: Validate_Infrastructure before_script:
    • apk add --no-cache docker-compose script:
    • docker-compose up
    • sleep 15s
    • health_state = $(docker container attach postgres_data
      | psql -U username postgres status)
    • health_state == 'running'
  • CValidate Application Infrastructure: stage: Validate_Infrastructure before_script:
    • apk add --no-cache docker-compose script:
    • docker-compose up -d
    • sleep 15s
    • health_state=$(docker container inspect postgres_data --format='{{.State.Health.Status}}')
    • If [[ "$health_state" != "healthy" ]]; then exit 1; fi
  • DValidate Application Infrastructure: stage: Validate_Infrastructure before_script:
    • apk add --no-cache docker-compose script:
    • docker-compose up
    • sleep 15s
    • health_state = $(docker container state postgres | grep "Health_status" | grep "running")
    • If [[ "$health_state" == "running" ]]; then exit 1; fi

How the community answered

(32 responses)
  • A
    9% (3)
  • B
    13% (4)
  • C
    75% (24)
  • D
    3% (1)

Why each option

To check a Docker container's health status in a GitLab CI pipeline, docker container inspect should be used with a format flag to retrieve the health status, and then the pipeline should exit if the container is unhealthy.

AValidate Application Infrastructure: stage: Validate_Infrastructure before_script: - apk add --no-cache docker-compose script: - docker-compose up - sleep 15s - health_state = $(docker container logs postgres_data | grep healthcheck) - health_state == 'healthy'

This option attempts to grep logs for healthcheck status, which is not a standard or reliable way to check Docker container health; furthermore, the variable assignment `health_state = $(...)` and comparison `health_state == 'healthy'` are not valid shell script syntax for assignment and conditional checks.

BValidate Application Infrastructure: stage: Validate_Infrastructure before_script: - apk add --no-cache docker-compose script: - docker-compose up - sleep 15s - health_state = $(docker container attach postgres_data \ | psql -U username postgres status) - health_state == 'running'

This option attempts to attach to the container and run a psql command to get the status, which is not how Docker health checks are typically consumed, and the comparison `health_state == 'running'` incorrectly assumes 'running' implies 'healthy' or that this command directly reports health.

CValidate Application Infrastructure: stage: Validate_Infrastructure before_script: - apk add --no-cache docker-compose script: - docker-compose up -d - sleep 15s - health_state=$(docker container inspect postgres_data --format='{{.State.Health.Status}}') - If [[ "$health_state" != "healthy" ]]; then exit 1; fiCorrect

This code block correctly starts the docker-compose services in detached mode (-d), waits for a period, and then uses 'docker container inspect' with the '{{.State.Health.Status}}' format to accurately retrieve the health status of the 'postgres_data' container. It then explicitly checks if the status is *not* 'healthy' and exits the pipeline with an error code if it isn't, which is the correct way to stop the pipeline upon an unhealthy container.

DValidate Application Infrastructure: stage: Validate_Infrastructure before_script: - apk add --no-cache docker-compose script: - docker-compose up - sleep 15s - health_state = $(docker container state postgres | grep "Health_status" | grep "running") - If [[ "$health_state" == "running" ]]; then exit 1; fi

This option attempts to grep `docker container state` for 'Health_status' and 'running', which is not a valid Docker command output structure for health, and the conditional logic `If [[ "$health_state" == "running" ]]; then exit 1; fi` would incorrectly exit if the state *is* running, rather than if it's unhealthy.

Concept tested: Docker container health checks in CI/CD

Source: https://docs.docker.com/engine/reference/commandline/container_inspect/

Topics

#Docker Compose#Container Health Check#GitLab CI/CD#Shell Scripting

Community Discussion

No community discussion yet for this question.

Full 300-910 Practice