Here is a rewritten version of the article, maintaining the original structure and technical accuracy while improving clarity and flow:

How to Deploy a Docker Stack Using Swarm on a VPS

đź“… May 19, 2025
✍️ By Ariffud Muhammad

How to deploy a Docker stack with Swarm on VPS

Docker is a powerful containerization platform that simplifies application deployment and management. One of its key features is Docker Stack, which allows you to deploy and manage a group of services as a single unit within a Docker Swarm cluster.

In this guide, we’ll show you how to deploy a Docker stack using Swarm on a virtual private server (VPS). By the end, you’ll be able to scale and manage your applications across multiple servers efficiently.

What Is a Docker Stack?

A Docker stack is a collection of interrelated services that work together to form a complete application. It allows you to define, deploy, and manage multi-container applications as a single entity.

Docker Swarm, the orchestration tool behind stacks, helps ensure high availability, load balancing, and fault tolerance across clusters of Docker nodes.

Key Components:

Services: Containers running specific parts of your app (e.g., web server, database).
Tasks: Individual instances of a service running on swarm nodes.
Nodes: Servers in the swarm (manager and worker nodes).
Networks & Volumes: Enable communication and persistent storage between services.

đź’ˇ Want to understand the difference between Docker containers and images? Read this guide.

Prerequisites

To follow this tutorial, you’ll need:

– At least one VPS (preferably more for scalability).
– Docker installed on each server.

We recommend using Hostinger’s Docker VPS hosting, which comes with Docker pre-installed. The KVM 1 plan offers 1 CPU, 4 GB RAM, and 50 GB NVMe storage for $4.99/month—suitable for most use cases.

Already have a VPS? You can install Docker manually or use Hostinger’s one-click template.

Verify Docker installation with:

bash
docker --version
sudo systemctl status docker

Familiarity with basic Docker CLI commands will be helpful. Use our Docker cheat sheet as a reference.

How to Deploy a Docker Stack

Step 1: Set Up Docker Swarm

On your manager node, initialize the swarm:

bash
docker swarm init

You’ll receive a docker swarm join command. Run it on each worker node:

bash
docker swarm join --token [TOKEN] [MANAGER-IP]:2377

On the manager node, verify the cluster:

bash
docker node ls

Step 2: Create a Docker Compose File

On the manager node, create a docker-compose.yml file:

bash
sudo nano docker-compose.yml

Paste the following:

yaml
version: "3.8"
services:
  web:
    image: nginx:latest
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"

  db:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: example
    deploy:
      replicas: 1

  proxy:
    image: jwilder/nginx-proxy
    environment:
      DEFAULT_HOST: manager.vps
    deploy:
      replicas: 2

Exit and save: PLACEHOLDERf64af558c8d24ba8, then PLACEHOLDER242f203520f80696, then Enter.

Step 3: Deploy the Stack

Run the following to deploy your services:

bash
docker stack deploy -c docker-compose.yml my_stack

Check service status:

bash
docker stack services my_stack

Step 4: Test the Deployment

Open your browser and enter your VPS IP address:


http://[VPS-IP]

You should see the NGINX welcome page.

Step 5: Scale Services

To scale the web service to 5 replicas:

bash
docker service scale my_stack_web=5

Verify the change:

bash
docker service ps my_stack_web

Step 6: Update or Roll Back the Stack

To update your stack:

1. Edit docker-compose.yml:

bash
sudo nano docker-compose.yml

2. Replace the


Discover more from WIREDGORILLA

Subscribe to get the latest posts sent to your email.

Similar Posts