...

/

Inspecting the Stack File: Services

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.

Press + to interact
web-fe:
image: Educative-Content/ddd-book:swarm-app <<---- Create all replicas with this image
command: python app.py <<---- Run this command when each replica starts
deploy:
replicas: 4 <<---- Deploy 4 replicas
update_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 replicas
failure_action: rollback ------┘ and perform a rollback if you encounter issues
placement: ------┐
constraints: | Only run replicas on worker nodes
- 'node.role == worker' ------┘
restart_policy: ------┐ Only restart replicas if
condition: 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 minutes
networks:
- counter-net <<---- Connect replicas to the "counter-net" network
ports:
- published: 5001 ------┐ Publish the service externally on port 5001 and
target: 8080 ------┘ map traffic to each replica on port 8080
volumes:
- type: volume
source: counter-vol ------┐ Mount the "counter-vol" volume to
target: /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 ...