Hello V,
Yes, it is possible to have multiple docker-compose.yml files and have services in them communicate with each other as long as they are on the same network. Laravel Sail uses Docker Compose under the hood, so you can leverage Docker Compose's networking features to achieve this.
Here's what you can do:
-
Ensure that your Laravel Sail application and your external JS application are on the same Docker network. You can define a network in your Laravel application's
docker-compose.ymland then reference that network in your JS application'sdocker-compose.yml. -
In your Laravel application's
docker-compose.yml, define a network at the bottom of the file:
networks:
sail:
driver: bridge
- In the service definitions in your Laravel
docker-compose.yml, make sure to connect each service to the defined network:
services:
laravel.test:
# ... other configuration ...
networks:
- sail
# ... other services ...
- In your JS application's
docker-compose.yml, reference the same network. You don't need to redefine the network, just make sure the name matches and that it's marked as external:
services:
js-app:
# ... other configuration ...
networks:
- sail
networks:
sail:
external: true
-
Now, when you run
sail upfor your Laravel application, it will create the network if it doesn't already exist. Then, when you rundocker-compose upfor your JS application, Docker Compose will attach the JS application's containers to the existingsailnetwork. -
To ensure that your JS application can communicate with the services in the Laravel application, use the service names defined in the Laravel
docker-compose.ymlas hostnames. For example, if you want to connect to the database from your JS application, you would use the service name of the database as the hostname.
Remember that you need to start the Laravel Sail environment first to ensure the network is created before you start your JS application. Also, make sure that the service names are unique across both docker-compose.yml files to avoid any conflicts.
By following these steps, you should be able to have both your Laravel application and your external JS application running in the same network and communicating with each other as needed.
I hope this helps! Let me know if you have any further questions.