PHP Code Sniffer with Docker

Yani Iliev
2 min readOct 2, 2016

--

In this post I will tell about my experience running PHPCS in a Docker container. I will share my Docker file and the script I use to run PHPCS on my projects.

I am using Alpine for my containers. I picked Alpine because it produces small Docker images and has all of the packages that I need. Let’s look at my Dockerfile for building my phpcs image:

FROM alpine:edgeRUN apk --no-cache add \
ca-certificates \
curl \
php5-cli \
php5-dom \
php5-iconv \
php5-phar \
php5-xdebug \
php5-zlib
WORKDIR /tmpRUN curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar && \
cp /tmp/phpcs.phar /usr/local/bin/phpcs && \
chmod +x /usr/local/bin/phpcs
# Set some useful defaults to phpcs
# show_progress - I like to see a progress while phpcs does its magic
# colors - Enable colors; My terminal supports more than black and white
# report_width - I am using a large display so I can afford a larger width
# encoding - Unicode all the way
RUN /usr/local/bin/phpcs --config-set show_progress 1 && \
/usr/local/bin/phpcs --config-set colors 1 && \
/usr/local/bin/phpcs --config-set report_width 140 && \
/usr/local/bin/phpcs --config-set encoding utf-8
ENTRYPOINT ["/usr/local/bin/phpcs"]

It is a pretty simple Dockerfile. The interesting part is towards the end of the file where I update phpcs config because default phpcs configuration doesn’t suit me well. There is a good list of configuration options here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options

To build this Dockerfile, I use:

docker build . yani/phpcs

This will produce an image named yani/phpcs that I can use to run phpcs.

To run phpcs, I’ve created a small bash script in /usr/local/bin/phpcs

#!/bin/bash
docker run -v $(pwd):$(pwd) -w=$(pwd) --rm yani/phpcs $@

What this script does is it will mount the same folder that I am in when executing phpcs and set the working directory in the container to be the same as to the one I am in. Example:
I am in /repos/unicorn. I execute phpcs. It will start a container running yani/phpcs mount /repos/unicorn from my filesystem to the container at the same location (/repos/unicorn). It will then change the working directory to /repos/unicorn and execute phpcs application. The $@ at the end of the of the line tells the script to include all parameters that I have called the script with. This allows me to run phpcs file-to-lint.php

Happy linting!

--

--