In this scenario, you will learn how the new Healthcheck instruction provides visibility into the state of applications running inside containers.

Steps
Add Healthcheck for Containers
Step 1 - Creating Service
The new Healthcheck functionality is created as an extension to the Dockerfile and defined when a Docker image is built.
Create HTTP Service with a Healthcheck
The Dockerfile below extends an existing HTTP service and adds a healthcheck.
The healthcheck will curl the HTTP server running every second to ensure it's up. If the server responds with a non-200 request, curl will fail and an exit code 1 will be returned. After three failures, Docker will mark the container as unhealthy.
The format of the instruction is HEALTHCHECK [OPTIONS] CMD command.
FROM katacoda/docker-http-server:health HEALTHCHECK --timeout=1s --interval=1s --retries=3 \ CMD curl -s --fail http://localhost:80/ || exit 1
Currently, Healthcheck supports three different options:
interval=DURATION (default: 30s). This is the time interval between executing the healthcheck.
timeout=DURATION (default: 30s). If the check does not finish before the timeout, consider it failed.
retries=N (default: 3). How many times to recheck before marking a container as unhealthy.
The command executing must be installed as part of the container deployment. Under the covers, Docker will use docker exec to execute the command.
Build and Run
Before continuing, build and run the HTTP service.
docker build -t http .
By default it will start in a healthy state.
docker run -d -p 80:80 --name srv http
In the next steps we'll cause the HTTP Server to start throwing errors.
You’ll love Katacoda

Guided Path
Knowing what you need to know is the hardest part. Our guided pathways help build your knowledge around real-world scenarios.

Learn By Doing
The best way to learn is by doing. All our tutorials are interactive with pre-configured live environments ready for you to use.

Stay up-to-date
It's a competitive industry. Your skills need to keep up with the latest approaches. Katacoda keeps your skills up-to-date.
You’ll love Katacoda

Guided Path
Knowing what you need to know is the hardest part. Our guided pathways help build your knowledge around real-world scenarios.

Learn By Doing
The best way to learn is by doing. All our tutorials are interactive with pre-configured live environments ready for you to use.

Stay up-to-date
It's a competitive industry. Your skills need to keep up with the latest approaches. Katacoda keeps your skills up-to-date.