Amitav Roy

Blog on web and travel

Install Laravel on your machine with Docker composer container

Posted on 22 Nov 2021 by Amitav Roy

Install Laravel on your machine with Docker composer container

It’s not necessary that busy days will mean your energy is completely drowned out. After long hours on call, finally I got a chance to dig deeper into the problem. This was bothering me for almost 2 days now and so I was up for the challenge. What’s the problem that I was trying to solve? - I want to install Laravel using a Composer image.

You may ask why? I do have PHP and Composer installed on my machine. The reason I am trying to do that is because of the Docker philosophy. I just need Docker installed on my machine. Rest everything will be a container.

Now coming to the point - where was I stuck? After a little bit of reading, I was able to find out how to create a composer container using the official Docker hub image to execute any composer command. But, what I was not able to figure out was how to get the PHP version inside the container to be PHP 7.4 instead of PHP 8.

When I was executing the following command on my terminal “docker run --rm -i -t -v $PWD:/app composer create-project laravel/laravel ProjectName”, I was able to install Laravel. But the dependencies were downloaded for PHP 8 because the container running composer and PHP was set to that. And, I wanted PHP 7.4. I started to analyse the composer's image to understand where that version is coming from. I could see that on line 12, an ENV variable was set to PHP 8. This means, if I could override that somehow then I would be able to get PHP 7.4.14 which is the recent one right now. Stackoverflow showed me a few results on how to do that. But, unfortunately it didn’t work. Here is a screenshot of the same:

Laravel installation through Docker

Sometimes, when you are stuck with one approach then it’s good to try something else. And, that’s what clicked for me. I read one article where a custom docker image is created which spins a PHP 7.4 container and installs composer on it. Now, from a requirements point of view this is solving my problem and hence I was able to quickly create a Dockerfile for that and mark the task as done.

But, the only thing which I still need to research is - is it possible to override the ENV variable? If yes, then for me it’s just one line of command which will do the trick for me. Right now however, using this gist I can create an image stored locally which I can use to install Laravel without having anything on my machine.

Image courtesy https://unsplash.com/@carrier_lost