Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Running Composer Install With Docker

I work on many Dockerized PHP-based web applications. As time goes by they use different version's of PHP with different extensions, which means that if I perform a composer update locally, I might not install a suitable package, and if I perform a composer install, it might fail to fetch the packages specified in the composer.lock file. This is because a package's composer.json file can specify what minimum version of PHP it expects, or any extensions it requires, which is tied to the version of the package and can change over time. Thus if your Dockerfile is using an older version of PHP, it may require an older package than you might not install if your local version of PHP is a later version.

Steps

If you add composer to the list of packages you install in your Dockerfile, you can use your web application's image to perform the composer install like so:

#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

docker run \
  --volume $DIR/app:/var/www/my.site.com \
  docker-registry.mydomain.com:5000/my-project-name \
  /usr/bin/composer install -d /var/www/my.site.com

This is assuming your "site" is located in the Docker container at /var/www/my.site.com and your composer.json file is in a folder called app from where this script is located.

Likewise, you can update your packages with the following:

#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

docker run \
  --volume $DIR/app:/var/www/my.site.com \
  docker-registry.mydomain.com:5000/my-project-name \
  /usr/bin/composer update -d /var/www/my.site.com
Last updated: 30th March 2020
First published: 30th March 2020