πΊπΈ English β’ π§π· Leia em PortuguΓͺs
Practical demonstration of Docker containers in microservices scenarios with load balancing and database integration
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.
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.
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 |
β’ β 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
β’ π 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
β’ π― Easy Deployment β Single-command container orchestration β’ π Configuration Management β Externalized configuration files β’ π§ Environment Parity β Consistent development/production environments β’ π‘ Educational Value β Perfect for learning microservices patterns
β’ 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
- Clone the repository:
git clone https://github.com/th-hoffmann/docker-php-loadbalancer-example.git
cd docker-php-loadbalancer-example- Configure the environment:
# Review and adjust configuration files
nano nginx.conf # Load balancer configuration
nano index.php # Application configuration- 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- Verify the deployment:
# Check service health
curl http://localhost:4500
# Monitor logs
docker logs loadbalancer
# Access the application
# Navigate to: http://localhost:4500docker-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)
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 { }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')";CREATE TABLE dados (
AlunoID int,
Nome varchar(50),
Sobrenome varchar(50),
Endereco varchar(150),
Cidade varchar(50),
Host varchar(50)
);β’ Network Segmentation β Isolated container networks β’ Environment Variables β Secure configuration management β’ Access Control β Principle of least privilege β’ Container Security β Non-root user execution
π¨ 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
β’ Service Decomposition β Breaking monoliths into services β’ Data Management β Database per service pattern β’ Communication Patterns β HTTP-based inter-service communication β’ Deployment Independence β Individual service deployment
β’ Container Orchestration β Multi-container application management β’ Image Optimization β Efficient container images β’ Networking β Custom network configuration β’ Volume Management β Data persistence patterns
Contributions are welcome! Feel free to:
β’ π Report bugs β’ π‘ Suggest improvements β’ π§ Submit pull requests β’ π Improve documentation
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by th-hoffmann based on the practical Docker training by Denilson Bonatti from Digital Innovation One.
π Back to top
Docker microservices demonstration with Nginx load balancing, PHP applications, and MySQL integration. Perfect for learning container orchestration and microservices architecture patterns.
docker microservices nginx php mysql load-balancing containers devops architecture scalability high-availability web-development database-integration infrastructure automation orchestration networking