Home > OS >  Docker HEALTHCHECK not working as expected
Docker HEALTHCHECK not working as expected

Time:05-17

I have a php project to test healthchecks with three files in it:

index.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
    <h1>Hi from container: <?php echo gethostname(); ?> </h1>
 </body>
</html>

health.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
  <h1>hi</h1>
 </body>
</html>

Dockerfile:

FROM php:8.0-apache
EXPOSE 80
COPY ["./", "/var/www/html/"]
HEALTHCHECK CMD curl --fail http://localhost/health.php exit 1 

I then start the image using docker run -p 8000:80 phpdemo_local

When running the dockerfile it starts up but if i do a docker ps i get the following:

CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                             PORTS     NAMES
e980991ce911   phpdemo_local:latest   "docker-php-entrypoi…"   42 seconds ago   Up 42 seconds (health: starting)   80/tcp    nice_pare

If I open my browser and browse to http://localhost:8000/health.php i get the following response: hi, this is obviously a HTTPStatusCode 200 so my healthcheck should also be passing cause i check for status 200

but for some reason docker ps returns the status "starting" and then eventually the health status changes to "unhealthy".

What am i doing wrong here?

I feel like the HEALTHCHECK url specified is not accessible to the healthchech ?!?

CodePudding user response:

You need to have || between the curl command and the exit command, so the exit only is executed if curl fails. Like this

HEALTHCHECK CMD curl --fail http://localhost/health.php || exit 1 
  • Related