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
The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.
Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.
Docker Dockerfiles HEALTHCHECK Instruction The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.
No, Dockerfile instructs Docker how to build your image(s). Docker Compose instructs Docker how to run your image(s). Thx, so I have to make a dockerfile just to have the copy command ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With