DEV Community 👩‍💻👨‍💻

Cover image for Composer in Docker
Chris Texe
Chris Texe

Posted on

Composer in Docker

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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 laravelapp/
Enter fullscreen mode Exit fullscreen mode

We cloned all Laravel files to our local directory laravelapp. Next we change directory to laravelapp:

cd laravelapp
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We will see that composer will start downloading packages:

Composer in Docker

Or course when you will use other commands, composer will work e.g.:

composer require laravelcollective/html
Enter fullscreen mode Exit fullscreen mode

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:

Chris Texe Twitter

Chris Texe LinkedIn

Also you can visit my websites:

Latest comments (2)

baohx profile image
Gordon Forsythe

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.
Including --user $(id -u):$(id -g) will keep files from being created as root in your home directory.

texe profile image
Chris Texe

Thanks for useful comment!

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.