Add global phpcs and drupal/coder to ddev in custom Dockerfile - ddev

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.

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=""

Related

I can't install composer globally in my web host, how can I use the "composer" command?

I hosted my website on french host Gandi, with their simple hosting plan.
I cannot move anything in the /usr/local/bin directory since it is read-only, so I used to manage with composer.phar, which works well.
I recently used a library which requires the composer executable to be present (This library executes something like "composer require xxx" and there is no fallback to composer.phar).
Is there a way to make it work ?
What I have done so far :
Tried to install composer globally (Failed because of the read-only filesystem)
Tried to install composer globally for the current user (Failed because there was no ~/.local/bin directory, and also failed after creating the directory and restarting the instance)
Tried to move the file to any directory of the $PATH variable (Failed because all of these directories are read-only)
Tried to rename composer.phar to composer, and allowed it to be executable chmod +x composer (Failed because it only works with the command ./composer and not with composer)
Ok, I finally succeeded with the following steps:
renamed composer.phar to composer mv composer.phar composer
made this new file executable chmod +x composer
added the current path (corresponding to my website's root) to the system's $PATH variable export PATH=$PATH:$PWD
I'm not fully satisfied with this since this relies on a specific website's root folder, but hey, it works! I'll try to update this answer if I find a way to create a folder available for every website I'll host there.

Composer: Command Not Found on Mac

I have installed Composer as per the instructions command not found
After installing I get the prompt that Composer is successfully installed but when I go to check the version it gives me error "Composer: Command Not Found"
I have been looking how to fix this issue and I had to read and understand little bit and the environment variables on MAC.
I understand the issue looks simple but it will get over complicated if you didn't understand how to install composer probably.
Solution 1
I found this solution here: https://duvien.com/blog/installing-composer-mac-osx
Open a terminal and navigate to your user directory, ie cd /User//
Run this command shown below to download Composer. This will create a Phar (PHP Archive) file called composer.phar:
curl -sS https://getcomposer.org/installer | php
Now we move composer.phar file to a directory
sudo mv composer.phar /usr/local/bin/
We want to run Composer with having to be root al the time, so we need to change the permissions:
sudo chmod 755 /usr/local/bin/composer.phar
Next, we need to let Bash know where to execute Composer:
nano ~/.bash_profile
Add this line below to bash_profile and save
alias composer="php /usr/local/bin/composer.phar"
and then run this command:
source ~/.bash_profile
Finally, run:
composer --version
Solution 2:
I understand that the first command you will find online when you try to google this issue would be
curl -sS https://getcomposer.org/installer | php
But actually this is the main terminal installation but sometimes I wanted to manually install the composer, so this 2nd solution about manually installing the composer in specific directory
First of all you need to understand where are you now on the terminal
use
pwd
and then install composer manually using the following commands
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') ==='baf1608c33254d00611ac1705c1d9958c817a1a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182') { echo 'Installer verified'; } else { echo 'Installer corrupt';
unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
to use it on that case you will read on the terminal to use this composer installation use
Use it: php composer.phar
so just write
php composer.phar
and it should be working.... one more thing, during this installation you can install the composer to specific directory / project folder... in that case you can use a flag with command No.4 to tell the terminal to install the composer in specific directory...this flag is --install-dir=your-directory-path
and you will the terminal this time after installing composer asking you to use the following path to call the composer
Use it: php /Users/muatafa/composer.phar
if you want to read more about this issue, I think you must read the composer documentation how to install it>>> https://getcomposer.org/download/
that's how I solved my current issue... if you still have any issues comment on this replay & Hopefully we can figure it out!
I had the same problem. "composer require something" works if installed in global and not locally.
If you install composer locally you'll then install dependencies using "php composer.phar require nameofyoudependency"
To install globally open a terminal to install as mentionned on the composer website. Then run "sudo mv composer.phar /usr/local/bin/composer"
Now you can use "composer" directly to install dependencies like => "composer require something/sdk"
the composer you installed is still named /usr/local/bin/composer.phar - look in the second yellow/brownish line in your output. Just rename it to just composer
Your composer command is not set to path, use
export PATH=$PATH:/usr/local/bin/
I had the same issue, After installing composer using the command line on their website. I forgot to put composer.phar into a directory on my PATH, so it can simply be called from any directory (Global install). and then I ran this command "sudo mv composer.phar /usr/local/bin/composer" and it was resolved
I figured out. in place of "composer require twilio/sdk" I needed to use
"composer.phar require twilio/sdk" since I am on mac. It worked

Laravel package installation on live server

How to install package in laravel on live server, whenever I run "command composer require monolog/monolog". It gives error Could not open input file: /home/root/composer.phar
First of all, you should not run composer require on live server. You should only run composer install so you should require it locally, test if everything is working fine and then on live server just install library that is tested in your application.
About error:
Could not open input file: /home/root/composer.phar
make sure this file really exists (if not you can download it from Composer site) and make sure it has correct permissions (it should have execute permission) - use chmod +x composer.phar to make it executable
Please try like this.
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
$ php composer.phar update monolog/monolog
You can install package without any problem.

How to cache package manager downloads for docker builds?

If I run composer install from my host, I hit my local composer cache:
- Installing deft/iso3166-utility (1.0.0)
Loading from cache
Yet when building a container having in its Dockerfile:
RUN composer install -n -o --no-dev
I download all the things, e.g.:
- Installing deft/iso3166-utility (1.0.0)
Downloading: 100%
It's expected, yet I like to avoid it. As even on a rebuilt, it would also download everything again.
I would like to have a universal cache for composer that I could also reshare for other docker projects.
I looked into this and found the approach to define a volume in the Dockerfile:
ENV COMPOSER_HOME=/var/composer
VOLUME /var/composer
I added that to my Dockerfile, and expected to only download the files once, and hit the cache afterwards.
Yet when I modify my composer, e.g. remove the -o flag, and rerun docker build ., I expected to hit the cache on build, yet I still download the vendors again.
How are volumes supposed to work to have a data cache inside a docker container?
Use the experimental feature : Docker buildkit (Supported Since docker 18.09, docker-compose 1.25.4)
In your dockerfile
# syntax=docker/dockerfile:experimental
FROM ....
# ......
RUN --mount=type=cache,target=/var/composer composer install -n -o --no-dev
Now before building, make sure the env var is exported:
export DOCKER_BUILDKIT=1
docker build ....
If you are using docker-compose, make sure to export also COMPOSE_DOCKER_CLI_BUILD :
export COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1
docker-compose build ...
If it does not work with docker-compose, make sure your docker-compose version is above 1.25.4
docker-compose version
I found two ways of dealing with this problem, yet none deal with composer volumes anymore.
Fasten composer download process: Use hirak/prestissimo
composer global require "hirak/prestissimo:^0.3"
💡 With Composer 2.0, the above step is no longer required for faster downloads. In fact, it won't install on Composer 2.0 environments.
Force docker to use a cached composer install. Docker uses a cache on a RUN if the added files didn't change. If you only do COPY . /your-php-app, docker build will refresh all the cashes and re-run composer install even if only one unrelated file in the source tree changed. In order to make docker build to run composer install only install on package changes, one has to add composer.json and composer.lock file before adding the source files. Since one also needs the source files anyway, one has to use different folders for composer install and rsync the content back to the then added folder; furthermore one then has to run the post-install scripts manually. It should look something like this (untested):
WORKDIR /tmp/
COPY composer.json composer.lock ./
RUN composer install -n -o --no-dev --no-scripts
WORKDIR /your-php-app/
COPY . /your-php-app/
RUN rsync -ah /tmp/* /your/php-app/
RUN composer run-script post-install-cmd
or combine the two =)
I would like to have a universal cache for composer that I could also reshare for other docker projects.
Using a shared volume for the Composer cache works great when working with containers. If you want to go broader than just containers, and use a shared cache for e.g. local development as well, I've developed a solution for that called Velocita - how it works.
Basically, you use one global Composer plugin for local projects and inside and build containers. This not only speeds up downloads tremendously, it also helps with 3rd party outage for example.
I would consider utilizing the $HOME/.composer/cache/files directory. This is where composer reads/write to when using composer install.
If you are able to mount it from your host to your container that would work. Also you could just tar it up after each time your run composer install and then drop that in before you run composer install the next time.
This is loosely how Travis CI recommends doing this.
Also, consider using the --prefer-dist flag with your composer install command.
Info on that can be found here: https://getcomposer.org/doc/03-cli.md#install
--prefer-dist: Reverse of --prefer-source, composer will install from dist if possible. This can speed up installs substantially on build servers and other use cases where you typically do not run updates of the vendors. It is also a way to circumvent problems with git if you do not have a proper setup.
Some references on utilizing the composer cache for you:
https://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/
https://github.com/travis-ci/travis-ci/issues/4579

what is phpmd and how to use it?

I have Ubuntu 14.04 + Sublime text 3 and installed phpcs packages
additionalty I have installed phpcs and php-cs-fixer on my system
From this blog
I have found that phpmd (PHP Mess Detector) is also a required library, so installed phpmd as per given instructions on official php md page using alternative method From the github repository everything was finished.
:~/phpmd$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /home/keshav/phpmd/composer.phar
Use it: php composer.phar
but now when I write on terminal
phpmd /opt/lampp/htdocs/myproject, myfile.php
phpmd: command not found
There is phpmd folder on Home directory and everything without any error.
I have local project on core PHP create composer.json in project folder as per suggested on github .
Please tell me what means by
Then install Composer in your project (or download the composer.phar directly):
I think the problem is you've installed phpmd in a local directory, but you're trying to use it as if it was installed globally.
Installation instruction on the referenced sites can't really be made any clearer. Since you've already installed phpcs and php-cs-fixer, and those work for you, just follow similar instructions for phpmd. These are all PHP projects and are installed in a similar way.
Anyway, to use phpmd as a global command you have several options.
Github
Clone the github repository just like you did and add the phpmd bin directory to your PATH variable.
Global composer installation
Use the composer global command to install phpmd globally. You will also need to make sure that composer's bin directory is in the PATH. By default it's ~/.composer/vendor/bin.
composer global require phpmd/phpmd
This command will install phpmd globally, and as soon as ~/.composer/vendor/bin is in your PATH you'll be able to call it by simply invoking phpmd.
It's very well explained in composer's documentation: https://getcomposer.org/doc/03-cli.md#global
Download the phar archive
This is the simplest thing you can do. Simply go the phpmd releases, choose the latest and download the phar archive.
Put the phar file to whatever place you'd like. Just remember that it needs to be in your PATH. You can also rename it, to skip the .phar extension.
For example:
wget http://static.phpmd.org/php/2.1.3/phpmd.phar
sudo mv phpmd.phar /usr/bin/phpmd
sudo chmod +x /usr/bin/phpmd
Docker container
First, fetch the docker image with static analysis tools for PHP:
docker pull jakzal/phpqa
One of the tools provided by the image is phpmd. The command below will run phpmd in a docker container and mount the current working directory as a /project.
docker run -it --rm -v $(pwd):/project -w /project jakzal/phpqa \
phpmd src text cleancode,codesize,controversial,design,naming,unusedcode
When you use the composer-based install, it gets installed into the ./bin directory within the ./vendors directory. So for me, relative to my project's root directory, it was here:
./vendor/bin/phpmd
And I was able to run it from my project's root by running ./vendor/bin/phpmd . text codesize. (I'm not getting any useful output yet, but another issue)

Resources