Skip to content

Instantly share code, notes, and snippets.

@akerouanton
Last active May 11, 2016 23:58
Show Gist options
  • Save akerouanton/1a781d966c775e37c052149447a6224c to your computer and use it in GitHub Desktop.
Save akerouanton/1a781d966c775e37c052149447a6224c to your computer and use it in GitHub Desktop.
{# Variables: php_version, src_dir, packages, php_extensions, pecl_packages, php_ini_configs, php_fpm_configs -#}
FROM php:{{ php_version }}-fpm
# Install packages
RUN apt-get update && \
apt-get install -y --no-install-recommends \
{% for package in packages -%}
{{ package }} \
{% endfor -%}
&& \
rm -rf /var/lib/apt/lists/*
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
{% if pre_ext_install != '' %}
RUN {{ pre_ext_install }}
{% endif %}
# Configure php & php-fpm
ENV PHP_INI /usr/local/etc/php/php.ini
ENV PHP_FPM /usr/local/etc/php-fpm.conf
{% if php_extensions != [] -%}
# Install custom php extensions
RUN docker-php-ext-install {{ php_extensions|join(' ') }}
{% endif %}
{% if pecl_packages != [] -%}
# Install pecl packages
RUN pecl config-set php_ini ${PHP_INI}
RUN yes | pecl install {{ pecl_packages|join(' ') }}
{% endif %}
{% if php_ini_configs != [] -%}
RUN echo "" >> ${PHP_INI} && \
{% for parameter in php_ini_configs %}
echo "{{ parameter }}" >> ${PHP_INI} {% if not loop.last %} && \{% endif %}
{% endfor %}
{%- endif %}
{%- if php_fpm_configs != [] -%}
RUN echo "" >> ${PHP_FPM} \
{% for parameter in php_fpm_configs %}
echo "{{ parameter }}" >> ${PHP_FPM} {% if not loop.last %} && \{% endif %}
{% endfor %}
{%- endif %}
# Create the source directory and set it as the working directory
ENV SRC_DIR {{ src_dir }}
RUN mkdir -p ${SRC_DIR}
WORKDIR ${SRC_DIR}
# Install dependencies
COPY ./composer.* ${SRC_DIR}
RUN php -d memory_limit=-1 /usr/local/bin/composer global require --prefer-dist hirak/prestissimo && \
php -d memory_limit=-1 /usr/local/bin/composer install --no-dev --prefer-dist --no-scripts && \
rm -rf ~/.composer
{% if enable_xdebug %}
RUN pecl install xdebug && \
echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so" >> ${PHP_INI} && \
echo "xdebug.remote_enable=1" >> ${PHP_INI} && \
echo "xdebug.remote_connect_back=1" >> ${PHP_INI}
{% endif %}
# Copy source code
COPY . ${SRC_DIR}
# Copy default parameters file
RUN cp app/config/parameters.yml.dist app/config/parameters.yml
# In order to avoid permission issues
RUN chown -R www-data:www-data ${SRC_DIR}
# Should always be the last !
VOLUME ${SRC_DIR}
#!/usr/bin/env python
import json, re
from jinja2 import Template
PHP_EXTENSIONS = ['exif', 'gd', 'gmp', 'intl', 'mbstring', 'pcntl', 'pdo_mysql', 'zip']
PECL_EXTENSIONS = ['amqp', 'memcache', 'sundown', 'xdebug']
PACKAGES = {
'gd': ['libgd3', 'libpng12-dev'],
'gmp': ['libgmp-dev'],
'memcache': ['libmemcached-dev'],
'amqp': ['librabbitmq-dev'],
'intl': ['libicu-dev'],
'openssl': ['libssl-dev']
}
def get_version(constraint):
return (re.search('(\d.\d+|\w+)', constraint).group(0) if re.search('\d.\d+', constraint) is not None else None)
def extract_required_exts(composer_json):
return {package[4:]: version for package, version in composer_json['require'].items() if package.startswith('ext-')}
def extract_php_version(composer_json):
return [get_version(version) for package, version in composer_json['require'].items() if package == 'php' and get_version(version) is not None].pop()
def get_php_exts(required_exts):
return {package: version for package, version in required_exts.items() if package in PHP_EXTENSIONS}
def get_pecl_packages(required_exts):
return {package: version for package, version in required_exts.items() if package in PECL_EXTENSIONS}
def is_xdebug_required(composer_json):
return True
def format_pecl_package_names(pecl_packages):
return [package if version == '*' else package + '-' + version for package, version in pecl_packages.items()]
def get_packages(extensions):
return [package for extension in extensions if extension in PACKAGES for package in PACKAGES[extension]]
def get_pecl_extension_declarations(pecl_packages):
return ["extension=%s.so" % package for package in pecl_packages]
composer_json = json.loads(open('composer.json', 'r').read())
required_exts = extract_required_exts(composer_json)
src_dir = '/usr/src/app'
php_version = extract_php_version(composer_json)
custom_php_exts = get_php_exts(required_exts)
custom_pecl_packages = get_pecl_packages(required_exts)
packages = ['git', 'zlib1g-dev'] + get_packages(required_exts)
php_extensions = ['zip'] + custom_php_exts.keys()
pecl_packages = format_pecl_package_names(custom_pecl_packages)
php_ini_configs = [
"date.timezone=Europe/Paris",
"cgi.fix_pathinfo=0",
] + get_pecl_extension_declarations(custom_pecl_packages)
php_fpm_configs = [
"catch_worker_output = yes",
"user = 1000",
"group = 1000",
"request_terminate_timeout = 300"
]
php_dockerfile = Template(open('Dockerfile-php.j2', 'r').read()).render(
php_version=php_version,
src_dir=src_dir.rstrip('/') + '/',
packages=sorted(packages),
php_extensions=sorted(php_extensions),
pecl_packages=sorted(pecl_packages),
php_ini_configs=sorted(php_ini_configs),
php_fpm_configs=sorted(php_fpm_configs),
pre_ext_install='ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h',
enable_xdebug=is_xdebug_required(composer_json)
)
open('Dockerfile', 'w').write(php_dockerfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment