Skip to content

Instantly share code, notes, and snippets.

@victorsmirnov
Created March 15, 2022 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorsmirnov/c16a0bd633ee4e6de8c5eac49459dabc to your computer and use it in GitHub Desktop.
Save victorsmirnov/c16a0bd633ee4e6de8c5eac49459dabc to your computer and use it in GitHub Desktop.
Run PHP in the Docker container.

The Gist shows how to run the PHP script in a controlled environment using Docker.

Define all the necessary libraries and extensions in the Dockerfile. The file in the Gist provides some examples; check it for tricks and inspiration. Next, build a local image using the command make build.

For example, run the composer update command in the project folder using the Docker container.

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --volume /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock \
    --env SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock \
    --workdir /app \
    php-local:7.4.10 composer install

We mount SSH keys from the SSH agent using macOS specific hack.

FROM php:7.4.10-cli
RUN apt-get update \
&& apt-get install -y gettext-base libfreetype6-dev libjpeg62-turbo-dev libpng-dev libxml2-dev \
libmcrypt-dev libc-client-dev libkrb5-dev libzip-dev \
imagemagick libmagickwand-dev \
wget git zip procps \
xfonts-75dpi xfonts-base xfonts-utils libfontenc1 xfonts-encodings \
cron \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /etc/cron.*/*
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN pecl config-set php_ini "$PHP_INI_DIR/php.ini"
RUN pear config-set php_ini "$PHP_INI_DIR/php.ini"
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-configure zip && docker-php-ext-install zip
RUN docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap
RUN docker-php-ext-install bcmath intl mysqli opcache pdo_mysql soap zip
RUN pecl install mongodb && docker-php-ext-enable mongodb
RUN pecl install apcu && docker-php-ext-enable apcu
RUN pecl install apcu_bc
RUN pecl install mcrypt-1.0.3 && docker-php-ext-enable mcrypt
RUN pecl install imagick && docker-php-ext-enable imagick
RUN pecl channel-update pecl.php.net \
&& pecl install xdebug-2.9.1 \
&& sed -i '/zend_extension="xdebug.so"/d' /usr/local/etc/php/php.ini

ISC License

Copyright (c) 2021, Victor Smirnov admin@victorsmirnov.blog

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

build:
docker build -t php-local:7.4.10 --ssh default .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment