A comprehensive guide to setting up and managing communication between multiple Laravel microservices using Docker and Docker Compose.
This project demonstrates how to:
- Set up multiple Laravel projects in separate Docker environments
- Establish communication between microservices
- Manage containerized Laravel applications
- Handle inter-service communication
- Implement service discovery
- Configure network connectivity between containers
- Multiple Laravel projects in Docker containers
- Inter-service communication
- Docker network configuration
- Service discovery
- Environment isolation
- Scalable microservices architecture
- Container orchestration
- Docker and Docker Compose installed
- Basic understanding of Laravel
- Basic knowledge of Docker concepts
- Two separate Laravel projects
- Git
microservices/
├── project-1/
│ ├── docker/
│ │ ├── nginx/
│ │ │ └── default.conf
│ │ └── php/
│ │ └── Dockerfile
│ ├── docker-compose.yml
│ └── .env
└── project-2/
├── docker/
│ ├── nginx/
│ │ └── default.conf
│ └── php/
│ └── Dockerfile
├── docker-compose.yml
└── .env
- Create a shared network:
docker network create laravel-network
- Configure docker-compose.yml for Project 1:
# project-1/docker-compose.yml
version: '3'
services:
app1:
build:
context: .
dockerfile: docker/php/Dockerfile
container_name: project1_app
volumes:
- .:/var/www/html
networks:
- laravel-network
environment:
- DB_HOST=mysql
- DB_DATABASE=project1
- DB_USERNAME=root
- DB_PASSWORD=secret
- PROJECT2_URL=http://project2_app:8000
nginx1:
image: nginx:alpine
container_name: project1_nginx
ports:
- "8001:80"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- laravel-network
depends_on:
- app1
mysql1:
image: mysql:8.0
container_name: project1_mysql
environment:
MYSQL_DATABASE: project1
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel-network
volumes:
- mysql1_data:/var/lib/mysql
networks:
laravel-network:
external: true
volumes:
mysql1_data:
- Configure docker-compose.yml for Project 2:
# project-2/docker-compose.yml
version: '3'
services:
app2:
build:
context: .
dockerfile: docker/php/Dockerfile
container_name: project2_app
volumes:
- .:/var/www/html
networks:
- laravel-network
environment:
- DB_HOST=mysql
- DB_DATABASE=project2
- DB_USERNAME=root
- DB_PASSWORD=secret
nginx2:
image: nginx:alpine
container_name: project2_nginx
ports:
- "8002:80"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- laravel-network
depends_on:
- app2
mysql2:
image: mysql:8.0
container_name: project2_mysql
environment:
MYSQL_DATABASE: project2
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel-network
volumes:
- mysql2_data:/var/lib/mysql
networks:
laravel-network:
external: true
volumes:
mysql2_data:
- Project 1 Service
// project-1/app/Services/Project2Service.php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class Project2Service
{
protected $baseUrl;
public function __construct()
{
$this->baseUrl = env('PROJECT2_URL');
}
public function getData()
{
try {
$response = Http::get("{$this->baseUrl}/api/data");
return $response->json();
} catch (\Exception $e) {
return [
'error' => 'Failed to communicate with Project 2',
'message' => $e->getMessage()
];
}
}
}
- Project 2 API Endpoint
// project-2/routes/api.php
Route::get('/data', function () {
return response()->json([
'message' => 'Data from Project 2',
'timestamp' => now()
]);
});
- Project 1 .env
# project-1/.env
APP_NAME=Project1
APP_ENV=local
APP_KEY=base64:your-key
APP_DEBUG=true
APP_URL=http://localhost:8001
DB_CONNECTION=mysql
DB_HOST=mysql1
DB_PORT=3306
DB_DATABASE=project1
DB_USERNAME=root
DB_PASSWORD=secret
PROJECT2_URL=http://project2_app:8000
- Project 2 .env
# project-2/.env
APP_NAME=Project2
APP_ENV=local
APP_KEY=base64:your-key
APP_DEBUG=true
APP_URL=http://localhost:8002
DB_CONNECTION=mysql
DB_HOST=mysql2
DB_PORT=3306
DB_DATABASE=project2
DB_USERNAME=root
DB_PASSWORD=secret
- Start Project 1:
cd project-1
docker-compose up -d
- Start Project 2:
cd project-2
docker-compose up -d
- From Project 1 to Project 2
// project-1/routes/web.php
Route::get('/test-connection', function () {
$service = new \App\Services\Project2Service();
return $service->getData();
});
- Access the endpoint
curl http://localhost:8001/test-connection
-
Network Configuration
- Use external networks for service communication
- Implement proper network security
- Use container names for service discovery
-
Environment Management
- Use environment variables for configuration
- Keep sensitive data in .env files
- Use different environments for development and production
-
Service Communication
- Implement proper error handling
- Use HTTP client for service communication
- Implement retry mechanisms for failed requests
-
Security
- Implement proper authentication between services
- Use HTTPS for service communication
- Implement rate limiting
- Use proper firewall rules
For questions, suggestions, or collaboration:
- Author: Murilo Livorato
- GitHub: murilolivorato
- linkedIn: https://www.linkedin.com/in/murilo-livorato-80985a4a/