1

Either I miss something, or the whole chain lacks something.

Here's my assumption:

The whole point of containerization in development, is to reduce the cost of environment setup, and create a prepared image with all the required pieces.

So, when I read that Laravel Sail is installing laravel via containerization, I get excited. Thus I install it via their instructions, and everything works.

Then the problem begins. Because:

  • After a successful installation, I create a git repo, with GitHub's default laravel .gitignore
  • Then I push the newly installed laravel app into my git repo.
  • Then I ask a developer to start developing it. Please note that:
    • He does not have PHP installed
    • He does not have Composer installed
  • He clonse the repo, and as per installation guide, runs ./vendor/bin/sail up
  • But ./vender folder is correctly excluded in .gitignore
  • Thus his command results in:

bash: ./vendor/bin/sail: No such file or directory

  • He Googles it of course, and finds out that people suggest to run composer update
  • He goes to install composer, then before that PHP, then all extensoins of PHP, then ...

Do I miss something here? The whole point of containerization was to not install the required environment locally.

What is the proper way of running a laravel app, that is not installed from https://laravel.build, but is cloned from a git repo, WITHOUT having PHP or Composer installed locally?

Update

I found Bitnami laravel docker and it's exactly what containers should be.

3
  • 1
    If you're doing development, you need composer installed. Period. Sail is just a bundle of scripts to assist with running and environment for local dev, which leverages docker but is distributed via composer, just like the rest of Laravel. You could certainly build a docker image that bundles all this mess in, but it would be far messier and complicated and a generally worse experience. So git clone, composer install, sail up. Commented Jun 10, 2021 at 19:27
  • @Sammitch, if I have to install PHP and Composer, I might as well install MySQL, and Apache, and Redis, etc. Then I would prefer to not use Docker at all. This scenario to me is like buying an electric car, but filling it with gas. Commented Jun 10, 2021 at 19:29
  • 1
    There is no logical connection to "I might as well install the whole LAMP stack". At most you need to install a PHP CLI package, and download a copy of composer. Please at least actually try before you simply abandon the notion and go make a ton more work for yourself. Commented Jun 10, 2021 at 19:36

3 Answers 3

5

You are right and the other developer doesn't need to have php nor composer installed. All he/she needs is Docker installed on the local machine.

If you scaffolded the project with what is mentioned in the official Laravel docs under the Getting started section, then you will have a docker-compose.yml file in your project root directory.

For Windows

For Linux

For Mac OS

All the developer has to do after git cloning the repository is to run

docker-compose up --build -d

That's it.

Sign up to request clarification or add additional context in comments.

1 Comment

They also need to follow that with sail artisan key:generate which needs to be done after cloning any Laravel app from a git repo.
1

For those struggling with this issue... I've found a command that work perfectly fine.

First of all, you don't need to locally have any PHP or Composer installed, maybe there is a misunderstanding about it, all you need is Docker.

Docker will install everything you need in something I understand is like a sandbox, not locally, for each project.

And for those downloaded projects, from GIT as example, that does not have vendor folder, and obviously cannot execute sail up you can simple execute:

docker run --rm --interactive --tty -v $(pwd):/app composer install

That command will download a composer image for docker, if you do not have one yet. Then, will run a composer install and you are free to execute a ./vendor/bin/sail up if you hadn't configured an alias or just sail up if you already configure an alias.

That's all.

2 Comments

The answer should probably use ./vendor/bin/sail up. If a person is cloning a laravel app from a repo, it's a safe assumption that they may not have an alias for sail and may not have it in their PATH.
Very good. Upvoted.
0

The official documentation lists the following 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

If you were to clone a Laravel project and run this command in the project root, it would create a very small container with php and composer installed and run composer in the project root to install all php dependencies. In effect, this installs the Laravel core code into the cloned project. Once the project in set up this way, the user should create a local .env file to match their development evironment.

cp .env.example .env            # creates a .env file to be populated for the local environment

With the envronment set up, they can now create the application containers in docker and run the application. Laravel provides the Sail helper for this.

./vendor/bin/sail up -d         # runs the docker containers in detached mode

Now it's a matter of setting up the laravel app and running the Laravel app. (I'm assuming the app uses one of the Laravel start kits that rely on Node.js. If you are using a Blade only application, you can skip the "npm" commands.)

sail artisan key:generate       # (Best Practice) Generate a new application key on each machine
sail artisan migrate            # Scaffold the database structure
sail artisan db:seed            # (Optional) Seed the database with data
sail npm install                # (Optional) Install front-end dependencies (Inertia, Vue, React, others...)
sail npm run dev                # (Optional) Run the front-end framework in development mode

With this, the new developer should be running an exact copy of both the project and the development environment as the original developer.

Your project README may include additional steps to set up some other dependencies, but this is the basic workflow for contributing to a Laravel project.

The only prerequisites for this workflow is to have Docker installed with an Internet connection. This is most easily accomplished on Windows, Mac, and Linux by installing Docker Desktop.

Alternate for Older Projects

If you are working on an older project that doesn't use Laravel Sail, but does have a docker-compose.yml file, you should be able to build and run the necessary containers with the following command.

docker-compose up --build -d

Once you have the containers running, you would need to install the project dependencies directly into the container.

docker ps             # find the container ID of your project's container
docker exec -it CONTAINER_ID php artisan key:generate
docker exec -it CONTAINER_ID php artisan migrate
docker exec -it CONTAINER_ID php artisan db:seed
docker exec -it CONTAINER_ID npm install
docker exec -it CONTAINER_ID npm run dev

Of course, Docker Desktop simplifies this process. With a button click you can have a terminal shell open directly in your container eliminating the need for the docker exec command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.