Inspecting the Stack File: Services
Let's examine the services section of the stack file.
Services are where most of the action happens. Our application defines two, and we’ll look at each in turn.
The web-fe
service
As you can see, the web-fe
service defines an image, an app, the desired number of replicas, rules for updating the app, a restart policy, which network to connect to, which ports to publish, and how to mount a volume. That’s a lot to take in, so we’ve annotated the file below. Take a minute to read through the file and annotations.
web-fe:image: Educative-Content/ddd-book:swarm-app <<---- Create all replicas with this imagecommand: python app.py <<---- Run this command when each replica startsdeploy:replicas: 4 <<---- Deploy 4 replicasupdate_config: ------┐ When you perform an update,parallelism: 2 | update 2 replicas at a time,delay: 10s | wait 10 seconds in between each set of two replicasfailure_action: rollback ------┘ and perform a rollback if you encounter issuesplacement: ------┐constraints: | Only run replicas on worker nodes- 'node.role == worker' ------┘restart_policy: ------┐ Only restart replicas ifcondition: on-failure | they've failed (non-zero return code),delay: 5s | wait five seconds between each restart attempt,max_attempts: 3 | only try three restarts,window: 120s ------┘ and give up trying after two minutesnetworks:- counter-net <<---- Connect replicas to the "counter-net" networkports:- published: 5001 ------┐ Publish the service externally on port 5001 andtarget: 8080 ------┘ map traffic to each replica on port 8080volumes:- type: volumesource: counter-vol ------┐ Mount the "counter-vol" volume totarget: /app ------┘ "/app" in each service replica
The image
key is the only mandatory key and tells Docker which image to use when creating service replicas. Swarm stacks don’t support building images at deploy time, so the image must exist before you deploy the app. Docker is also opinionated and assumes you want to pull images from Docker Hub. However, you can add the registry’s DNS name ...