Skip to content

Run everything in custom Docker image? #112

@hkirsman

Description

@hkirsman

The problem

Sometimes we have to uninstall a tool because it's dependency does not match with Drupal. Also the more tools we add, the more problems there will be with dependencies.

Solution

Could everything be intalled into Docker image so it could be run from anywhere and from any system?

Here's Dockerfile that Claude suggested:

# Use PHP 8.2 as base image
FROM php:8.2-cli

# Install required system packages
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    nodejs \
    npm \
    && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pcntl

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create working directory
WORKDIR /app

# Install global tools without conflict
RUN composer global require \
    phpstan/phpstan:^1.10 \
    vimeo/psalm:^5.15 \
    squizlabs/php_codesniffer:^3.7 \
    phpcompatibility/php-compatibility:^9.3 \
    dealerdirect/phpcodesniffer-composer-installer:^1.0 \
    && composer clear-cache

# Install ESLint globally
RUN npm install -g eslint

# Add Drupal coding standards
RUN composer global require drupal/coder:^8.3

# Create configuration directory
RUN mkdir -p /config

# Copy default configuration files
COPY phpstan.neon /config/phpstan.neon
COPY psalm.xml /config/psalm.xml
COPY phpcs.xml /config/phpcs.xml

# Add scripts directory
COPY scripts /scripts
RUN chmod +x /scripts/*

# Add to PATH
ENV PATH="/scripts:/root/.composer/vendor/bin:${PATH}"

# Create entrypoint script
RUN echo '#!/bin/sh\n\
if [ "$1" = "psalm" ]; then\n\
    psalm --config=/config/psalm.xml "${@:2}"\n\
elif [ "$1" = "phpstan" ]; then\n\
    phpstan analyse --configuration=/config/phpstan.neon "${@:2}"\n\
elif [ "$1" = "phpcs" ]; then\n\
    phpcs --standard=/config/phpcs.xml "${@:2}"\n\
else\n\
    exec "$@"\n\
fi' > /usr/local/bin/entrypoint.sh \
    && chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

Build the image:

docker build -t code-quality-tools .

Run individual tools:

# For Psalm
docker run -v $(pwd):/app code-quality-tools psalm /app/src

# For PHPStan
docker run -v $(pwd):/app code-quality-tools phpstan /app/src

# For PHPCS
docker run -v $(pwd):/app code-quality-tools phpcs /app/src

You can modify your GrumPHP configuration to use these Docker commands instead of local tools. Here's how you could update your configuration:

parameters:
  docker_image: 'code-quality-tools'
  grumphp.run_on_paths: ['web/modules/custom', 'web/themes/custom', 'shared']
grumphp:
  tasks:
    psalm:
      metadata:
        command: 'docker run -v $(pwd):/app ${docker_image} psalm'
    phpstan:
      metadata:
        command: 'docker run -v $(pwd):/app ${docker_image} phpstan'
    phpcs:
      metadata:
        command: 'docker run -v $(pwd):/app ${docker_image} phpcs'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions