If you use the Composer Docker container you have the problem, that there is no Composer package cache, because the state of the Docker container is not saved. That's no problem, if you have only one project. If you have multiple projects it's a waste of time to download every package again and again. There is a simple trick to solve this problem.

Start Composer Docker container

Run the following command from the root of your project to start the Composer Docker container. The current directory of your location will be mounted to the Docker contaienr under /app. If you don't have already downloaded the image, no problem. Docker will download the prooph Composer image automatically.

$ docker run --rm -it --volume $(pwd):/app prooph/composer:7.0 [Composer command]

The Composer image may not fulfill the requirements of a library or your project. You can force the Composer installation by adding the argument --ignore-platform-reqs.

Bash script with Composer cache

The following bash script helps to quickly run Composer from the root of your project. The Composer cache directory is mounted to the Composer folder in the Docker container. So the Composer cache will be used in different projects.

#!/bin/bash
docker run --rm -it --volume [your host path]/composer_cache:/root/composer/ --volume $(pwd):/app prooph/composer:7.0 $@

Using ssh-agent forwarding

You have private repositories and want to use SSH authentication? That's no problem, you can mount your ssh-agent socket in the Docker container. Here is the complete bash script with ssh-agent forwarding.

#!/bin/bash
docker run --rm -it --volume $(dirname $SSH_AUTH_SOCK):$(dirname $SSH_AUTH_SOCK) -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK --volume [your host path]/composer_cache:/root/composer/ --volume $(pwd):/app prooph/composer:7.0 $@

If you get an error like getaddrinfo: Name or service not known then add the parameter --dns 8.8.8.8 to the Docker run call.

Conclusion

Using the Composer cache in a Docker container saves you a lot of time when you install packages for different projects. You can mount any other directory in a Docker container if needed. If you have any other hints on using Composer in a Docker container, please feel free to leave a comment.