When developing Laravel applications in a Docker environment, you often need to simultaneously develop custom Composer packages. This article shares an elegant solution using symbolic links.
Problems
- Docker containers cannot access host machine absolute paths
- Packages scattered across projects, difficult to manage centrally
- Package modifications require reinstallation to take effect
Solution
1. Create Unified Package Development Directory
mkdir -p /home/developer/packages/php
mkdir -p /home/developer/project/my-laravel-app/plugins
# Create symbolic links
ln -s /home/developer/packages/php/my-custom-package /home/developer/project/my-laravel-app/plugins/my-custom-package
2. Docker Volume Mounting
# docker-compose.yml
volumes:
- ./:/var/www/html # Laravel project mounted to /var/www/html, NGINX usually points to /var/www/html
- /home/developer/packages/php:/home/developer/packages/php # Key: Make the same path available inside container
3. Composer Configuration
{
"repositories": [
{
"type": "path",
"url": "plugins/my-custom-package",
"options": {
"symlink": true
}
}
]
}
4. Independent Version Control for Packages
Each package should have its own Git repository:
cd /home/developer/packages/php/my-custom-package
git init
git remote add origin https://github.com/your-org/my-custom-package.git
Why Independent Git is Needed?
- Version Management: Packages have their own evolution history
- Cross-project Sharing: Multiple projects can reference different versions
- Release Process: Easy to tag and publish to Packagist
- Permission Control: Different packages can have different developer permissions
- CI/CD: Packages can have independent testing and deployment processes
The main project's .gitignore should exclude the symbolic link directory:
/plugins/
Advantages
- Centralized Management: All packages developed in one place
- Real-time Updates: Changes take effect immediately, no reinstallation needed
- Multi-project Sharing: One codebase serves multiple projects
- Container Friendly: Perfect for Docker environments
- Independent Publishing: Each package can have independent version control and publishing
Core Principle
Through dual mounting strategy:
- Host machine creates symbolic links pointing to unified package directory
- Docker mounts the same path, making symbolic links work inside containers
- Composer uses relative paths, ensuring consistency between host and container
Restoration Strategy After Development
After package development is complete and published, switch back to official package sources:
1. Publish Package to Official Repository
# In package directory
cd /home/developer/packages/php/my-custom-package
git tag v1.0.0
git push origin v1.0.0
# Publish to Packagist or private Composer repository
2. Update Project's composer.json
{
"repositories": [
// Remove or comment out local path
// {
// "type": "path",
// "url": "plugins/my-custom-package",
// "options": {
// "symlink": true
// }
// }
],
"require": {
"vendor/my-custom-package": "^1.0" // Change to official version number
}
}
3. Clean Up Local Symbolic Links
# Remove symbolic links
rm /home/developer/project/my-laravel-app/plugins/my-custom-package
# Reinstall official version
composer update vendor/my-custom-package
4. Restore Docker Configuration (Optional)
If no longer developing other packages, you can remove the package directory mounting:
# docker-compose.yml
volumes:
- ./:/var/www/html
# Remove package mounting
# - /home/developer/packages/php:/home/developer/packages/php
This completes the transition from development mode to official release mode, ensuring the project uses stable official versions!
This way you can enjoy the convenience of local package development in a Docker environment!
Top comments (0)