Using Composer image locally? - php

As far as I understand the official composer image is meant to be used as a is php management tool and not like any other images you can use within the docker-compose file. So basically I can use the docker container if I don't have or don't want to install composer locally/nativelly.
So, I have created a root directory for my app which is empty at the moment, but if I run for example docker run --rm -it -volume $PWD:/app composer create-project laravel/laravel . I can't see the Laravel app being installed within my directory. Have I missunderstood something ora any ideas what I'm doing wrong?

You should either use the flag -v or --volume.
This command worked for me, just make sure you are in an empty directory:
docker run --rm -it -v $PWD:/app composer create-project laravel/laravel .

Related

how to do composer install during docker build

After deploying the application to laravel, I need to run these commands.
docker exec -it php bash
composer update --ignore-platform-reqs
exit
cd back/src
sudo chmod o+w ./storage/ -R
But when deploying to other developers, this is inconvenient, how can I include these commands in a dockerfile or docker-compose.yml? And even, it is possible that after build, docker-composer up -d is immediately filled
Composer does not start from the system(root), so i have to run it from another container
In the docker-compose.yml file you can set the command to update the packakges. Also set the volume, which will give the correct rights.
php:
command:
- composer update --ignore-platform-reqs
volumes:
- ./storage/:/app/storage:rw
But all of this does depend on the image you're using. Which docker image do you use?

How to setup and run laravel, from git?

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.
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.
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.
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.

Installing composer in a docker image

So i tried many ways, but this one seems the most legit way of installing composer in a docker image :
https://hub.docker.com/r/composer/composer/
Now when im trying to do the "same", i'm getting:
latest: Pulling from composer/composer
Status: Image is up to date for composer/composer:latest
Composer could not find a composer.json file in /app
When im running it:
docker run --rm -v $(pwd):/app composer/composer install
Even if i change the /app route to /var/www, i still get the same error that it cant find the composer.json in /app.
How do i make it so that it looks in the directory where i need the composer? I dont understand the $(pwd) part but tried it several ways.. I dont have any volumes created as im using php from tutum/lamp.
I am also not using docker-compose so if possible, tell me some way of doing it without docker compose.
Can provide more details if needed.
EDIT
Could it be something regarding the volumes? Currently i have the composer.json file in /var/protobuf and i use this command: docker run --rm -v $(pwd):/var/protobuf composer/composer install to tell it about the directory of the json file, but it still keeps looking in /app, maybe i need to create volumes?

custom script on docker run

I am trying to install the skeleton application of Zend Framework 3 with Docker.
The installation works fine, but I'm not able to run some composer scripts. In the composer.json there are some custom composer scripts, which should be generally launched with
composer cs-fix
I would like to lauch there commands with the Composer Docker image, using
docker run --rm -ti --volume $PWD:/app composer cs-fix
When I try to do this, I obtain the following error
/docker-entrypoint.sh: line 60: exec: cs-fix: not found
Is my command wrong?
Found it! Instead of trying to run the custom composer script, I need to use the special run-script command, as in
docker run --rm -it --volume $PWD:/app composer run-script "cf-fix"

codecept: command not found

I did a fresh installation of Ubuntu and after installing Yii2 etc I can't seem to be able to run codecept anymore.
I'm using Yii2. I required the latest codecept version in composer.json which is working fine. But I can't seem to find a way to get codeception running again.
I've been looking through all the guides but none of them have anything other then just to composer require or add it to the composer.json. I do have noticed when I wanted to add codecept to my $PATH that I don't have a codecept file in root/vendor/bin/, where I think the $PATH entry should point to.
I have no idea where to go from here. Did I miss some step somewhere maybe?
Just to sum things up:
codeception (yiisoft/yii2-codeception) is installed.
root/vendor/bin/ does not contain anything that points to codeception.
command: codecept run unit returns: codecept: Command not found
command: (./)vendor/bin/codecept return: "path": no such file or directory.
codecept: command not found on Ubuntu then you should follow these steps:
sudo composer global require "codeception/codeception=2.1.*" "codeception/specify=*" "codeception/verify=*"
and then run this command:
sudo ln -s ~/.composer/vendor/bin/codecept /usr/local/bin/codecept
So codecept build and codecept run will work.
If you are using Windows then run this command:
composer global require "codeception/codeception=2.1.*" "codeception/specify=*" "codeception/verify=*"
Add this line into your path:
~\AppData\Roaming\Composer\vendor\bin
You should add composer global packages binaries to your PATH.
For local development I'm using Vagrant, here is example for it:
export PATH=$PATH:/home/vagrant/.composer/vendor/bin
If you have some like this error:
$ php ./vendor/bin/codecept run
Could not open input file: ./vendor/bin/codecept
Just run:
$ rm composer.lock
$ composer update

Categories