When you are PHP developer I am sure you use Composer. In this article I will not explain here what Composer is (I assume you know it). But how to use Composer without installing PHP in your operating system? The answer is simple - use Docker.
I use composer in my system (currently Linux Mint 20.3) by this command:
docker run --rm -v $(pwd):/app composer -V
Let's see what parameters we have here:
docker run
- we run image
--rm
- we remove this image from memory after it will finish job
-v $(pwd):/app
- we map current directory (pwd) to /app directory inside of docker container
composer
- we run composer image (it's image name)
-V
- we check the version of composer (we don't have to do it, I did it to check if composer is working)
When we will run the above command the composer will start working. First docker checks if the image is present in the local system. If yes, Docker will run it, if not, Docker will download it. So when you will run this command first time, it can take some time to download the image but later running the command will be very fast.
From now every time you want to use composer you can run the above command. I know, I know, I hear your shout: What does it mean? Should I use this long command every time?
No you shouldn't. You can make an alias in your system. Open the .bashrc
file from your home directory and add at the end of this file these lines:
#my aliases
alias composer='docker run --rm -v $(pwd):/app composer'
From now when you will type composer
in your terminal, composer will start running. Of course after saving the .bashrc
file you have to close terminal (it was opened) and open it again in order to load the new settings form .bashrc
file.
CAUTION! The .bashrc
file is hidden!
Let's try to make an example from real life. In this example I will install Laravel application but it is only example.
First let's clone Laravel repository:
git clone https://github.com/laravel/laravel.git laravelapp/
We cloned all Laravel files to our local directory laravelapp
. Next we change directory to laravelapp
:
cd laravelapp
Now we are ready to install vendor packages by using this command:
composer install
#or if we didn't create alias:
docker run --rm -v $(pwd):/app composer install
We will see that composer will start downloading packages:
Or course when you will use other commands, composer will work e.g.:
composer require laravelcollective/html
This command will install laravelcollective
package in your Laravel application.
Is it simple? Tell me in comments what do you think.
It would be great if you will comment or follow me on social media:
Also you can visit my websites:
Top comments (3)
Also volume in your $COMPOSER_HOME or $HOME/.config/composer directory for added speed, and to include any github auth you have set up on your host system.
It's all documented in the docker hub page for composer. hub.docker.com/_/composer
Including --user $(id -u):$(id -g) will keep files from being created as root in your home directory.
Thanks for useful comment!
I would suggest you to change a little bit:
'docker run --rm -u "$(id -u):$(id -g)" -v $(pwd):/app composer'
thus, the user that will run the command is the same as logged one.