Bird
Raised Fist0
Dockerdevops~10 mins

Container health checks in Docker - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the Dockerfile line to add a health check that runs the command 'curl -f http://localhost/'

Docker
HEALTHCHECK CMD [1]
Drag options to blanks, or click blank then click option'
Acurl -f http://localhost/
Becho 'healthy'
Cls /app
Dping -c 1 localhost
Attempts:
3 left
💡 Hint
Common Mistakes
Using commands that do not test the service health like 'ls' or 'echo'.
Using ping which may not be installed or meaningful inside the container.
2fill in blank
medium

Complete the Docker Compose service definition to add a health check that retries every 10 seconds

Docker
healthcheck:
  test: [1]
  interval: 10s
Drag options to blanks, or click blank then click option'
A["CMD", "curl", "-f", "http://localhost/"]
Bcurl -f http://localhost/
C"curl -f http://localhost/"
D["RUN", "curl", "-f", "http://localhost/"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using plain strings instead of arrays for the test command.
Using RUN instead of CMD in the test array.
3fill in blank
hard

Fix the error in this Dockerfile health check line to correctly specify the command

Docker
HEALTHCHECK CMD [1]
Drag options to blanks, or click blank then click option'
A["RUN", "curl", "-f", "http://localhost/"]
Bcurl -f http://localhost/
C["curl", "-f", "http://localhost/"]
Dcurl http://localhost/
Attempts:
3 left
💡 Hint
Common Mistakes
Using plain strings instead of JSON arrays.
Using RUN in the command array.
4fill in blank
hard

Fill both blanks to create a health check in Docker Compose that retries every 5 seconds and fails after 3 retries

Docker
healthcheck:
  test: [1]
  interval: 5s
  retries: [2]
Drag options to blanks, or click blank then click option'
A["CMD", "curl", "-f", "http://localhost/"]
B10s
C3
Dcurl -f http://localhost/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an array for the test command.
Setting retries to a string like '3' instead of a number.
5fill in blank
hard

Fill all three blanks to define a Dockerfile health check with a 30-second timeout, 10-second interval, and 5 retries

Docker
HEALTHCHECK --timeout=[1] --interval=[2] --retries=[3] CMD curl -f http://localhost/
Drag options to blanks, or click blank then click option'
A30s
B10s
C5
D60s
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up timeout and interval values.
Using strings instead of numbers for retries.

Practice

(1/5)
1. What is the main purpose of a HEALTHCHECK in a Docker container?
easy
A. To restart the Docker daemon automatically
B. To update the container's image
C. To check if the container's application is running correctly
D. To increase the container's CPU usage

Solution

  1. Step 1: Understand the role of HEALTHCHECK

    The HEALTHCHECK instruction runs a command inside the container to verify if the app is working properly.
  2. Step 2: Identify what HEALTHCHECK does not do

    It does not restart Docker daemon, update images, or increase CPU usage.
  3. Final Answer:

    To check if the container's application is running correctly -> Option C
  4. Quick Check:

    HEALTHCHECK verifies app status = B [OK]
Hint: HEALTHCHECK tests app inside container, not Docker itself [OK]
Common Mistakes:
  • Confusing HEALTHCHECK with Docker daemon restart
  • Thinking HEALTHCHECK updates images
  • Assuming HEALTHCHECK changes resource usage
2. Which of the following is the correct syntax to add a health check in a Dockerfile?
easy
A. HEALTHCHECK CMD curl -f http://localhost/ || exit 1
B. HEALTHCHECK RUN curl -f http://localhost/ || exit 1
C. HEALTHCHECK EXEC curl -f http://localhost/ || exit 1
D. HEALTHCHECK CHECK curl -f http://localhost/ || exit 1

Solution

  1. Step 1: Recall Dockerfile HEALTHCHECK syntax

    The correct syntax uses HEALTHCHECK CMD followed by the command to run.
  2. Step 2: Identify incorrect keywords

    RUN, EXEC, CHECK are not valid keywords for HEALTHCHECK instruction.
  3. Final Answer:

    HEALTHCHECK CMD curl -f http://localhost/ || exit 1 -> Option A
  4. Quick Check:

    HEALTHCHECK uses CMD keyword = C [OK]
Hint: HEALTHCHECK always uses CMD keyword in Dockerfile [OK]
Common Mistakes:
  • Using RUN instead of CMD in HEALTHCHECK
  • Using EXEC or CHECK which are invalid
  • Omitting CMD keyword entirely
3. Given this Dockerfile snippet:
HEALTHCHECK CMD curl -f http://localhost/health || exit 1
CMD ["./start-server.sh"]

What will be the health status if curl fails?
medium
A. unhealthy
B. unknown
C. starting
D. healthy

Solution

  1. Step 1: Understand HEALTHCHECK command behavior

    If the command returns a non-zero exit code (like exit 1), Docker marks the container as unhealthy.
  2. Step 2: Analyze the curl failure effect

    Since curl -f fails, it triggers exit 1, so health status becomes unhealthy.
  3. Final Answer:

    unhealthy -> Option A
  4. Quick Check:

    Failed HEALTHCHECK command = unhealthy [OK]
Hint: Failed health check command sets status to unhealthy [OK]
Common Mistakes:
  • Assuming failure means healthy
  • Confusing 'starting' with unhealthy
  • Thinking status stays unknown on failure
4. You wrote this in your Dockerfile:
HEALTHCHECK CMD curl http://localhost/health || exit 1

But your container health status is always starting. What is the likely problem?
medium
A. The container has no network access
B. The HEALTHCHECK syntax is invalid
C. Docker daemon is not running
D. The health check command never succeeds within the start period

Solution

  1. Step 1: Understand 'starting' health status meaning

    'starting' means the container is still within the initial grace period before health checks are considered final.
  2. Step 2: Analyze why health check stays 'starting'

    If the command never succeeds during this period, Docker keeps status as 'starting'. Syntax is correct, Docker daemon running, and network likely accessible.
  3. Final Answer:

    The health check command never succeeds within the start period -> Option D
  4. Quick Check:

    Failing health check in start period = starting [OK]
Hint: Starting means health check not yet passed in grace period [OK]
Common Mistakes:
  • Assuming syntax error causes 'starting' status
  • Blaming Docker daemon when it runs fine
  • Ignoring network issues but they are less likely here
5. You want to create a health check that retries 3 times before marking unhealthy and waits 10 seconds between checks. Which Dockerfile snippet correctly sets this up?
hard
A. HEALTHCHECK --timeout=10s --retries=3 CMD curl -f http://localhost/health || exit 1
B. HEALTHCHECK --interval=10s --retries=3 CMD curl -f http://localhost/health || exit 1
C. HEALTHCHECK --interval=3s --retries=10 CMD curl -f http://localhost/health || exit 1
D. HEALTHCHECK --start-period=10s --retries=3 CMD curl -f http://localhost/health || exit 1

Solution

  1. Step 1: Identify correct flags for interval and retries

    --interval sets wait time between checks; --retries sets how many failures before unhealthy.
  2. Step 2: Match values to requirements

    We want 10 seconds between checks and 3 retries, so --interval=10s --retries=3 is correct. Timeout and start-period are different settings.
  3. Final Answer:

    HEALTHCHECK --interval=10s --retries=3 CMD curl -f http://localhost/health || exit 1 -> Option B
  4. Quick Check:

    Interval=10s and retries=3 flags = A [OK]
Hint: Use --interval for wait, --retries for failure count [OK]
Common Mistakes:
  • Confusing timeout with interval
  • Swapping retries and interval values
  • Using start-period instead of interval
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard