Managing the App

Let's make some updates to the app we previously deployed.

Manage services

We know that a stack is a set of related services and infrastructure that gets deployed and managed as a unit. And while that’s a fancy sentence full of buzzwords, it reminds us that the stack is built from normal Docker resources; networks, volumes, secrets, services, etc. This means we can inspect them with their normal Docker commands: docker network, docker volume, docker secret, docker service, etc.

With this in mind, it’s possible to use the docker service command to manage services that are part of the stack. A simple example would be using the docker service scale command to increase the number of replicas in the appserver service. However, this is not the recommended method!

Imperative method

Here’s a quick example of why the imperative method (making changes via the CLI) is bad:

Imagine you have a stack deployed from the docker-stack.yml file that you cloned from GitHub earlier in the chapter. This means you have two replicas of the appserver service. If you use the docker service scale command to change that to 4 replicas, the current observed state of the cluster will be 4 running replicas, but the stack file will still define 2.

Admittedly, that doesn’t sound like the end of the world. However, imagine you then edit the stack file to use a newer image, and roll it out the recommended way with the docker stack deploy command. As part of this rollout, the number of appserver replicas in the cluster will be rolled back to 2, because you never updated the stack file to 4 replicas.

For this kind of reason, it’s recommended to make all changes to the application via the stack file and to manage the stack file in a proper version control system.

Declarative method

The recommended method is the declarative method, which uses the stack file as the ultimate source of truth. As such, all changes to the stack should be made to the stack file, and then the updated stack file should be used to redeploy the app.

Let’s walk through the process of making a couple of declarative changes to the stack.

We’ll make the following changes:

  • Increase the number of appserver replicas from 2 to 10
  • Increase the stop grace period for the visualizer service to 2 minutes

Update the stack file

Edit the docker-stack.yml file and update the following two values:

  • .services.appserver.deploy.replicas=10
  • .services.visualizer.stop_grace_period=2m

The relevant sections of the stack file will now look like this:

Get hands-on with 1000+ tech skills courses.