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-target
Note: 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.
Get the main-target container IPAddress from the docker inspect command output.
Now we will create a Dockerfile with the following code:
#File: Dockerfile
FROM busybox
LABEL AUTHOR Pavan Deverakonda
HEALTHCHECK --interval=5s CMD ping -c 1 172.17.0.2
Note: You could find the IP from previous docker inspect command.
Let's call this image as monitor
docker build -t monitor .
docker run -dt --name monping monitor; docker ps
Observe the STATUS column for corresponding containers.
alias s="docker inspect -f '{{json .State}}' "
s monping |jq
|
docker inspect output filter with jq for HEALTHCHECK |
USECASE 2: HEALTHCHECK for web applications
Let's see the 'curl' command usage in general
The following command returns HTML content which may be multiple lines
curl http://devopshunter.blog.com/test
Let'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" busybox
Here 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 images
For 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-con
Now 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 ps
My 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:
UNHEALTHY
docker run -dt --name health-con3 --health-cmd "curl -f http://localhost" --health-interval=3s --health-retries=1 busybox
watch docker ps
HEALTHY
docker run -dt --name health-con3 --health-cmd "curl -f http://localhost" --health-interval=3s --health-retries=1 nginx
watch docker ps
|
healthy status |
Let's build a Healthcheck image
#File: Dockerfile
FROM nginx
LABEL AUTHOR Pavan Deverakonda
HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost || exit 1
EXPOSE 80
Now build the image
docker build -t moncurl:2 .
docker images
create the container with that image:
docker run -dt --name health-con2 moncurl:2 sh
Please comment and share with your friends!