Laravel Sail Multi-App Support Part 1

Scott Knight
3 min readApr 25, 2021

Sail is a great step forward for Laravel development. However, running multiple applications simultaneously is more difficult than it was running homestead. The docker containers require a little more care to set up than doing the same thing using homestead.

Setting up a simple docker environment to run sail is pretty straightforward, but requires some knowledge of docker, docker-compose and sail.

Creating the apps

Create the applications normally. For demonstration purposes, I created a directory to put the applications in, ~/work/laravel8-multi-site. Then I just created an application using the instructions from https://laravel.com/docs/8.x/installation. The only thing I did to simplify the docker-compose file was to exclude all the apps, with this command:

curl -s “https://laravel.build/example-app?with=" | bash

Then it was simple to rename the directory application1 and repeat the process, calling it application2.

Now, if you go to the application1 directory and run “sail up” it should start all the docker containers for that app. If you try and do the same thing in the application2 directory before shutting down application1, you’ll get an error about ports already being in use. That’s because both the mysql instance (port 3306) and the application container (port 80) attempt to expose ports to the outside. Outside of the docker environment, the port can only be mapped to one container.

Here’s the docker-compose.yml file that it created:

Remapping the ports to avoid conflict

One solution is to define a couple of variables in the environment file to reset the ports for each application so they don’t conflict. The application port defaults to 80, but you could define APP_PORT to remap it to a different port. Similarly, defining FORWARD_DB_PORT would remap the port for the mysql instance. So you would update the .env file for each project like this:

application1/.env
application2/.env

Then you can connect to each application directly, using the port number defined in the APP_PORT value in the environment files:

Conclusion

Remapping the ports will get the job done. However, with a lot of applications remembering the port numbers for each application can become a pain.

For a more homestead-like experience, see https://scott-knight.medium.com/laravel-sail-multi-app-part-2-7fd5156b2277

--

--

Scott Knight

Technology leader with an interest in improving performance.