Docker container – health check

HEALTHCHECK instruction is used to tell the Docker how to test a container to see if its still working or not.  This is used to detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server is still running.

In this post, I am going to show an example how we can add this instruction in the Dockerfile and test this.

Refer my code @java-rest-docker

Here is my Dockerfile


FROM java:8

WORKDIR /opt

ADD hello-rest-${project.version}-jar-with-dependencies.jar /opt/helloRest.jar
ADD bin /opt/bin

RUN chmod +x /opt/helloRest.jar
RUN chmod +x /opt/bin/run.sh

CMD ["/opt/bin/run.sh"]

EXPOSE 8080

HEALTHCHECK --interval=1m --timeout=3s CMD curl -f http://localhost:8080/api/greeting || exit 1

Check the last line of my Dockerfile where I instruct the docker to access localhost:8080/api/greeting in every 1 min and do the health check and fail it if there are any issues. If the exit status 0 when the container is healthy. If it’s 1, then it’s not in healthy status.

My docker CE version is 17.06.2-ce.

Now Build the image and the run the docker container.Then run this command and check the health check output. Here replace the container name “elegant_bartik” with your local container name.


  docker inspect --format "{{json .State.Health }}" elegant_bartik

Output:
{“Status”:”healthy”,”FailingStreak”:0,”Log”:[{“Start”:”2017-09-19T18:23:54.612256172-04:00″,”End”:”2017-09-19T18:23:54.652678939-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 1022 0 –:–:– –:–:– –:–:– 1100\nHello World”},{“Start”:”2017-09-19T18:24:54.652833383-04:00″,”End”:”2017-09-19T18:24:54.696685407-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 842 0 –:–:– –:–:– –:–:– 916\nHello World”},{“Start”:”2017-09-19T18:25:54.696867514-04:00″,”End”:”2017-09-19T18:25:54.748451647-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 814 0 –:–:– –:–:– –:–:– 846\nHello World”},{“Start”:”2017-09-19T18:26:54.74862166-04:00″,”End”:”2017-09-19T18:26:54.798137953-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0\r100 11 100 11 0 0 663 0 –:–:– –:–:– –:–:– 687\nHello World”},{“Start”:”2017-09-19T18:27:54.798446904-04:00″,”End”:”2017-09-19T18:27:54.845142125-04:00″,”ExitCode”:0,”Output”:” % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0Hello World\r100 11 100 11 0 0 991 0 –:–:– –:–:– –:–:– 1100\n”}]}

Advertisements