Skip to content

A simple project demonstrating a load-balanced PHP application using Docker and Nginx.

Notifications You must be signed in to change notification settings

th-hoffmann/docker-php-loadbalancer-example

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 Docker PHP Load Balancer Example

Status Docker PHP MySQL Nginx

πŸ‡ΊπŸ‡Έ English β€’ πŸ‡§πŸ‡· Leia em PortuguΓͺs

Practical demonstration of Docker containers in microservices scenarios with load balancing and database integration

πŸ“‹ About the Project

This project was developed as a practical demonstration of Docker's real function in microservices environments, showcasing how containers can be effectively applied in day-to-day development scenarios. The project implements a complete microservices architecture with Nginx load balancing, PHP applications, and MySQL database integration.

🎯 Objective

Create a practical example that demonstrates the real function of containers in microservices scenarios, answering key questions about Docker's practical applications in modern development workflows and providing hands-on examples that can be applied in real projects.

πŸ—οΈ Architecture Overview

The project implements a complete microservices ecosystem demonstrating industry-standard patterns:

Component Technology Purpose Configuration
Load Balancer Nginx Traffic distribution Port 4500
Web Services PHP/Apache Application logic Multiple instances
Database MySQL Data persistence Remote MySQL server
Container Orchestration Docker Service management Custom networking

⚑ Features

πŸ”§ Microservices Architecture

β€’ βœ… Load Balancing β†’ Nginx upstream configuration with multiple backend servers β€’ βœ… Service Discovery β†’ Automatic service registration and discovery β€’ βœ… Horizontal Scaling β†’ Multiple PHP application instances β€’ βœ… Database Integration β†’ MySQL connectivity with connection pooling

πŸ›‘οΈ Production-Ready Patterns

β€’ πŸ”„ High Availability β†’ Multiple service instances for redundancy β€’ πŸ“Š Load Distribution β†’ Round-robin load balancing algorithm β€’ 🌐 Network Isolation β†’ Custom Docker networking for security β€’ πŸš€ Container Orchestration β†’ Efficient resource management

βš™οΈ Development Workflow

β€’ 🎯 Easy Deployment β†’ Single-command container orchestration β€’ πŸ“ Configuration Management β†’ Externalized configuration files β€’ πŸ”§ Environment Parity β†’ Consistent development/production environments β€’ πŸ’‘ Educational Value β†’ Perfect for learning microservices patterns

πŸš€ How to Use

πŸ“‹ Prerequisites

β€’ Docker Engine: 20.10+ with BuildKit support β€’ Docker Compose: 2.0+ for multi-container orchestration β€’ System Requirements: 4GB RAM, 10GB disk space β€’ Network Access: Internet connection for image downloads

πŸ”§ Installation & Deployment

  1. Clone the repository:
git clone https://github.com/th-hoffmann/docker-php-loadbalancer-example.git
cd docker-php-loadbalancer-example
  1. Configure the environment:
# Review and adjust configuration files
nano nginx.conf  # Load balancer configuration
nano index.php   # Application configuration
  1. Deploy the services:
# Build the Nginx load balancer container
docker build -t nginx-loadbalancer .

# Start the load balancer
docker run -d -p 4500:4500 --name loadbalancer nginx-loadbalancer

# Verify deployment
docker ps
  1. Verify the deployment:
# Check service health
curl http://localhost:4500

# Monitor logs
docker logs loadbalancer

# Access the application
# Navigate to: http://localhost:4500

πŸ“ Project Structure

docker-php-loadbalancer-example/
β”œβ”€β”€ 🐳 dockerfile                    # Nginx load balancer container
β”œβ”€β”€ βš™οΈ  nginx.conf                   # Load balancer configuration
β”œβ”€β”€ 🐘 index.php                     # PHP application code
β”œβ”€β”€ πŸ—„οΈ  banco.sql                    # Database schema
β”œβ”€β”€ πŸ“– README.md                     # Documentation (English)
└── πŸ“– README_pt-br.md              # Documentation (Portuguese)

βš™οΈ Configuration Details

🌐 Load Balancer Setup

The Nginx configuration implements advanced load balancing patterns:

http {
    upstream all {
        server 172.31.0.37:80;
        server 172.31.0.151:80;
        server 172.31.0.149:80;
    }

    server {
         listen 4500;
         location / {
              proxy_pass http://all/;
         }
    }
}

events { }

🐘 Database Integration

The PHP application demonstrates proper database connectivity patterns:

$servername = "54.234.153.24";
$username = "root";
$password = "Senha123";
$database = "meubanco";

$link = new mysqli($servername, $username, $password, $database);

// Insert random data to demonstrate functionality
$valor_rand1 = rand(1, 999);
$valor_rand2 = strtoupper(substr(bin2hex(random_bytes(4)), 1));
$host_name = gethostname();

$query = "INSERT INTO dados (AlunoID, Nome, Sobrenome, Endereco, Cidade, Host) 
          VALUES ('$valor_rand1', '$valor_rand2', '$valor_rand2', '$valor_rand2', '$valor_rand2', '$host_name')";

πŸ—„οΈ Database Schema

CREATE TABLE dados (
    AlunoID int,
    Nome varchar(50),
    Sobrenome varchar(50),
    Endereco varchar(150),
    Cidade varchar(50),
    Host varchar(50)
);

πŸ›‘οΈ Security

πŸ” Best Practices Applied

β€’ Network Segmentation β†’ Isolated container networks β€’ Environment Variables β†’ Secure configuration management β€’ Access Control β†’ Principle of least privilege β€’ Container Security β†’ Non-root user execution

⚠️ Security Considerations

🚨 Note: This project is designed for educational and development purposes. For production deployment, consider:

β€’ SSL/TLS termination at load balancer β€’ Database connection encryption β€’ Secrets management with Docker Secrets β€’ Container image vulnerability scanning β€’ Network policies and firewall rules β€’ Secure database credentials management

πŸ“š Applied Concepts

πŸ—οΈ Microservices Architecture

β€’ Service Decomposition β†’ Breaking monoliths into services β€’ Data Management β†’ Database per service pattern β€’ Communication Patterns β†’ HTTP-based inter-service communication β€’ Deployment Independence β†’ Individual service deployment

🐳 Docker & Containerization

β€’ Container Orchestration β†’ Multi-container application management β€’ Image Optimization β†’ Efficient container images β€’ Networking β†’ Custom network configuration β€’ Volume Management β†’ Data persistence patterns

🀝 Contributing

Contributions are welcome! Feel free to:

β€’ πŸ› Report bugs β€’ πŸ’‘ Suggest improvements β€’ πŸ”§ Submit pull requests β€’ πŸ“– Improve documentation

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Developed by th-hoffmann based on the practical Docker training by Denilson Bonatti from Digital Innovation One.

⭐ If this project was helpful, consider giving it a star!

πŸ”™ Back to top


About

Docker microservices demonstration with Nginx load balancing, PHP applications, and MySQL integration. Perfect for learning container orchestration and microservices architecture patterns.

Topics

docker microservices nginx php mysql load-balancing containers devops architecture scalability high-availability web-development database-integration infrastructure automation orchestration networking

About

A simple project demonstrating a load-balanced PHP application using Docker and Nginx.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%