Implementing a healthcheck for a Docker Container
Tags: docker, ec2, healthcheck, busybox
Published: 2023-02-25 07:23:14

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)
```

Please visit my **[GitHub Repository for CI-CD/Docker/ECS/ECR articles](https://github.com/awsmine/Docker_projects)** on various topics being updated on constant basis.

# References

- [Dockerfile reference](https://docs.docker.com/engine/reference/builder/)

- [HEALTHCHECK](https://docs.docker.com/engine/reference/run/#healthcheck)




## 1. launch a container from busybox image

```
docker container run -dt --name busybox busybox sh

# check container is up and running
docker ps
```



## 2. get the ip address of the busybox container

```
docker inspect busybox 
```

- running for 5s

- [HW-4-get-IP-busybox-container.pdf](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-get-IP-busybox-container.pdf)


## 3. Create a Dockerfile

```
FROM busybox
HEALTHCHECK --interval=5s CMD ping -c 1 172.17.0.2
```

## 4. build the container

```
docker build -t monitoring .

dpocker images
```

## 5. launch a container from the monitoring image

```
docker container run -dt --name monitor monitoring sh
```


- 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](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-healthy.pdf)

## 6. docker inspect monitor to see the healthy exitcode 0

- [HW-4-inspect-busybox.pdf](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-inspect-busybox.pdf)

## 7. healthy exitcode 0

- [HW-4-healthy-exitcode-0.pdf](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-healthy-exitcode-0.pdf)

## 8. docker stop busybox, Docker Container becomes unhealthy

```
docker ps
```

- unhealthy

- [HW-4-busybox-stop-container-monitor-unhealthy.pdf](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-busybox-stop-container-monitor-unhealthy.pdf)


## 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
```

- [HW-4-retries-1.pdf](https://github.com/awsmine/aws-bootcamp-cruddur-2023/blob/main/_docs/assets/week_1/HW-4-retries-1.pdf)

## 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.