Files
docker-php-fpm/.ansible/DOCKERFILES/Dockerfile-work.j2
2022-12-09 11:12:08 +01:00

567 lines
21 KiB
Django/Jinja

# vi: ft=dockerfile
{{ edit_comment_work }}
{% import './jinja2/macros-work.j2' as fn %}
########################################################################################################################
########################################################################################################################
###
### Stage 1/7: Devilbox work image (HELP BUILDER)
###
########################################################################################################################
########################################################################################################################
###
### Installs all cli tools required to install custom tools
###
FROM devilbox/php-fpm:{{ php_version }}-slim as devilbox-work-help-builder
{% set build_deps = [] -%}
{%- for ext in help_software_enabled -%}
{%- for dep in fn.get_build_dep(php_version, ext, help_software_available) | from_json -%}
{%- if dep -%}
{{- build_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- if build_deps -%}
###
### Install build_dep
###
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 %}
&& rm -rf /var/lib/apt/lists/*
{%- endif %}
{% set run_deps = [] -%}
{%- for ext in help_software_enabled -%}
{%- for dep in fn.get_run_dep(php_version, ext, help_software_available) | from_json -%}
{%- if dep -%}
{{- run_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- if run_deps -%}
###
### Install run_dep
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{% for run_dep in run_deps | unique | sort %}
{{ run_dep }} \
{% endfor %}
&& rm -rf /var/lib/apt/lists/*
{%- endif %}
###
### Devilbox required helper tools from group_vars (work-help.yml)
###
{% for tool in help_software_enabled -%}
{# Not disabled #}
{%- if ('disabled' not in help_software_available[tool]) or (php_version not in help_software_available[tool]['disabled']) -%}
{{- "\n" }}# -------------------- {{ tool }} --------------------
{{- "\n" }}RUN set -eux \
{#- -#}
{#- [PRE] -#}
{%- if fn.get_pre(php_version, tool, help_software_available) | length -%}
{{ "\n\t" }}&& {{ fn.get_pre(php_version, tool, help_software_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- [COMMAND] -#}
{%- if fn.get_type(php_version, tool, help_software_available) == 'custom' -%}
{{ "\n\t" }}&& {{ fn.tool_custom_get_command(php_version, tool, help_software_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- [POST] -#}
{%- if fn.get_post(php_version, tool, help_software_available) | length -%}
{{ "\n\t" }}&& {{ fn.get_post(php_version, tool, help_software_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- Finalize -#}
{{ "\n\t" }}&& true{{ "\n" }}
{%- endif -%}
{%- endfor %}
###
### Configure Bash (composer, node yarn)
###
RUN set -eux \
&& { \
echo 'PATH="${PATH}:/usr/local/bin:/usr/local/sbin"'; \
echo 'PATH="${HOME}/.yarn/bin:${PATH}"'; \
echo 'PATH="${HOME}/.composer/vendor/bin:${PATH}"'; \
echo 'PATH="{{ nvm_home }}/versions/node/$(nvm version default)/bin:${PATH}"'; \
echo "export PATH"; \
echo ". /etc/bash-devilbox"; \
echo "if [ -d /etc/bashrc-devilbox.d/ ]; then"; \
echo " for f in /etc/bashrc-devilbox.d/*.sh ; do"; \
echo " if [ -r \"\${f}\" ]; then"; \
echo " . \"\${f}\""; \
echo " fi"; \
echo " done"; \
echo " unset f"; \
echo "fi"; \
} | tee -a /home/${MY_USER}/.bashrc /root/.bashrc \
&& chown ${MY_USER}:${MY_GROUP} /home/${MY_USER}/.bashrc
########################################################################################################################
########################################################################################################################
###
### Stage 2/7: Devilbox work image (HELP)
###
########################################################################################################################
########################################################################################################################
###
### Copies all cli tools required to run Devilbox and its intranet into a clean image
###
FROM devilbox/php-fpm:{{ php_version }}-slim as devilbox-work-help
{% set run_deps = [] -%}
{%- for ext in help_software_enabled -%}
{%- for dep in fn.get_run_dep(php_version, ext, help_software_available) | from_json -%}
{%- if dep -%}
{{- run_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- if run_deps -%}
###
### Install run_dep
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{% for run_dep in run_deps | unique | sort %}
{{ run_dep }} \
{% endfor %}
&& rm -rf /var/lib/apt/lists/*
{% endif %}
###
### Copy nvm
###
COPY --from=devilbox-work-help-builder /opt/nvm /opt/nvm
###
### Copy binaries (Composer & Pip binaries)
###
COPY --from=devilbox-work-help-builder /usr/local/bin /usr/local/bin
###
### Python packages
###
COPY --from=devilbox-work-help-builder /usr/local/lib /usr/local/lib
########################################################################################################################
########################################################################################################################
###
### Stage 3/7: Devilbox work image (HELP TEST)
###
########################################################################################################################
########################################################################################################################
###
### Test all Devilbox cli utils if copying was successful
###
FROM devilbox-work-help as devilbox-work-help-test
###
### Devilbox required helper tools from group_vars (work-help.yml)
###
{% for tool in help_software_enabled -%}
{# Not disabled #}
{%- if ('disabled' not in help_software_available[tool]) or (php_version not in help_software_available[tool]['disabled']) -%}
{{- "\n" }}# -------------------- {{ tool }} --------------------
{{- "\n" }}RUN set -eux \
{#- -#}
{#- [CHECK] -#}
{%- if 'check' in help_software_available[tool] -%}
{{ "\n\t" }}&& {{ help_software_available[tool]['check'] | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- Finalize -#}
{{ "\n\t" }}&& true{{ "\n" }}
{%- endif -%}
{%- endfor %}
###
### Re-activate modules which have been deactivated in mods (for testing).
###
RUN set -eux \
&& if find /usr/local/lib/php/extensions/ -name phalcon.so | grep phalcon; then \
echo "extension=phalcon.so" > /usr/local/etc/php/conf.d/docker-php-ext-phalcon.ini; \
fi \
&& if find /usr/local/lib/php/extensions/ -name psr.so | grep psr; then \
echo "extension=psr.so" > /usr/local/etc/php/conf.d/docker-php-ext-psr.ini; \
fi
###
### Check if PHP still works
###
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
########################################################################################################################
########################################################################################################################
###
### Stage 4/7: Devilbox work image (TOOLS BUILDER)
###
########################################################################################################################
########################################################################################################################
###
### This stage can be easily modified by adding tools into the ./php_tools/ directory.
###
FROM devilbox-work-help as devilbox-work-tools-builder
{% set build_deps = [] -%}
{%- set run_deps = [] -%}
{%- set tools_apt = [] -%}
{%- for tool in tools_enabled -%}
{%- for dep in fn.get_build_dep(php_version, tool, tools_available) | from_json -%}
{%- if dep -%}
{{- build_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- for dep in fn.get_run_dep(php_version, tool, tools_available) | from_json -%}
{%- if dep -%}
{{- run_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- if ('disabled' not in tools_available[tool]) or (php_version not in tools_available[tool]['disabled']) -%}
{%- if fn.tool_apt_get_package(php_version, tool, tools_available) -%}
{{- tools_apt.append(fn.tool_apt_get_package(php_version, tool, tools_available)) -}}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- if build_deps or run_deps or tools_apt -%}
###
### Install build_dep, run_dep or tools type: apt
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{{- "\n" -}}
{%- if build_deps -%}
{{- "\t\t# ---------- build_deps ----------\n" -}}
{%- for apt in build_deps | unique | sort -%}
{{- "\t\t" + apt + " \\" + "\n" -}}
{%- endfor -%}
{%- endif -%}
{%- if run_deps -%}
{{- "\t\t# ---------- run_deps ----------\n" -}}
{%- for apt in run_deps | unique | sort -%}
{{- "\t\t" + apt + " \\" + "\n" -}}
{%- endfor -%}
{%- endif -%}
{%- if tools_apt -%}
{{- "\t\t# ---------- type: apt ----------\n" -}}
{%- for apt in tools_apt | unique | sort -%}
{{- "\t\t" + apt + " \\" + "\n" -}}
{%- endfor -%}
{%- endif -%}
{{- "\t" -}}&& rm -rf /var/lib/apt/lists/*{{- "\n" -}}
{% endif %}
###
### Install defined php_tools
###
{% for tool in tools_enabled -%}
{# Not disabled #}
{%- if ('disabled' not in tools_available[tool]) or (php_version not in tools_available[tool]['disabled']) -%}
{{- "\n" }}# -------------------- ({{ fn.get_type(php_version, tool, tools_available) }}) {{ tool }} --------------------
{{- "\n" }}RUN set -eux \
{#- -#}
{#- [PRE] -#}
{%- if fn.get_pre(php_version, tool, tools_available) | length -%}
{{ "\n\t" }}&& {{ fn.get_pre(php_version, tool, tools_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- [CUSTOM] -#}
{%- if fn.get_type(php_version, tool, tools_available) == 'custom' -%}
{{ "\n\t" }}&& {{ fn.tool_custom_get_command(php_version, tool, tools_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- [NPM] -#}
{%- if fn.get_type(php_version, tool, tools_available) == 'npm' -%}
{%- set npm_package = fn.tool_npm_get_package(php_version, tool, tools_available) -%}
{%- set npm_version = fn.tool_npm_get_version(php_version, tool, tools_available) -%}
{%- set npm_binary = fn.tool_npm_get_binary(php_version, tool, tools_available) -%}
{{ "\n\t" }}&& su -c '. {{ nvm_home }}/nvm.sh; npm install --location=global {{ npm_package }}{{ npm_version }}' devilbox \
{%- if npm_binary | length -%}
{{ "\n\t" }}&& ln -s {{ nvm_home }}/versions/node/$(su -c '. {{ nvm_home }}/nvm.sh; node --version' devilbox)/bin/{{ npm_binary }} /usr/local/bin/ \
{%- endif -%}
{%- endif -%}
{#- -#}
{#- [COMPOSER] -#}
{%- if fn.get_type(php_version, tool, tools_available) == 'composer' -%}
{%- set comp_composer = fn.tool_composer_get_composer(php_version, tool, tools_available) -%}
{%- set comp_package = fn.tool_composer_get_package(php_version, tool, tools_available) -%}
{%- set comp_version = fn.tool_composer_get_version(php_version, tool, tools_available) -%}
{%- set comp_flags = fn.tool_composer_get_flags(php_version, tool, tools_available) -%}
{%- set comp_binary = fn.tool_composer_get_binary(php_version, tool, tools_available) -%}
{#- FIXME: use su -c ' devilbox -#}
{{ "\n\t" }}&& COMPOSER_HOME="{{ composer_home }}" COMPOSER_MEMORY_LIMIT=-1 /usr/local/bin/composer-{{ comp_composer }} global require --no-interaction --no-cache {{ comp_flags }} {{ comp_package }}{{ comp_version }} \
{%- if comp_binary | length -%}
{{ "\n\t" }}&& ln -s {{ composer_home }}/vendor/{{ comp_package }}/{{ comp_binary }} /usr/local/bin/ \
{%- endif -%}
{{ "\n\t" }}&& rm -rf {{ composer_home }}/cache/* \
{%- endif -%}
{#- -#}
{#- [RUBYGEM] -#}
{%- if fn.get_type(php_version, tool, tools_available) == 'rubygem' -%}
{%- set rubygem_package = fn.tool_rubygem_get_package(php_version, tool, tools_available) -%}
{%- set rubygem_version = fn.tool_rubygem_get_version(php_version, tool, tools_available) -%}
{{ "\n\t" }}&& gem install {{ rubygem_package }}{{ rubygem_version }} \
{%- endif -%}
{#- -#}
{#- [PIP] -#}
{%- if fn.get_type(php_version, tool, tools_available) == 'pip' -%}
{%- set pip_package = fn.tool_pip_get_package(php_version, tool, tools_available) -%}
{%- set pip_version = fn.tool_pip_get_version(php_version, tool, tools_available) -%}
{{ "\n\t" }}&& pip install --no-cache-dir --no-compile --force-reinstall {{ pip_package }}{{ pip_version }} \
{%- endif -%}
{#- -#}
{#- [POST] -#}
{%- if fn.get_post(php_version, tool, tools_available) | length -%}
{{ "\n\t" }}&& {{ fn.get_post(php_version, tool, tools_available) | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- [CHECK] -#}
{%- if 'check' in tools_available[tool] -%}
{{ "\n\t" }}&& {{ tools_available[tool]['check'] | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{#- -#}
{#- Finalize -#}
{{ "\n\t" }}&& true{{ "\n" }}
{%- endif -%}
{%- endfor %}
###
### Post fixes
###
RUN set -eux \
&& if [ ! -d "/var/lib/gems" ]; then \
mkdir "/var/lib/gems"; \
fi
########################################################################################################################
########################################################################################################################
###
### Stage 5/7: Devilbox work image (TOOLS)
###
########################################################################################################################
########################################################################################################################
###
### This stage can be easily modified by adding tools into the ./php_tools/ directory.
###
FROM devilbox-work-help as devilbox-work-tools
{% set run_deps = [] -%}
{%- set tools_apt = [] -%}
{%- for tool in tools_enabled -%}
{%- for dep in fn.get_run_dep(php_version, tool, tools_available) | from_json -%}
{%- if dep -%}
{{- run_deps.append(dep) -}}
{%- endif -%}
{%- endfor -%}
{%- if ('disabled' not in tools_available[tool]) or (php_version not in tools_available[tool]['disabled']) -%}
{%- if fn.tool_apt_get_package(php_version, tool, tools_available) -%}
{{- tools_apt.append(fn.tool_apt_get_package(php_version, tool, tools_available)) -}}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- if build_deps or run_deps or tools_apt -%}
###
### Install run_dep or tools type: apt
###
RUN set -eux \
&& DEBIAN_FRONTEND=noninteractive apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
{{- "\n" -}}
{%- if run_deps -%}
{{- "\t\t# ---------- run_deps ----------\n" -}}
{%- for apt in run_deps | unique | sort -%}
{{- "\t\t" + apt + " \\" + "\n" -}}
{%- endfor -%}
{%- endif -%}
{%- if tools_apt -%}
{{- "\t\t# ---------- type: apt ----------\n" -}}
{%- for apt in tools_apt | unique | sort -%}
{{- "\t\t" + apt + " \\" + "\n" -}}
{%- endfor -%}
{%- endif -%}
{{- "\t" -}}&& rm -rf /var/lib/apt/lists/*{{- "\n" -}}
{% endif %}
###
### Copy Custom (binaries in /usr/local/bin)
###
COPY --from=devilbox-work-tools-builder /usr/local/bin /usr/local/bin
###
### Copy NPM
###
COPY --from=devilbox-work-tools-builder /opt/nvm /opt/nvm
###
### Copy Composer
###
COPY --from=devilbox-work-tools-builder /usr/local/src /usr/local/src
###
### Copy Pip
###
COPY --from=devilbox-work-tools-builder /usr/local/lib /usr/local/lib
###
### Copy RubyGems
###
COPY --from=devilbox-work-tools-builder /var/lib/gems /var/lib/gems
# FIXME: / TODO: Add post: stage into here as well
########################################################################################################################
########################################################################################################################
###
### Stage 6/7: Devilbox work image (TOOLS TEST)
###
########################################################################################################################
########################################################################################################################
###
### Test all Devilbox cli utils if copying was successful
###
FROM devilbox-work-tools as devilbox-work-tools-test
{% for tool in tools_enabled -%}
{%- if fn.is_not_disabled(php_version, tool, tools_available) -%}
{{- "\n" }}# -------------------- ({{ fn.get_type(php_version, tool, tools_available) }}) {{ tool }} --------------------
{{- "\n" }}RUN set -eux \
{%- if 'check' in tools_available[tool] -%}
{{ "\n\t" }}&& {{ tools_available[tool]['check'] | indent( "\t" ) }}{{ "\t\\" }}
{%- endif -%}
{{ "\n\t" }}&& true{{ "\n" }}
{%- endif -%}
{%- endfor %}
###
### Re-activate modules which have been deactivated in mods (for testing).
###
RUN set -eux \
&& if find /usr/local/lib/php/extensions/ -name phalcon.so | grep phalcon; then \
echo "extension=phalcon.so" > /usr/local/etc/php/conf.d/docker-php-ext-phalcon.ini; \
fi \
&& if find /usr/local/lib/php/extensions/ -name psr.so | grep psr; then \
echo "extension=psr.so" > /usr/local/etc/php/conf.d/docker-php-ext-psr.ini; \
fi
###
### Check if PHP still works
###
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
########################################################################################################################
########################################################################################################################
###
### Stage 7/7: Devilbox sliwork image (FINAL)
###
########################################################################################################################
########################################################################################################################
###
### Prepare final base image (STAGE: work)
###
FROM devilbox-work-tools as work
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 }}-work"
LABEL "org.opencontainers.image.title"="PHP-FPM {{ php_version }}-work"
LABEL "org.opencontainers.image.description"="PHP-FPM {{ php_version }}-work"
###
### Volumes
###
VOLUME /shared/backups
VOLUME /var/log/php
VOLUME /var/mail
###
### Ports
###
EXPOSE 9000
###
### Where to start inside the container
###
WORKDIR /shared/httpd
###
### Entrypoint
###
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
ENTRYPOINT ["/docker-entrypoint.sh"]