34

We are deploying our services using a Docker Compose file and Docker Swarm. I was wondering if there is any difference between putting the healthcheck inside the Dockerfile or if it is better to put it in the docker-compose.yml.

I feel like I've read through all available documentation, but couldn't find anything.

docker-compose.yml

healthcheck:
    test: ["CMD", "curl", "-f", "http://localhost:8081/ping"]
    interval: 30s
    timeout: 10s

Dockerfile

HEALTHCHECK --interval=30s --timeout=10s CMD curl -f http://localhost:8081/ping
kev
  • 8,928
  • 14
  • 61
  • 103

1 Answers1

35

Adding health check to the Dockerfile, will make the health-check part of the image, so that anyone pulling the image from the registry will get the health check by default.

Compose files are usually less shared than the actual docker images they run. The dockercompose health-check allows adding/overrriding healthchecks for images for someone who is not creating the image but rather is pulling it from a remote registry. It is more suitable in situations where the pulled image doesn't have a health-check by default.

In your case, since you are creating the image, adding the health-check to the dockerfile makes more sense.

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 1
    Just to remember, it also makes the image a little bit bigger, because it will need to have curl or wget installed. – John John Pichler Feb 28 '20 at 08:24
  • 6
    @JohnJohnPichler: the image will need to include curl or wget, regardless of whether the health check is specified in the `Dockerfile` or in `docker-compose.yml`. – jdhildeb Mar 03 '20 at 18:38