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
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.