Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Docker Swarm

Docker Swarm is a native clustering and orchestration tool for Docker. This guide covers key concepts, steps to create and manage a Docker Swarm, examples, and best practices for using Docker Swarm with Express.js applications.

Key Concepts of Docker Swarm

  • Swarm: A cluster of Docker nodes (machines running Docker) where you deploy services.
  • Node: An individual machine within a swarm. It can be a manager node or a worker node.
  • Service: The definition of the tasks (containers) to be run on the nodes in the swarm.
  • Task: A single instance of a container running in a service. A task is scheduled on a node within the swarm.
  • Manager Node: A node that manages the swarm, maintains cluster state, and schedules tasks.
  • Worker Node: A node that receives and executes tasks from the manager nodes.

Setting Up the Project

Initialize a new Express.js project and create a Dockerfile:

// Initialize a new project
// npm init -y

// Install Express
// npm install express

// Create the project structure
// mkdir src
// touch src/index.js Dockerfile .dockerignore .gitignore

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application:

Example: index.js

// src/index.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Docker Swarm!');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

Creating a Dockerfile

Create a Dockerfile to containerize your Express application:

Example: Dockerfile

// Dockerfile
FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Run app when the container launches
CMD ["node", "src/index.js"]

Initializing a Docker Swarm

Initialize a Docker Swarm on your manager node:

// Initialize a Docker Swarm
docker swarm init

Adding Nodes to the Swarm

Add worker nodes to the swarm using the join token provided by the manager node:

// Get the join token for worker nodes
docker swarm join-token worker

// Run the following command on each worker node to join the swarm
docker swarm join --token  :2377

Creating a Docker Compose File for Swarm

Create a docker-compose.yml file to define the services for your Docker Swarm:

Example: docker-compose.yml

// docker-compose.yml
version: '3.8'
services:
  web:
    image: your-dockerhub-username/my-express-app:latest
    ports:
      - "3000:3000"
    deploy:
      replicas: 3
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
    networks:
      - webnet

networks:
  webnet:

Deploying the Stack to the Swarm

Deploy the stack to the Docker Swarm using the docker stack deploy command:

// Deploy the stack to the swarm
docker stack deploy -c docker-compose.yml my_stack

// Check the status of the services
docker service ls

// Check the status of the tasks
docker service ps my_stack_web

Scaling the Service

Scale the service to the desired number of replicas:

// Scale the service to 5 replicas
docker service scale my_stack_web=5

// Check the status of the tasks
docker service ps my_stack_web

Best Practices for Using Docker Swarm

  • Use Multiple Manager Nodes: Use multiple manager nodes to ensure high availability and fault tolerance.
  • Secure Your Swarm: Use TLS to encrypt communication between nodes and authenticate nodes joining the swarm.
  • Monitor Your Swarm: Use monitoring tools to track the health and performance of your swarm and services.
  • Automate Deployments: Use CI/CD pipelines to automate the building, testing, and deployment of your Docker services.
  • Use Overlay Networks: Use overlay networks to enable communication between services across multiple Docker daemons.

Testing Docker Swarm Integration

Test your Docker Swarm setup to ensure it works correctly:

Example: Testing with Mocha and Chai

// Install Mocha and Chai
// npm install --save-dev mocha chai

// test/app.test.js
const chai = require('chai');
const expect = chai.expect;
const axios = require('axios');

describe('Express App', () => {
    it('should return Hello, Docker Swarm!', async () => {
        const response = await axios.get('http://localhost:3000');
        expect(response.data).to.equal('Hello, Docker Swarm!');
    });
});

// Add test script to package.json
// "scripts": {
//   "test": "mocha"
// }

// Run tests
// docker run -p 3000:3000 your-dockerhub-username/my-express-app:latest
// npm test

Key Points

  • Swarm: A cluster of Docker nodes (machines running Docker) where you deploy services.
  • Node: An individual machine within a swarm. It can be a manager node or a worker node.
  • Service: The definition of the tasks (containers) to be run on the nodes in the swarm.
  • Task: A single instance of a container running in a service. A task is scheduled on a node within the swarm.
  • Manager Node: A node that manages the swarm, maintains cluster state, and schedules tasks.
  • Worker Node: A node that receives and executes tasks from the manager nodes.
  • Follow best practices for using Docker Swarm, such as using multiple manager nodes, securing your swarm, monitoring your swarm, automating deployments, and using overlay networks.

Conclusion

Docker Swarm is a powerful tool for orchestrating Docker containers in a cluster. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively use Docker Swarm to manage your Express.js applications. Happy coding!