Files
docker-php-fpm/.ansible/DOCKERFILES/Dockerfile-mods.j2
2022-12-07 16:41:34 +01:00

477 lines
20 KiB
Django/Jinja

# vi: ft=dockerfile
{{ edit_comment_mods }}
FROM devilbox/php-fpm:{{ php_version }}-base as builder
{#
###################################################################################################
Loop over enabled build dependencies and gather build dependencies
###################################################################################################
#}
{%- set build_deps = [] -%}
{%- for ext in extensions_enabled -%}
{#- Not disabled AND not already available by default -#}
{%- if (('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled']))
and ('already_avail' not in extensions_available[ext] or php_version not in extensions_available[ext]['already_avail']) -%}
{#- Version specific build dependency available? -#}
{%- if php_version in extensions_available[ext] and 'build_dep' in extensions_available[ext][php_version] -%}
{%- for build_dep in extensions_available[ext][php_version]['build_dep'] -%}
{#- Append build dependencies to our array -#}
{{- build_deps.append(build_dep) -}}
{%- endfor -%}
{#- Generic build dependency available? -#}
{%- elif 'all' in extensions_available[ext] and 'build_dep' in extensions_available[ext]['all'] -%}
{%- for build_dep in extensions_available[ext]['all']['build_dep'] -%}
{#- Append build dependencies to our array -#}
{{- build_deps.append(build_dep) -}}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
###
### Install
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{% for build_dep in build_deps | unique | sort %}
{{ build_dep }} \
{% endfor %}
# Build tools
autoconf \
bison \
bisonc++ \
ca-certificates \
curl \
dpkg-dev \
file \
flex \
g++ \
gcc \
git \
lemon \
libc-client-dev \
libc-dev \
libcurl4-openssl-dev \
libssl-dev \
make \
patch \
pkg-config \
re2c \
xz-utils \
&& rm -rf /var/lib/apt/lists/*
# Fix timezone (only required for testing to stop php -v and php-fpm -v from complaining to stderr)
RUN set -eux \
&& echo "date.timezone=UTC" > /usr/local/etc/php/php.ini
###
### Install and enable PHP modules
###
# Enable ffi if it exists
RUN set -eux \
&& if [ -f /usr/local/etc/php/conf.d/docker-php-ext-ffi.ini ]; then \
echo "ffi.enable = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-ffi.ini; \
fi
{# Loop over enabled extensions #}
{% for ext in extensions_enabled %}
{#-
###############################################################################################
# Not disabled AND not already enabled
###############################################################################################
-#}
{%- if (('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled'])) and ('already_avail' not in extensions_available[ext] or php_version not in extensions_available[ext]['already_avail']) %}
# -------------------- Installing PHP Extension: {{ ext }} --------------------
RUN set -eux \
{#-
# --------------------------------------------------------------------------------
# PRE COMMAND
# --------------------------------------------------------------------------------
-#}
{#- Version specific pre-command available? -#}
{%- if php_version in extensions_available[ext] and 'pre' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Version specific pre-command
{{- "\n\t" }}&& {{ extensions_available[ext][php_version]['pre'] }} \
{#- Generic pre-command available? -#}
{%- elif 'all' in extensions_available[ext] and 'pre' in extensions_available[ext]['all'] %}
{{- "\n\t" }}# Generic pre-command
{{- "\n\t" }}&& {{ extensions_available[ext]['all']['pre'] }} \
{%- endif -%}
{#-
# --------------------------------------------------------------------------------
# INSTALLATION (VERSION SPECIFIC)
# --------------------------------------------------------------------------------
#}
{%- if php_version in extensions_available[ext] and 'type' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Installation: Version specific
{#-
# -------------------- 1.) Builtin --------------------
-#}
{%- if extensions_available[ext][php_version]['type'] == 'builtin' -%}
{{- "\n\t" }}# Type: Built-in extension
{%- if 'configure' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Custom: configure command
{{- "\n\t" }}&& docker-php-ext-configure {{ ext }} {{ extensions_available[ext][php_version]['configure'] }} \
{%- elif 'configure' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Default: configure command
{{- "\n\t" }}&& docker-php-ext-configure {{ ext }} {{ extensions_available[ext]['all']['configure'] }} \
{%- endif -%}
{{- "\n\t" }}# Installation
{{- "\n\t" }}&& docker-php-ext-install -j$(getconf _NPROCESSORS_ONLN) {{ ext }} \
{#-
# -------------------- 2.) PECL --------------------
-#}
{%- elif extensions_available[ext][php_version]['type'] == 'pecl' -%}
{{- "\n\t" }}# Type: PECL extension
{%- if 'command' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Custom: Pecl command
{{- "\n\t" }}&& {{ extensions_available[ext][php_version]['command'] }} \
{%- else -%}
{{- "\n\t" }}# Default: Pecl command
{{- "\n\t" }}&& pecl install {{ ext }}{% if 'version' in extensions_available[ext][php_version] %}-{{ extensions_available[ext][php_version]['version'] }}{% endif %} \
{%- endif -%}
{{- "\n\t" }}# Enabling
{{- "\n\t" }}&& docker-php-ext-enable {{ ext }} \
{#-
# -------------------- 3.) GIT --------------------
-#}
{%- elif extensions_available[ext][php_version]['type'] == 'git' -%}
{{- "\n\t" }}# Type: GIT extension
{{- "\n\t" }}&& git clone {{ extensions_available[ext][php_version]['git_url'] }} /tmp/{{ ext }} \
{{- "\n\t" }}&& cd /tmp/{{ ext }} \
{%- if 'git_ref' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Custom: Branch
{{- "\n\t" }}&& git checkout {{ extensions_available[ext][php_version]['git_ref'] }} \
{%- endif -%}
{%- if 'command' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Custom: Install command
{{- "\n\t" }}&& {{ extensions_available[ext][php_version]['command'] }} \
{%- else -%}
{{- "\n\t" }}# Default: Install command
{{- "\n\t" }}&& phpize \
{{- "\n\t" }}&& ./configure {% if 'configure' in extensions_available[ext][php_version] %} {{ extensions_available[ext][php_version]['configure'] }}{% endif %} \
{{- "\n\t" }}&& make -j$(getconf _NPROCESSORS_ONLN) \
{{- "\n\t" }}&& make install \
{%- endif -%}
{{- "\n\t" }}# Enabling
{{- "\n\t" }}&& docker-php-ext-enable {{ ext }} \
{#-
# -------------------- 4.) CUSTOM --------------------
-#}
{%- elif extensions_available[ext][php_version]['type'] == 'custom' -%}
{{- "\n\t" }}# Type: Custom extension
{{- "\n\t" }}&& {{ extensions_available[ext][php_version]['command'] }} \
{%- endif -%}
{#-
# --------------------------------------------------------------------------------
# INSTALLATION (GENERIC)
# --------------------------------------------------------------------------------
#}
{%- elif 'all' in extensions_available[ext] and 'type' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Installation: Generic
{#-
# -------------------- 1.) Builtin --------------------
-#}
{%- if extensions_available[ext]['all']['type'] == 'builtin' -%}
{{- "\n\t" }}# Type: Built-in extension
{%- if 'configure' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Custom: configure command
{{- "\n\t" }}&& docker-php-ext-configure {{ ext }} {{ extensions_available[ext]['all']['configure'] }} \
{%- endif -%}
{{- "\n\t" }}&& docker-php-ext-install -j$(getconf _NPROCESSORS_ONLN) {{ ext }} \
{#-
# -------------------- 2.) PECL --------------------
-#}
{%- elif extensions_available[ext]['all']['type'] == 'pecl' -%}
{{- "\n\t" }}# Type: PECL extension
{%- if 'command' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Custom: Pecl command
{{- "\n\t" }}&& {{ extensions_available[ext]['all']['command'] }} \
{%- else -%}
{{- "\n\t" }}# Default: Pecl command
{{- "\n\t" }}&& pecl install {{ ext }}{% if 'version' in extensions_available[ext]['all'] %}-{{ extensions_available[ext]['all']['version'] }}{% endif %} \
{%- endif -%}
{{- "\n\t" }}# Enabling
{{- "\n\t" }}&& docker-php-ext-enable {{ ext }} \
{#-
# -------------------- 2.) GIT --------------------
-#}
{%- elif extensions_available[ext]['all']['type'] == 'git' -%}
{{- "\n\t" }}# Type: GIT extension
{{- "\n\t" }}&& git clone {{ extensions_available[ext]['all']['git_url'] }} /tmp/{{ ext }} \
{{- "\n\t" }}&& cd /tmp/{{ ext }} \
{%- if 'git_ref' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Custom: Branch
{{- "\n\t" }}&& git checkout {{ extensions_available[ext]['all']['git_ref'] }} \
{%- endif -%}
{%- if 'command' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Custom: Install command
{{- "\n\t" }}&& {{ extensions_available[ext]['all']['command'] }} \
{%- else -%}
{{- "\n\t" }}# Default: Install command
{{- "\n\t" }}&& phpize \
{{- "\n\t" }}&& ./configure {% if 'configure' in extensions_available[ext]['all'] %} {{ extensions_available[ext]['all']['configure'] }}{% endif %} \
{{- "\n\t" }}&& make -j$(getconf _NPROCESSORS_ONLN) \
{{- "\n\t" }}&& make install \
{%- endif -%}
{{- "\n\t" }}# Enabling
{{- "\n\t" }}&& docker-php-ext-enable {{ ext }} \
{#-
# -------------------- 4.) CUSTOM --------------------
-#}
{%- elif extensions_available[ext]['all']['type'] == 'custom' -%}
{{- "\n\t" }}# Type: Custom extension
{{- "\n\t" }}&& {{ extensions_available[ext]['all']['command'] }} \
{%- endif -%}
{%- endif -%}
{#-
# --------------------------------------------------------------------------------
# POST COMMAND
# --------------------------------------------------------------------------------
-#}
{#- Version specific post-command available? -#}
{%- if php_version in extensions_available[ext] and 'post' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Version specific post-command
{{- "\n\t" }}&& {{ extensions_available[ext][php_version]['post'] }} \
{#- Generic post-command available? -#}
{%- elif 'all' in extensions_available[ext] and 'post' in extensions_available[ext]['all'] %}
{{- "\n\t" }}# Generic post-command
{{- "\n\t" }}&& {{ extensions_available[ext]['all']['post'] }} \
{%- endif -%}
{#- End of: Not disabled AND not already enabled -#}
{%- endif -%}
{#-
###############################################################################################
# Ensure modules that are not disabled have an appropriate ini directive and are loaded
###############################################################################################
-#}
{%- if ('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled']) -%}
{%- if php_version in extensions_available[ext] and 'load_ext' in extensions_available[ext][php_version] -%}
{{- "\n\t" }}# Activate: Version specific extension
{{- "\n\t" }}&& if [ ! -f "/usr/local/etc/php/conf.d/docker-php-ext-{{ ext }}.ini" ]; then \
{{- "\n\t" }} echo "extension={{ extensions_available[ext][php_version]['load_ext'] }}" > "/usr/local/etc/php/conf.d/docker-php-ext-{{ ext }}.ini"; \
{{- "\n\t" }}fi \
{%- elif 'all' in extensions_available[ext] and 'load_ext' in extensions_available[ext]['all'] -%}
{{- "\n\t" }}# Activate: Generic extension
{{- "\n\t" }}&& if [ ! -f "/usr/local/etc/php/conf.d/docker-php-ext-{{ ext }}.ini" ]; then \
{{- "\n\t" }} echo "extenstion={{ extensions_available[ext]['all']['load_ext'] }}" > "/usr/local/etc/php/conf.d/docker-php-ext-{{ ext }}.ini"; \
{{- "\n\t" }}fi \
{%- endif -%}
{%- endif %}
{#-
###############################################################################################
# Test extensions
###############################################################################################
-#}
{#- Not disabled #}
{%- if (('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled'])) and ('already_avail' not in extensions_available[ext] or php_version not in extensions_available[ext]['already_avail']) %}
{%- if build_fail_fast -%}
{{- "\n\t" }}# ---- Test extension ----
{{- "\n\t" }}&& /usr/local/sbin/php-fpm --test \
{{- "\n\t" }}\
{{- "\n\t" }}&& (php -v 2>&1 1>/dev/null || true) \
{{- "\n\t" }}&& PHP_ERROR="$( php -v 2>&1 1>/dev/null )" \
{{- "\n\t" }}&& if [ -n "${PHP_ERROR}" ]; then echo "${PHP_ERROR}"; false; fi \
{{- "\n\t" }}&& PHP_ERROR="$( php -i 2>&1 1>/dev/null )" \
{{- "\n\t" }}&& if [ -n "${PHP_ERROR}" ]; then echo "${PHP_ERROR}"; false; fi \
{{- "\n\t" }}\
{{- "\n\t" }}&& (php-fpm -v 2>&1 1>/dev/null || true) \
{{- "\n\t" }}&& PHP_FPM_ERROR="$( php-fpm -v 2>&1 1>/dev/null )" \
{{- "\n\t" }}&& if [ -n "${PHP_FPM_ERROR}" ]; then echo "${PHP_FPM_ERROR}"; false; fi \
{{- "\n\t" }}&& PHP_FPM_ERROR="$( php-fpm -i 2>&1 1>/dev/null )" \
{{- "\n\t" }}&& if [ -n "${PHP_FPM_ERROR}" ]; then echo "${PHP_FPM_ERROR}"; false; fi \
{{- "\n\t" }}\
{%- if ext == 'opcache' -%}
{{- "\n\t" }}&& php -m | grep -oiE '^Zend Opcache$' \
{{- "\n\t" }}&& php-fpm -m | grep -oiE '^Zend Opcache$'
{%- elif ext == 'readline' -%}
{{- "\n\t" }}&& php -m | grep -oiE '^readline$'
{%- elif ext not in ['blackfire', 'ioncube'] -%}
{{- "\n\t" }}&& php -m | grep -oiE '^{{ ext }}$' \
{{- "\n\t" }}&& php-fpm -m | grep -oiE '^{{ ext }}$'
{%- else %}
{{- "\n\t" }}&& true
{%- endif %}
{{- "\n\n\n" }}
{%- else -%}
{{- "\n\t" }}&& true{{ "\n\n\n" }}
{%- endif %}
{%- endif %}
{%- endfor %}
# Fix php.ini settings for enabled extensions
RUN set -eux \
&& chmod +x "$(php -r 'echo ini_get("extension_dir");')"/*
# Fix oracle dir for images that don't have oci installed
RUN set -eux \
&& mkdir -p /usr/lib/oracle/
# Shrink everything down
RUN set -eux \
&& (find /usr/local/bin -type f -print0 | xargs -n1 -0 -P$(getconf _NPROCESSORS_ONLN) strip --strip-all -p 2>/dev/null || true) \
&& (find /usr/local/lib -type f -print0 | xargs -n1 -0 -P$(getconf _NPROCESSORS_ONLN) strip --strip-all -p 2>/dev/null || true) \
&& (find /usr/local/sbin -type f -print0 | xargs -n1 -0 -P$(getconf _NPROCESSORS_ONLN) strip --strip-all -p 2>/dev/null || true)
{{ edit_comment_mods }}
FROM devilbox/php-fpm:{{ php_version }}-base as final
MAINTAINER "cytopia" <cytopia@everythingcli.org>
###
### Labels
###
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
#LABEL "org.opencontainers.image.created"=""
#LABEL "org.opencontainers.image.version"=""
#LABEL "org.opencontainers.image.revision"=""
LABEL "maintainer"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.authors"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.url"="https://github.com/devilbox/docker-php-fpm"
LABEL "org.opencontainers.image.documentation"="https://github.com/devilbox/docker-php-fpm"
LABEL "org.opencontainers.image.source"="https://github.com/devilbox/docker-php-fpm"
LABEL "org.opencontainers.image.vendor"="devilbox"
LABEL "org.opencontainers.image.licenses"="MIT"
LABEL "org.opencontainers.image.ref.name"="{{ php_version }}-mods"
LABEL "org.opencontainers.image.title"="PHP-FPM {{ php_version }}-mods"
LABEL "org.opencontainers.image.description"="PHP-FPM {{ php_version }}-mods"
###
### Install runtime libraries
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{# Loop over enabled run-time dependencies -#}
{%- set run_deps = [] -%}
{%- for ext in extensions_enabled -%}
{#- Not disabled -#}
{%- if ('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled']) -%}
{#- Version specific run-time dependency available? -#}
{%- if php_version in extensions_available[ext] and 'run_dep' in extensions_available[ext][php_version] -%}
{%- for run_dep in extensions_available[ext][php_version]['run_dep'] -%}
{#- Append run dependencies to our array -#}
{{- run_deps.append(run_dep) -}}
{%- endfor -%}
{#- Generic run-time dependency available? -#}
{%- elif 'all' in extensions_available[ext] and 'run_dep' in extensions_available[ext]['all'] -%}
{%- for run_dep in extensions_available[ext]['all']['run_dep'] -%}
{#- Append run dependencies to our array -#}
{{- run_deps.append(run_dep) -}}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% for run_dep in run_deps | unique | sort %}
{{ run_dep }} \
{% endfor %}
ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
\
&& update-ca-certificates
###
### Copy artifacts from builder
###
ARG EXT_DIR
COPY --from=builder ${EXT_DIR}/ ${EXT_DIR}/
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
COPY --from=builder /usr/local/bin/ /usr/local/bin/
COPY --from=builder /usr/local/lib/ /usr/local/lib/
COPY --from=builder /usr/lib/oracle/ /usr/lib/oracle/
###
### Post Install executions
###
{# Loop over enabled extensions #}
{%- set post_commands = {} -%}
{% for ext in extensions_enabled %}
{%- if (('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled'])) and ('already_avail' not in extensions_available[ext] or php_version not in extensions_available[ext]['already_avail']) %}
{#- Version specific post-command available? -#}
{%- if php_version in extensions_available[ext] and 'post' in extensions_available[ext][php_version] -%}
{{- post_commands.update({ext: extensions_available[ext][php_version]['post']}) -}}
{#- Generic post-command available? -#}
{%- elif 'all' in extensions_available[ext] and 'post' in extensions_available[ext]['all'] %}
{{- post_commands.update({ext: extensions_available[ext]['all']['post']}) -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{% if post_commands | length > 0 -%}
RUN set -eux \
{%- for ext in post_commands -%}
{{- "\n\t" }}# ---------- {{ ext }} ----------
{{- "\n\t" }}&& {{ post_commands[ext] }} \
{%- endfor -%}
{{- "\n\t" }}&& true{{- "\n\n\n" }}
{%- endif -%}
{% if debug %}
###
### Verify
###
RUN set -eux \
&& echo "date.timezone=UTC" > /usr/local/etc/php/php.ini \
&& php -v | grep -oE 'PHP\s[.0-9]+' | grep -oE '[.0-9]+' | grep '^{{ php_version }}' \
&& /usr/local/sbin/php-fpm --test \
\
&& PHP_ERROR="$( php -v 2>&1 1>/dev/null )" \
&& if [ -n "${PHP_ERROR}" ]; then echo "${PHP_ERROR}"; false; fi \
&& PHP_ERROR="$( php -i 2>&1 1>/dev/null )" \
&& if [ -n "${PHP_ERROR}" ]; then echo "${PHP_ERROR}"; false; fi \
\
&& PHP_FPM_ERROR="$( php-fpm -v 2>&1 1>/dev/null )" \
&& if [ -n "${PHP_FPM_ERROR}" ]; then echo "${PHP_FPM_ERROR}"; false; fi \
&& PHP_FPM_ERROR="$( php-fpm -i 2>&1 1>/dev/null )" \
&& if [ -n "${PHP_FPM_ERROR}" ]; then echo "${PHP_FPM_ERROR}"; false; fi \
&& rm -f /usr/local/etc/php/php.ini \
\
{% for ext in extensions_enabled %}
{#- Not disabled #}
{%- if ('disabled' not in extensions_available[ext]) or (php_version not in extensions_available[ext]['disabled']) %}
{%- if ext == 'opcache' %}
&& php -m | grep -oiE '^Zend Opcache$' \
&& php-fpm -m | grep -oiE '^Zend Opcache$' \
{# #}
{%- elif ext == 'readline' %}
&& php -m | grep -oiE '^readline$' \
{# #}
{%- elif ext not in ['blackfire', 'ioncube'] %}
&& php -m | grep -oiE '^{{ ext }}$' \
&& php-fpm -m | grep -oiE '^{{ ext }}$' \
{# #}
{%- endif %}
{%- endif %}
{% endfor %}
&& true
{% endif %}
# Deactive PSR and Phalcon:
# https://github.com/devilbox/docker-php-fpm/issues/201
RUN set -eux \
&& rm -f /usr/local/etc/php/conf.d/docker-php-ext-phalcon.ini || true \
&& rm -f /usr/local/etc/php/conf.d/docker-php-ext-psr.ini || true \
###
### Ports
###
EXPOSE 9000
###
### Entrypoint
###
CMD ["/usr/local/sbin/php-fpm"]
ENTRYPOINT ["/docker-entrypoint.sh"]