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: 28th February 2022
First published: 30th March 2020

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch