How to use PHP Composer as a Docker container?

A new way to use applications without installing it on your OS.

Requirement: Docker installed

It’ very easy to replace the PHP and Composer installation by Docker. Once Docker installed, you will create a shell script file that will start a container when you type the composer command.

Follow the steps bellow:

  1. Open the terminal

  2. Copy the commmands bellow:

cat > /usr/local/bin/composer <<EOF
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
echo "Current working directory: '"$(pwd)"'"
docker run --rm --interactive --tty -v $(pwd):/app -v ~/.ssh:/root/.ssh composer $@
EOF
  1. Make the file executable:
chmod +x /usr/local/bin/composer

It’s done!

For testing, go to a php application directory and type composer update. You will see the composer running as if it were installed. When the process finish, the container will be destroyed. Amazing, isn’t it?

PS: It’s important to explain that the command will mount two volumes inside the container: one for the application folder and one for the ssh keys. The last one allow composer download private packages.