Docker Swarm continuous deployment using native service health checks

Image by Wilfried Pohnke from Pixabay

Create swarm cluster

Swarm cluster contains at least one master node and optional worker nodes. This blog post primarily focuses on configuring health checks instead of swarm cluster creation. You can read more cluster creation here

$ docker swarm init --advertise-addr
Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
$ docker node ls

dxn1zf6l61qsb1josjja83ngz * manager1 Ready Active Leader

Create Service

  1. Docker image used in this article built from Github project DockerHealthCheckDemo,
  2. SSH into master node and execute the following command to create new serviceemployee_service(this is just a basic command, more details will be added later) with 2 replicas(containers)
$ docker service create --replicas 2 -p 8080:8080 --name employee_service employee_springboot:latest
$ docker service ls

9uk4639qpg7n employee_service 2/2 employee

Continuous deployment

Update the service by bringing up new container from update image and maintain delay of 120 seconds between each container update. Continuous deployment can be achieved using the following command.

$ docker service update --force --detach=false --update-parallelism=1 --update-delay=300s --update-failure-action=rollback --update-order=start-first employee_service

Container Health check

To prevent this error, we need to add custom health check for the container. There are couple of way to do it

  1. Docker provides HEALTHCHECK instruction(command) to achieve this(preferred way)
  2. Or, Add health-cmd flag to docker service update command


First, let’s look at example HEALTHCHECK instruction below


health-cmd flag

Second way of doing this is using the health-cmd command with docker service update

$ docker service update --force --detach=false --update-parallelism=1 --update-delay=300s --update-failure-action=rollback --update-order=start-first --health-cmd="curl -f http://localhost:8080/api/v1/health/find/status | grep UP || exit 1" --health-start-period=2m --health-interval=5s --health-timeout=30s employee_service
  • -health-cmd="curl -f http://localhost:8080/api/v1/health/find/status | grep UP || exit 1"
  • --health-start-period=2m
  • --health-interval=5s
  • --health-timeout=30s


  1. Clone the repo and build the image
$ git clone$ cd DockerHealthCheckDemo$ docker build -t employee_springboot .
$ docker service create --replicas 2 -p 8080:8080 --name  employee_service employee_springboot:latest
New service with two containers
$ docker service update --force --detach=false --update-parallelism=1  --update-delay=120s --update-failure-action=rollback  --update-order=start-first --update-failure-action=rollback employee_service
Docker Service Updating
Docker Service Updated with new containers


Using docker service update command and Dockerfile HEALTHCHECK instruction, we can continuously deploy applications without interrupting the user workflow.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavan Jadda

Pavan Jadda

Full Stack Developer(Java, Angular and React)