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:
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:
This makes the current node the swarm manager. To view swarm nodes, run:
Step 3: Create Overlay Networks
Set up overlay networks to enable communication between services:
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:
Then deploy Redis as the backend cache:
Deploy the worker service to process votes:
Step 5: Deploy the Database Service
Create the database service using a volume for persistent storage:
--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:
--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:
Step 7: Verify Services
List all running services to verify the deployment:
Check the task allocation for specific services:
Repeat for other services like redis
and worker
.