11

Laravel Sail, has a good premisse to create a zero dependency environment, with, php, redis, mariadb etc...

But its instalation require composer, composer require php installed,

is this a paradox?

There are some way to achieve zero dependency, with docker installed?

Aloiso Gomes
  • 640
  • 6
  • 12
  • Laravel requires PHP and composer.. if you don't want those then don't use laravel sail and conifgure your own docker env.. – naamhierzo Feb 23 '22 at 09:36
  • This isn't so much of a paradox. The idea of having Sail (or Docker in general) is provide a specific application environment for testing and eventually deployment. Most people will have PHP installed locally, and your IDE will likely depend on it too. This PHP installation may be customized, a different version, or be built with different extensions than your project requires, yet the build tools will still run. – Nilpo Oct 09 '22 at 04:50

4 Answers4

13

edit

I was listening to the laravel news podcast yesterday and the people from Laravel created a solution for this problem. They now have a section with a command that spins up a php container to install php dependencies through a container first and move them to your host computer.

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs


As Sail gives you the option to manage the container from the host through Artisan, then no.

If you don't mind to give up the ability to use Sail in this way and just want to set up a dockerized environment for Laravel, then yes. You can make a container for php in which you copy your project and through volumes maps to the host. You can enter a container (as if using ssh) through docker-compose exec <service> <command>, in the repo I linked, the php Dockerfile uses Alpine as the linux distro, which comes with the ash (instead of bash) shell. Because of his you can run

docker-compose exec php ash

to enter the container and run commands inside it.

online Thomas
  • 8,864
  • 6
  • 44
  • 85
1

It's in the documentation here:

https://laravel.com/docs/9.x/sail#installing-composer-dependencies-for-existing-projects

You can use the sail Docker container on its own to bootstrap the process, and run composer in that container. The main part of that documentation is this command:

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs

There is a laravelsail/php7.4:latest container too, if you are not yet using PHP 8.


Edit: the above used to work, but now appears to be broken. The container does not have a sail user or group, and that leads to permission problems.

When the sail up command is executed, the entrypoint to the PHP container will take the current user ID and group ID, then assign them to the sail user and group. That happens (now) in the sail up command and not the laravelsail container listed above.

I'm not sure when this changed. For now you can run the above without the -u option, and set up the application as root within the container. Permissions will need to be manually fixed after that.

Also note the laravelsail/php7.3 container only has composer v1.8 installed initially, but is easily updated using composer self-update.

Jason
  • 4,411
  • 7
  • 40
  • 53
0

This command worked for me on windows:

docker run -v /c/xampp/htdocs/boilerplate:/var/www/html -w /var/www/html composer install  --ignore-platform-reqs
Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Yogesh.galav
  • 225
  • 1
  • 5
  • 17
0

You can do that by setting up Laravel Sail on a machine that has local setup & then using this command sail artisan sail:publish

After running this command, the Dockerfiles and other configuration files used by Laravel Sail will be placed within a docker directory in your application's root directory. After customizing your Sail installation, you may wish to change the image name for the application container in your application's docker-compose.yml file. After doing so, rebuild your application's containers using the build command.

Shreyansh Panchal
  • 827
  • 12
  • 22