Latest
Docker 1 min read

Deploy an Application Stack to Docker Swarm

Deploy an Application Stack to Docker Swarm
Docker · November 1, 2026

Docker lab/version note: use supported Linux-container tools and patched images. Check version compatibility before running the examples. Docker, Swarm and platform-specific deployment steps were not tested live.

What this guide covers

A Swarm stack describes services that the cluster should run. Every eligible node needs access to the same application image, so build and publish the image before deploying the stack.

Before you start

Use a working Swarm cluster and the Node.js application from the container guide. You need a registry repository that all worker nodes can reach and credentials with suitable permissions.

Step 1: Build and publish the image

From the application directory on the build machine:

read -r -p 'Your registry/repository:release image reference: ' APP_IMAGE
export APP_IMAGE
docker login
docker build --pull -t "$APP_IMAGE" .
docker push "$APP_IMAGE"
docker buildx imagetools inspect "$APP_IMAGE"

Enter your own registry/repository reference. Record the pushed digest and use that exact image for deployment where repeatability matters.

Step 2: Create the stack file

On the manager, save this as stack.yml:

version: '3.8'
services:
  web:
    image: ${APP_IMAGE}
    environment:
      PORT: '3000'
      APP_VERSION: swarm-release-1
    networks: [app]
    ports:
      - target: 3000
        published: 8080
        protocol: tcp
        mode: ingress
    healthcheck:
      test: ['CMD', 'node', '-e', "fetch('http://127.0.0.1:3000/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 10s
    stop_grace_period: 15s
    deploy:
      replicas: 2
      placement:
        constraints: [node.role == worker]
        max_replicas_per_node: 1
      resources:
        reservations:
          cpus: '0.10'
          memory: 32M
        limits:
          cpus: '0.50'
          memory: 128M
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 30s
      update_config:
        parallelism: 1
        order: stop-first
        delay: 10s
        monitor: 30s
        failure_action: rollback
      rollback_config:
        parallelism: 1
        order: stop-first
networks:
  app:
    driver: overlay

Swarm uses the supplied image; it does not build from source during stack deploy. Review resource limits and update settings for the real application.

Step 3: Deploy from the manager

Set APP_IMAGE on the manager to the published image reference, then run:

: "${APP_IMAGE:?Export the approved image reference on this manager}"
sudo --preserve-env=APP_IMAGE docker stack config -c stack.yml > rendered-stack.yml
sudo --preserve-env=APP_IMAGE docker stack deploy --with-registry-auth -c stack.yml app
sudo docker stack services app
sudo docker stack ps app --no-trunc
sudo docker service inspect app_web --pretty
sudo docker service logs --tail 50 app_web
curl --fail http://192.168.50.22:8080/healthz

If using a private registry, sign in on the manager with credentials suitable for the workers. --with-registry-auth forwards registry authentication to the Swarm agents.

Step 4: Check the published service

Inspect task errors and request the published service port on an allowed node address. Check more than one node to verify the routing path. A requested replica count does not mean all tasks started successfully.

Check the result

The desired tasks should be running the expected image and return the expected response. Keep persistent data and backups separate from disposable service tasks.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Decide How SonarQube Handles Generated Code

Sonar November 2, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.