Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Created September 16, 2022 23:05
Show Gist options
  • Save MatthewDaniels/ba052cd8f380c38f98e4ee396049c537 to your computer and use it in GitHub Desktop.
Save MatthewDaniels/ba052cd8f380c38f98e4ee396049c537 to your computer and use it in GitHub Desktop.
Use this script to quickly and easily run composer commands without the need for it being installed on the host machine.

Composer Command Runner

Overview

Use this script to quickly and easily run composer commands without the need for it being installed on the host machine.

This concept is used in Google Cloud's Cloud Build whereby the working directory (where your php code is) is mounted inside a docker container to execute isolated steps in a build pocess.

Run the script from within a project to install and maintain dependencies or from a parent directory to use the create-project command.


Usage

To execute a command: ./composer-command.sh {{ composer commands }} the {{ composer commands }} above get passed to composer for execution (eg: install)

Some suggested composer options (add on the end of your commands): --dev for development --prefer-dist --optimize-autoloader for production --ignore-platform-reqs for all platforms so composer does not check the "platform" (as that is the composer docker container)

Note: the script always includes the --verbose command - remove this if you want the commands to be quieter.

Examples

Install dependencies in your dev box (run this within your project folder) ./composer-command.sh install --dev --ignore-platform-reqs

Install Laravel (run this from a parent directory) ./composer-command.sh create-project laravel/laravel example-app --ignore-platform-reqs


Info / more options / ideas

see: https://hub.docker.com/_/composer

#!/bin/bash
# use this to run composer commands in a container (rather than on the host machine)
# to execute:
# ./composer-command.sh {{ composer commands }}
# the {{ composer commands }} above get passed to composer for execution (eg: "install")
#
# ./composer-command.sh install --dev
#
# Install Laravel (run this from a parent directory)
# ./composer-command.sh create-project laravel/laravel example-app --ignore-platform-reqs
# Some suggested composer options
# '--dev' when using this for dev work
# '--prefer-dist --optimize-autoloader' for production
# '--ignore-platform-reqs' for all platforms so composer does not check the "platform" (as that is the composer docker container)
# Info / more options / ideas
# @see: https://hub.docker.com/_/composer
docker run --rm --interactive --tty \
--volume $PWD:/app \
composer "$@" --verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment