2

Following the examples in https://www.ddev.com/ddev-local/customizing-ddev-local-images-with-a-custom-dockerfile/ and the example Docker file, I tried to composer global require drupal/coder:

ARG BASE_IMAGE=drud
FROM $BASE_IMAGE
RUN composer global require drupal/coder:8.3.5 --verbose
RUN composer global require dealerdirect/phpcodesniffer-composer-installer --verbose
RUN export PATH="$PATH:$HOME/.composer/vendor/bin"
RUN phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder

This didn't work as expected, as it installs for root. However the user that will later try to invoke it, isn't root.

amitaibu
  • 1,026
  • 1
  • 7
  • 23

1 Answers1

1

As answered by @rfay in https://github.com/drud/ddev/issues/2173#issuecomment-613671025

Everything in Dockerfile build time (for all Dockerfiles everywhere) happens as root. composer global require should just be using root's home directory as a temporary storage place.

In this case, phpcs isn't really being installed globally, it's being installed in root's composer cache. Instead this should be done:

ARG BASE_IMAGE
FROM $BASE_IMAGE

ENV COMPOSER_HOME=/usr/local/composer

# We try to avoid when possible relying on composer to download global, so in PHPCS case we can use the phar.
RUN curl -L https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar -o /usr/local/bin/phpcs && chmod +x /usr/local/bin/phpcs
RUN curl -L https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar -o /usr/local/bin/phpcbf && chmod +x /usr/local/bin/phpcbf

# If however we need to download a package, we use `cgr` for that.
RUN composer global require consolidation/cgr
RUN $COMPOSER_HOME/vendor/bin/cgr drupal/coder:^8.3.1
RUN $COMPOSER_HOME/vendor/bin/cgr dealerdirect/phpcodesniffer-composer-installer

# Register Drupal's code sniffer rules.
RUN phpcs --config-set installed_paths $COMPOSER_HOME/global/drupal/coder/vendor/drupal/coder/coder_sniffer --verbose
# Make Codesniffer config file writable for ordinary users in container.
RUN chmod 666 /usr/local/bin/CodeSniffer.conf
# Make COMPOSER_HOME writable if regular users need to use it.
RUN chmod -R ugo+rw $COMPOSER_HOME
# Now turn it off, because ordinary users will want to be using the default
ENV COMPOSER_HOME=""
amitaibu
  • 1,026
  • 1
  • 7
  • 23
  • 1
    The "normal" way to install this is just site-local though right? So if these are installed in the project then none of this is needed? – rfay May 18 '20 at 13:59
  • 2
    Correct, that's why it's titled "Add global phpcs". Indeed, one can `ddev composer require --dev` instead. – amitaibu May 18 '20 at 17:46
  • Going with global phpcs, even though it's the recommended way to install according to the GitHub page - was giving errors. So I recommend going with local PHPCS, as we've done here -- https://github.com/Gizra/drupal-starter/pull/43 – amitaibu Jul 08 '20 at 20:35