Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Health Check command for docker(1.12) container (Not in Dockerfile!)

Docker Version 1.12, I got a Dockerfile from Here

FROM nginx:latest

RUN touch /marker

ADD ./check_running.sh /check_running.sh
RUN chmod +x /check_running.sh

HEALTHCHECK --interval=5s --timeout=3s CMD ./check_running.sh

I'm able to roll the updates and health checks with check_running.sh shell script. Here, the check_running.sh script is copied to image, so the launched container has it.

Now, my question is there any way to Health Check from out side of the container and script also located outside.

I'm excepting a health check command to get the container performance(Depends on what we wrote in script), IF the container is not performing good it should roll-back to previous version ( Kind of a process that monitors the containers, if it is not good, it should roll-back to previous)

Thanks

like image 984
Veerendra Avatar asked Aug 09 '16 05:08

Veerendra


People also ask

What is health check in dockerfile?

Health Check of Docker Containers. One of the new features in Docker 1.12 is how health check for a container can be baked into the image definition. And this can be overridden at the command line. Just like the CMD instruction, there can be multiple HEALTHCHECK instructions in Dockerfile but only the last one is effective.

How to check if a docker container is healthy or unhealthy?

Using the Docker Healthcheck Command. 1 starting – Initial status when the container is still starting. 2 healthy – If the command succeeds then the container is healthy. 3 unhealthy – If a single run of the takes longer than the specified timeout then it is considered unhealthy. If a health check fails then the will run ...

What is heathcheck instruction in Docker?

A HEATHCHECK instruction determines the state of a Docker Container. It determines whether the Container is running in a normal state or not. It performs health checks at regular intervals. The initial state is starting and after a successful checkup, the state becomes healthy.

How does Docker know if a service is running?

With no health check specified, Docker has no way of knowing whether or not the services running within your container are actually up or not. In Docker, health checks can be specified in the Dockerfile as well as in a compose file. I'll cover both implementations in a later step.


3 Answers

is there any way to Health Check from out side of the container and script also located outside.

Kind of a process that monitors the containers, if it is not good, it should roll-back to previous

You have several options:

  1. From outside, you run a process inside the container to check its health with docker exec. This could be any sequence of shell commands. If you want to keep your scripts outside of the container, you might use something like cat script.sh | docker exec -it container sh -s.
  2. You check the container health from outside the container, e.g. by looking for a process that should be running inside the container (try to set a security profile and use ps -Zax or try looking for children of the daemon), or you can give each container a specific user ID with --user 12345 and then look for that or e.g. connecting to its services. You'd have to make sure it's running inside the right container. You can access the containers' filesystem below /var/lib/docker/devicemapper/mnt/<hash>/rootfs.
  3. You run a HEALTHCHECK inside the container and check its health with docker inspect --format='{{json .State.Health.Status}}' <containername> combined with e.g. a line in the Dockerfile: HEALTHCHECK CMD wget -q -s http://some.host to check the container has internet access.

I'd recommend option 3, because it's likely to be more compatible with other tools in the future.

like image 94
Dej Avatar answered Sep 24 '22 16:09

Dej


Just got comment from a blog!. He refered Docker documentation HealthCheck section. There is a health check "option" for docker command to "override" the dockerfile defaults. I have not checked yet!. But it seems good for me to get what I want. Will check and update the answer!

like image 43
Veerendra Avatar answered Sep 22 '22 16:09

Veerendra


The Docker inspect command lets you view the output of commands that succeed or fail

docker inspect --format='{{json .State.Health}}' your-container-name
like image 21
f-society Avatar answered Sep 25 '22 16:09

f-society