Implementing a healthcheck for a Docker Container

awsmine

Revathi Joshi

Posted on February 25, 2023

Implementing a healthcheck for a Docker Container

Among all the week 1's home-work exercises, I like the most about how you implement a healthcheck for a Docker Container. So I thought of writing an article about it

  • Docker allows us to tell the platform on how to test that our application is healthy.

  • When Docker starts a container, it monitors the process that the container runs.

  • If the process ends, the container exits.

  • We can specify certain options before the CMD operation, these includes:

  • It is self-explanatory

  • 172.17.0.2 - this is the IP Address of the EC2 instance

HEALTHCHECK --interval=5s CMD ping -c 1 172.17.0.2

--interval=DURATION (default: 30s)
--timeout=DURATION (default: 30s)
--start-period=DURATION (default: 0s)
--retries=N (default: 3)
Enter fullscreen mode Exit fullscreen mode

Please visit my GitHub Repository for CI-CD/Docker/ECS/ECR articles on various topics being updated on constant basis.

References

1. launch a container from busybox image

docker container run -dt --name busybox busybox sh

# check container is up and running
docker ps
Enter fullscreen mode Exit fullscreen mode

2. get the ip address of the busybox container

docker inspect busybox 
Enter fullscreen mode Exit fullscreen mode

3. Create a Dockerfile

FROM busybox
HEALTHCHECK --interval=5s CMD ping -c 1 172.17.0.2
Enter fullscreen mode Exit fullscreen mode

4. build the container

docker build -t monitoring .

dpocker images
Enter fullscreen mode Exit fullscreen mode

5. launch a container from the monitoring image

docker container run -dt --name monitor monitoring sh
Enter fullscreen mode Exit fullscreen mode
  • docker ps to see the healthcheck

  • depending upon the interval you specify the healthcheck would be perfomon accordings

  • it is healthy because it is able to ping the busybox container

  • HW-4-healthy.pdf

6. docker inspect monitor to see the healthy exitcode 0

7. healthy exitcode 0

8. docker stop busybox, Docker Container becomes unhealthy

docker ps
Enter fullscreen mode Exit fullscreen mode

9. specify interval=5s and retries=1

docker run -dt --name tmp --health-cmd "curl -f http://localhost" --health-interval=5s --health-retries=1 busybox sh
Enter fullscreen mode Exit fullscreen mode

10. Clean Up

  • docker stop CONTAINER NAME

  • docker rm CONTAINER NAME

  • docker rmi -f REPOSITORY:TAG NAME

  • docker rmi -f IMAGE ID

What we have done so far

  • We have successfuly learnt how to implement a healthcheck on a Docker Container.
💖 💪 🙅 🚩
awsmine
Revathi Joshi

Posted on February 25, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related