5 useful methods of using Composer with Docker on macOS [WIP]

Article at glance

  • Aliases at a shell are must have.
  • You don’t need brew and all dependencies on your Mac directly.
  • It’s possible to use it with the phpStorm.

When I’ve started my an adventure with PHP there was nothing like Composer (I didn’t count PEAR/PECL). Everytime, when I wanted to use dependencies I have to copy it and past it directly in my source code.

It was dark ages in our industry. We have to track our dependencies manually, and subscribe all possible newsletter to make sure there is no security gap.

Because it was difficult, time consuming and not efficient, we didn’t have so many libraries as today. Composer and packagist did amazing job in our environment and change PHP in untold way.

Before we start, you must know that Docker commands are pretty long. There is no way to remember tham and use them in regular work. This is reason why I’ve prepared shell alias to simplify usage of it.

Bellow, you will see each Composer command in three variant:

  1. Docker Without Alias (this is scary version)
  2. Docker With Alias
  3. Without Docker at all

And this is my alias. You have to copy and past it into your ~/.bash_profile file.

alias composer='docker run --init --tty --rm -v $(pwd):/p:delegated -v composer_data:/tmp:delegated -w /p -- composer'

5 useful methods of using Composer with Docker on macOS

1. Install/Add dependency

docker run --init --tty --rm \
           -v $(pwd):/p:delegated \
           -v composer_data:/tmp:delegated -w /p \
           -- composer require symfony/finder

composer require symfony/finder
composer require symfony/finder

2. Update dependency

docker run --init --tty --rm \
           -v $(pwd):/p:delegated \
           -v composer_data:/tmp:delegated -w /p \
           -- composer update symfony/finder
composer update symfony/finder
composer update symfony/finder

3. Run Commands

docker run --init --tty --rm \
           -v $(pwd):/p:delegated \
           -v composer_data:/tmp:delegated -w /p \
           -- composer run release
composer run release
composer run release

4. Outdated libraries

docker run --init --tty --rm \
           -v $(pwd):/p:delegated \
           -v composer_data:/tmp:delegated -w /p \
           -- composer remove symfony/finder
composer remove symfony/finder
composer remove symfony/finder

Tweet this Composer command

5. Build Production Autoloader

docker run --init --tty --rm \
           -v $(pwd):/p:delegated \
           -v composer_data:/tmp:delegated -w /p \
           -- composer dump-autoload --classmap-authoritative
composer run dump-autoload --classmap-authoritative
composer run dump-autoload --classmap-authoritative

Summary

EXTRA: Performance Issue

EXTRA: Configuration for phpStorm

EXTRA: 5 not popular but very usufull commands

  • composer about – Shows the short information about Composer.
  • composer archive – Creates an archive of this composer package.
  • composer bin – Run a command inside a bin namespace
  • composer browse – Opens the package’s repository URL or homepage in your browser.
  • composer cc – Clears composer’s internal package cache.
  • composer check-platform-reqs – Check that platform requirements are satisfied.
  • composer clear-cache – Clears composer’s internal package cache.
  • composer clearcache – Clears composer’s internal package cache.
  • composer config – Sets config options.
  • composer create-project – Creates new project from a package into given directory.
  • composer depends – Shows which packages cause the given package to be installed.
  • composer diagnose – Diagnoses the system to identify common errors.
  • composer dump-autoload – Dumps the autoloader.
  • composer dumpautoload – Dumps the autoloader.
  • composer exec – Executes a vendored binary/script.
  • composer fund – Discover how to help fund the maintenance of your dependencies.
  • composer global – Allows running commands in the global composer dir ($COMPOSER_HOME).
  • composer help – Displays help for a command
  • composer home – Opens the package’s repository URL or homepage in your browser.
  • composer i – Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  • composer info – Shows information about packages.
  • composer init – Creates a basic composer.json file in current directory.
  • composer install – Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  • composer licenses – Shows information about licenses of dependencies.
  • composer list – Lists commands
  • composer outdated – Shows a list of installed packages that have updates available, including their latest version.
  • composer prohibits – Shows which packages prevent the given package from being installed.
  • composer remove – Removes a package from the require or require-dev.
  • composer require – Adds required packages to your composer.json and installs them.
  • composer run – Runs the scripts defined in composer.json.
  • composer run-script – Runs the scripts defined in composer.json.
  • composer search – Searches for packages.
  • composer self-update – Updates composer.phar to the latest version.
  • composer selfupdate – Updates composer.phar to the latest version.
  • composer show – Shows information about packages.
  • composer status – Shows a list of locally modified packages, for packages installed from source.
  • composer suggests – Shows package suggestions.
  • composer u – Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  • composer update – Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  • composer upgrade – Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  • composer validate – Validates a composer.json and composer.lock.
  • composer why – Shows which packages cause the given package to be installed.
  • composer why-not – Shows which packages prevent the given package from being installed

Newsletter

Do you like this post? Join to my a newsletter. In first a mail, I will share you Docker Cheat Sheet which I use almost everyday.

Keep in touchI will share you everything I know about PHP.

Marcin Pilsniak

Author: Marcin Pilsniak

I have been using PHP for 13 years. Furthermore, for the last 4 years I have been using AWS in my everyday work. I’m certified AWS Architect, Developer and SysOps.