Hello Guys in this post I wish to explore more intresting instructions HEALTHCHECK which can be used in Dockerfile.
The most common requirement for any real-time projects monitoring using a side-car container which could run in parallel and try to check the process or application URL check or container reachability using ping command etc.
Dockerfie instruction HEALTHCHECK |
In Dockerfile we can have HEALTHCHECK instruction that allows us to know the condition of an application test runs as expected or not, when a container runs this will be returns a status as healthy, unhealthy based on the HEALTHCHECK command exit code. If the exit code 0 then returns healthy otherwise unhealthy.
HEALTHCHECK [options] CMD [health check command]
Example:
HEALTHCHECK --interval=3s CMD ping c1 172.17.0.2
here are the Healthcheck options
- --interval=time in sec (duration 30s is default)
- --timeout=time in sec (duration 30s is default)
- --start-period=time in sec (duration 0s is default)
- --retries=3 () default 3
Let's jump into experiment mode:
docker run -dt --name main-target busybox sh; docker ps docker inspect main-target
More specific
To get only IPaddress of a containter use the following format option:alias dcip="docker inspect \ -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' " ipdc main-targetNote: when you define the alias for ipdc important point don't miss space at the of the line.
Guys, here is glimpse of 'ping' command exit code:
ping -c1 google.com ; echo $? ping -c1 shekhar.com ; echo $?Observe that exit codes values, if a website exits returns 0 if not non-zero.
#File: Dockerfile FROM busybox LABEL AUTHOR Pavan Deverakonda HEALTHCHECK --interval=5s CMD ping -c 1 172.17.0.2
docker build -t monitor . docker run -dt --name monping monitor; docker ps
alias s="docker inspect -f '{{json .State}}' " s monping |jq
docker inspect output filter with jq for HEALTHCHECK |
USECASE 2: HEALTHCHECK for web applications
The following command returns HTML content which may be multiple lines
curl http://devopshunter.blog.com/testLet's make this command usage in minimal way using -f or --fail options in curl command:
# Fail in silently single liner curl http://devopshunter.blog.com/test.txt -f
curl command with --fail or -f option |
Run a container with a healthcheck command using. A Linux command that checks http uri using `curl` that returns HTML code or HTTP code as per the web applicaiton availability.
docker run -dt --name health-con \ --health-cmd "curl --fail http://localhost" busyboxHere we have not used any HEALTHCHECK options, so it will try to check by running health-cmd 30sec interval 3 retries and timeout for each as 30sec. that means after 2 minutes you can get the health status as 'unhealthy'. Because busybox don't run any web server inside the container.
We can check the health status of self container or other container which is accessable. Otherwise if it shared a network with other container which is running a web server.
#File: Dockerfile FROM busybox LABEL AUTHOR Pavan Deverakonda HEALTHCHECK --interval=5s CMD curl --fail http://localhost
Build the monitoring image that contains HEALTHCHECK instruction.
docker build -t moncurl . docker imagesFor now we will test the same busybox container - unhealthy status.
docker run -dt --name moncurl-con moncurl sh # Check the container health watch docker ps #cleanup docker rm -v -f health-conNow let's see the interval option how it will impact a container health:
docker run -dt --name health-con --health-cmd "curl --fail http://localhost" --health-interval=3s busybox watch docker psMy observation - at 0s(when container started) healthcheck starts after 3s test it, retries 3 times that means 3times 3s = 9s you will get the health status changed.
USECASE 3: HEALTHCHECK with Interval and Retries options
We can run a container to check the health with options interval and retries together as:docker run -dt --name health-con3 --health-cmd "curl -f http://localhost" --health-interval=3s --health-retries=1 busybox watch docker ps
docker run -dt --name health-con3 --health-cmd "curl -f http://localhost" --health-interval=3s --health-retries=1 nginx watch docker ps
healthy status |
#File: Dockerfile FROM nginx LABEL AUTHOR Pavan Deverakonda HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost || exit 1 EXPOSE 80Now build the image
docker build -t moncurl:2 . docker imagescreate the container with that image:
docker run -dt --name health-con2 moncurl:2 sh
No comments:
Post a Comment