Containerizing a web application with Docker and Docker Swarm

Overview

In today's fast-paced digital landscape, containerization has become an essential approach for deploying and managing web applications. Docker Swarm, a container orchestration tool, allows you to efficiently scale and distribute containers across multiple hosts. In this blog post, we will explore how Docker Swarm, along with Drupal and PostgreSQL, can be leveraged to containerize a web application and host it on DigitalOcean Droplets.

Technologies Used

  • Docker

  • PostgreSQL

  • Drupal

  • Digital Ocean

  • Ubuntu

Architecture

The project will consist of three Digital Ocean droplets configured with the latest version of Docker installed.

Setup and Installation

The first step was to provision 3 instances of Digital Ocean droplets that will act as the manager and workers for the Docker Swarm cluster. Docker Swarm enables the management of a cluster of Docker nodes as a single entity, allowing you to scale and distribute your application with ease.

Provision Nodes

  1. Using Digital Ocean, I provisioned 3 droplets with Ubuntu 22.10 x64 images.

  1. The latest version of Docker was installed on each of the 3 nodes.

  2. I initialized the Docker swarm cluster on node1 with docker swarm init --advertise-addr HOST_ADDRESS. With the newly created docker swarm join token, I added both nodes 2 & 3 to the swarm as worker nodes. In this case, I also updated the roles of the worker nodes to managers with docker node update --role manager node2 docker node update --role manager node3.

Web Application Service

For the web application, I used Drupal. Drupal is a free and open-source web content management system written in PHP and distributed under the GNU General Public License.

  1. I created an overlay network to manage the communications between the services of the app.

  2. I created the Drupal service configured with version 9 of the official Drupal image using the docker service create command within the custom network created prior.

Optionally, a Dockerfile that defines the application's environment, dependencies, and configurations can be used. Use the official Drupal Docker image as the base image and customize it as needed. Incorporate any necessary modules, themes, or custom code. Build the Docker image, and push it to a container registry for easy deployment across the cluster.

Database Service

For the application's database, PostgreSQL provides a robust and reliable option.

Similar to the web application service, I used the docker service create command in creating the PostgreSQL using version 14 of the offical PostgreSQL image within the same custom network created prior.

Optionally, create a separate Dockerfile for the PostgreSQL container, specifying the necessary environment variables, such as database credentials and configurations. Ensure that the PostgreSQL container is accessible within the Docker Swarm cluster and that the Drupal container can communicate with it.

Load Balancing and High Availability

One of the advantages of using Docker Swarm is its built-in load-balancing capabilities. Docker Swarm automatically distributes incoming requests across the available replicas of your application, ensuring high availability and efficient resource utilization. You can configure external load balancers, such as DigitalOcean's Load Balancers, to distribute traffic to the Docker Swarm cluster.

Scaling and Updating the Application

Docker Swarm makes it easy to scale your application horizontally by increasing the number of replicas. As your application's demands grow, simply update the number of replicas for the corresponding service. Additionally, rolling updates and rollbacks are effortless with Docker Swarm, allowing you to deploy new versions of your application seamlessly and with minimal downtime.

Resources

Conclusion

Containerizing a web application using Docker Swarm, Drupal, and PostgreSQL offers numerous benefits, including scalability, high availability, and simplified management. DigitalOcean Droplets provide a reliable hosting environment for Docker Swarm clusters, allowing you to focus on developing and delivering your application. By harnessing the power of containerization and orchestration, you can streamline your deployment process and ensure a smoother web application experience for your users.