Managing the App

Manage services

There are two ways to manage Docker stacks:

  • Imperatively

  • Declaratively

The imperative method is where you run Docker commands to make changes to the stack. For example, using the docker service scale command to increase and decrease the number of service replicas.

The declarative method is where you make all changes via the stack file. For example, if you want to change the number of service replicas, you edit the stack file with the desired replica count and run another docker stack deploy command.

Declarative method

The declarative method is the preferred method.

Consider the following example that demonstrates why you should manage stacks declaratively.

Imagine you’ve deployed an app from a stack file that includes a reporting service and a catalog service. The stack file includes other services that are part of the app, but we’re only interested in these two. It’s currently running five replicas of the reporting service, but year-end reporting has started, and it’s experiencing slow performance due to increased demand. You decide to run an imperative docker service scale command to increase the number of reporting replicas to 10. This fixes the performance issues, but the current state of the app is out of sync with the stack file — the stack file defines five replicas, but the cluster is running 10. Later in the day, a colleague is tasked with rolling out a new version of the catalog service — the catalog service is part of the same app and, therefore, is defined in the same stack file as the reporting service. Your colleague makes the changes declaratively by editing the stack file with the new version of the catalog service and running a docker stack deploy command to push the updates. When Docker receives the updated version of the stack file, it rolls out the new version of the catalog service and changes the number of reporting replicas back to five. This will cause the reporting service to start running slowly again.

This is why you should make all changes declaratively via your stack files and manage them in a version control system.

Update the stack file

Getting back to the app you deployed, let’s do the following:

  • Increase the number of web-fe replicas from 4 to 10

  • Update the web-fe service to the newer swarm-appv2 image

Edit the compose.yaml, increase the web-fe replica count to 10, and change the image to swarm-appv2.

Get hands-on with 1300+ tech skills courses.