Docker Swarm Setup and Application Deployment

Introduction

This guide covers setting up Docker Swarm and deploying a small example application using Docker services. We'll initialise the swarm, create overlay networks, and deploy services for a simple voting application.

Step 1: Verify Docker Installation

Before initialising Docker Swarm, check your Docker installation:

docker info

This command provides system details and confirms that Docker is installed correctly.

Step 2: Initialise Docker Swarm

To enable Docker Swarm, initialise it with the following command:

docker swarm init

This makes the current node the swarm manager. To view swarm nodes, run:

docker node ls

Step 3: Create Overlay Networks

Set up overlay networks to enable communication between services:

sudo docker network create -d overlay frontend
sudo docker network create -d overlay backend

Step 4: Deploy the Voting App Services

Deploy individual services for the voting app. Start with the frontend voting app:

sudo docker service create --name vote -p 80:80 --network frontend --replicas 2 dockersamples/examplevotingapp_vote:before

Then deploy Redis as the backend cache:

sudo docker service create --name redis --network frontend --replicas 2 redis:3.2

Deploy the worker service to process votes:

sudo docker service create --name worker --network frontend --network backend dockersamples/examplevotingapp_worker

Step 5: Deploy the Database Service

Create the database service using a volume for persistent storage:

sudo docker service create \
--name db \
--mount type=volume,source=db-data,target=/var/lib/postgresql/data \
--network backend \
postgres:9.4

The volume db-data is created in /var/lib/docker/volumes/db-data/_data.

Alternatively, use a bind mount for more control over data storage:

sudo docker service create \
--name db \
--mount type=bind,source=/home/user/data,target=/var/lib/postgresql/data \
--network backend \
postgres:9.4

Step 6: Deploy the Results Service

Deploy the results service for the voting app:

sudo docker service create --name result --network frontend -p 50001:80 dockersamples/examplevotingapp_result:before

Step 7: Verify Services

List all running services to verify the deployment:

docker service ls

Check the task allocation for specific services:

docker service ps result

Repeat for other services like redis and worker.