Day 83 of 90 Days of DevOps Challenge: Deploying a Web Application with Docker Swarm

As the adoption of microservices and containerization continues to grow, managing and orchestrating containers efficiently becomes paramount. In this blog post, we’ll explore how to deploy a web application using Docker Swarm, a robust container orchestration tool that simplifies the management and scaling of containerized applications.


Project Overview

The objective of this project is to deploy a web application using Docker Swarm. We will utilize Swarm's features, including load balancing, rolling updates, and service discovery, to ensure the application's high availability and reliability. The project will involve creating a Dockerfile to package the application and deploying it onto a Swarm cluster configured for automated failover and horizontal scaling.


Key Components

1. Docker Swarm

Docker Swarm is Docker's native clustering and orchestration tool. It allows you to manage a cluster of Docker nodes as a single virtual system, providing features like:

  • Load Balancing: Distributing incoming requests across containers to optimize resource use.

  • Service Discovery: Automatically registering and discovering services in the cluster.

  • Rolling Updates: Gradually deploying updates without downtime.

2. Dockerfile

A Dockerfile is a text file containing instructions on how to build a Docker image. It defines the application environment and dependencies, allowing for consistent builds and deployments.


Implementation Steps

Step 1: Set Up Your Docker Environment

  1. Install Docker: Ensure Docker is installed on your system or VM. Follow the installation guide on the official Docker website.

  2. Initialize Docker Swarm: Open a terminal and run the following command to initialize the Swarm cluster:

     docker swarm init
    

Step 2: Create Your Application

  1. Create Application Directory: Set up a new directory for your application files:

     mkdir my-web-app
     cd my-web-app
    
  2. Create a Simple Web Application: For demonstration, create a simple HTML file. Create a file named index.html:

     <!DOCTYPE html>
     <html>
     <head>
         <title>My Web Application</title>
     </head>
     <body>
         <h1>Welcome to My Web Application!</h1>
         <p>This application is running on Docker Swarm.</p>
     </body>
     </html>
    

Step 3: Create a Dockerfile

  1. Create Dockerfile: In the same directory, create a file named Dockerfile with the following content:

     # Use the official Nginx image
     FROM nginx:alpine
    
     # Copy the HTML file to the Nginx server directory
     COPY index.html /usr/share/nginx/html
    
     # Expose port 80
     EXPOSE 80
    

Step 4: Build the Docker Image

  1. Build the Image: Run the following command to build your Docker image:

     docker build -t my-web-app .
    

Step 5: Deploy to Docker Swarm

  1. Create a Service: Use the following command to create a service in Docker Swarm:

     docker service create --name web-app --replicas 3 -p 80:80 my-web-app
    
    • --replicas 3: Specifies that we want three instances of the service running for load balancing.

    • -p 80:80: Maps port 80 on the host to port 80 on the container.

Step 6: Monitor the Service

  1. Check Service Status: Run the following command to check the status of your deployed service:

     docker service ls
    

  2. Access the Web Application: Open a web browser and navigate to http://<your-docker-host-ip>. You should see your web application running!

Step 7: Rolling Updates

  1. Update the Application: To demonstrate rolling updates, modify the index.html file and change the content.

  2. Rebuild the Image: Build the updated image again:

     docker build -t my-web-app:latest .
    
  3. Update the Service: Run the following command to update the service with the new image:

     docker service update --image my-web-app:latest web-app
    

Step 8: Scale the Application

  1. Scale the Service: You can easily scale the number of replicas for your service:

     docker service scale web-app=5
    

    This command will increase the number of running instances to five, ensuring higher availability under load.


Conclusion

Deploying a web application using Docker Swarm provides an efficient way to manage containerized applications at scale. By leveraging Swarm’s features like load balancing and rolling updates, you can ensure that your applications remain highly available and reliable in production environments.

This project highlights the benefits of Docker Swarm for orchestrating containerized applications, making it an essential tool for modern software development and deployment.