Docker container health check
For example one webhook is running as a container
( how to know whether it is healthy or unhealthy ).
How the health check will make an added advantage to our application ?
In some scenarios, the container will be in running state, but we don’t see any interaction between the application container and the client.
One of the reason for these kind of behavior may be load.
- In the above scenario we need to troubleshoot to understand the behavior of the container.
( we can troubleshoot the container with several approaches , for example,
docker ps
docker ps -a ( analyst why container is in stopped state and go through that specific container logs ).
( but in stopped state container will not provide the logs , then what is the best way to troubleshoot ??? )
How to check the healthcheck of the container using the healthcheck command in the Dockerfile ?
What is healthcheck ?
Healthcheck are exactly what they sound like - a way of checking the health of some resource. In the case of Docker, a health check is a command used to determine the health of a running container
When a healthcheck command specified, it tells Docker how to test the container to see if it’s working. With no health check specified, docker has no way of knowing whether or not the services running within your container are actually up or not
Take an example of python was flask framework
pythonapp/Dockerfile
FROM python:2.7
MAINTAINER Madhu Sudhan reddy "jmstechhome@gmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py”]
Steps
#Simple Python Helloworld app using docker
Build the image using the following command
docker build -t pythonapp:v1 .
Run the Docker container using the command shown below.
docker run -it -p 80:5000 --name myapp pythonapp:v1
The application will be accessible at
http://<host_ip>:80
pythonapp/app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello world!!"
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')
pythonapp/requirements.txt
flask
>>>>
Lets start with the requirements.txt:
Flask==0.12.2
And the Dockerfile
FROM python:3.6-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD [“python”, “app.py”]
And finally.app.py;
from flask import Flask
app = Flask(_name_)
@app.route(‘/‘)
def hello_world():
return ‘Hello world’
if_name_==‘_main_’:
app.run(host=“0.0.0.0”)
Now lets build the container
docker build -t docker-flask
This should build pretty quickly, Then we will run the container
docker run —rm —name docker-flask -p 5000:5000 docker-flask
Now test by opening up your browser to localhost:5000 you should see “Hello world”
Add a health check to the Dockerfile
Since the goal of the container is to serve the traffic on port 5000. Our health check should make sure this is happening
A health check is configured In the Dockerfile using the HEALTHCHECK instruction. There are two ways to use the HEALTHCHECK Instruction
HEALTHCHECK ( OPTIONS ) CMD command
Or if you want to disable a health check from a parent image:
HEALTHCHECKNONE
So we are obviously going to use the first. So lets add the HEALTHCHECK instruction , and we will use curl to ensure that our app is serving traffic on port 5000
So add this line to the Dockerfile right before the last line (CMD)
HEALTHCHECK CMD curl —fail http://localhost:5000/ || exit 1
In this case, we are using the default options, which are interval 30s, timeouts 30s, start-period 0s and retries 3. Read the health check instruction reference for more information on the options.
FROM python:3.6-alpine
COPY . /app
WORKDIR /app
RUN app add curl
RUN pip install -r requirements.txt
HEALTHCHECK CMD curl —fail http://localhost:5000/ || exit 1
CMD [“python”, “app.py” ]
See the health status
Lets rebuild and run our container
docker build -t docker-flask.
docker run —rm —name docker-flask -p 5000:5000 docker-flask
Now lets take a look at the health status. Notice we have the —name option to the above command so we can early inspect t the container
docker inspect —format=‘{{Json.State.Health}}’ docker-flask
If you run the immediately after the container starts, you will see status is starting
{“Status”:”starting”,”FailingStreak” :0, “Log” :[]}
And after that the health check name ( after the default interval of 30s):
{“Status”:”starting”,”FailingStreak” :0, “Log” :[ ( “start”: “27-10-23773ry23ry3ry82r3yr8”, “End”:” 2017-07-29”}