Rollout and Rollback In Docker Swarm - saviovettoor/DevOps-wiki GitHub Wiki

Deploying a service

docker service create --name nginxserver --replicas 4 --publish 8080:80 nginx:latest

4 replicas - Service name nginxserver - expose over 8080

Rolling update

When you rollout and update following is the state:

  • Preparing
  • Ready
  • Starting
  • Running

Using the following options in the service configuration, we can customize the way the update is done:

--update-parallelis: number of tasks to update at the same time.
By default, the scheduler updates 1 task at a time. You can pass the --update-parallelism flag to configure the maximum number of service tasks that the scheduler updates simultaneously.

--update-delay: time to wait before updating the next batch of tasks.

Update-parallelism

docker service create --replicas 3 --name redis --update-delay 10s redis:3.0.6

Inspect the redis service:

docker service inspect --pretty redis

Now you can update the container image for redis. The swarm manager applies the update to nodes according to the UpdateConfig policy:

docker service update --image redis:3.0.7 redis

The scheduler applies rolling updates as follows by default:

  • Stop the first task.
  • Schedule update for the stopped task.
  • Start the container for the updated task.
  • If the update to a task returns RUNNING, wait for the specified delay period then start the next task.
  • If, at any time during the update, a task returns FAILED, pause the update.

Lets check new image in the desired state

docker service inspect --pretty redis

Rollback

Swarm knows the previous specification of the redis service, it’s possible to rollback to this one with the following command:

docker service rollback redis

Automatic rollback

We can automatically rollback to our previous image based on the healthcheck for that image which you created should contain instruction about HEALTHCHECK link

docker service create --name whoami --replicas 2 --update-failure-action rollback --update-delay 10s --update-monitor 10s 
--publish 8000:80 nginx

We specify the --update-failure-action flag and give it the rollback value so that an unsuccessful update will trigger a rollback automatically.

⚠️ **GitHub.com Fallback** ⚠️