Composer in Docker
Tags: php, docker, webdev, linux
Published: 2022-08-14 16:48:00

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:

```bash
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:

```bash
#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:

```bash
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`:

```bash
cd laravelapp
```

Now we are ready to install vendor packages by using this command:

```bash
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:


![Composer in Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuxmfej3uw525ls9r0ar.png)

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

```composer 
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*:

[![Chris Texe Twitter](https://madlon.eu/wp-content/uploads/sites/21/TwitterTexeButton.webp)](https://twitter.com/TexeChris)

[![Chris Texe LinkedIn](https://madlon.eu/wp-content/uploads/sites/21/LinkedInTexeButton.webp)](https://www.linkedin.com/in/texe/)

Also you can visit my websites:

- [Linux blog](https://varlock.net)
- [Web Agency website](https://madlon.eu)