This is to show how healthcheck works in Docker Swarm.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Andrey Arapov 0e2f678bfb
1 år sedan
Dockerfile first commit 1 år sedan updates 1 år sedan first commit 1 år sedan

Docker Swarm health check example

This example shows how health check works in Docker Swarm and how it heals the service.

What does this container do?

Container runs a nginx server and it also runs a script which writes a dead status to /usr/share/nginx/html/status file on every odd run which is then picked by the health check that expects alive status from the nginx running in the container.

So every odd run the container gets unhealthy status. Then you can observe that running this container in Docker Engine alone will not restart it. Hence we will run it as a service in the Docker Swarm which will ensure the container gets restarted until it gets healthy status.

Building image

docker build -t healthcheck-test .

Setting up env

Make sure you have recent Docker Engine installed.

docker swarm init

Running the service

docker service create \
  --name test1 \
  --mount type=bind,src=/tmp,dst=/usr/share/nginx/html \
  --detach healthcheck-test

Watching the health check

First, the container will start in unhealthy mode as expected, health check will pick this and Docker Swarm will restart that container which then will start in a healthy mode.

docker ps |grep test1
docker service ls

Cleaning up

docker service rm test1
docker swarm leave --force
sudo rm -f -- /tmp/status
docker rmi healthcheck-test


If you want to restart the container manually, you need to downscale it to 0 first and then upscale it back again.

docker service scale test1=0
docker service scale test1=1