diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..58dada9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +FROM php:8.3-apache AS runtime + +ENV APACHE_DOCUMENT_ROOT=/var/www/website/public/ +ENV WEBSITE_ROOT=/var/www/website/ +ENV TZ=Europe/Brussels +ENV COMPOSER_ALLOW_SUPERUSER=1 +ENV DEBIAN_FRONTEND=noninteractive + +COPY . ${APACHE_DOCUMENT_ROOT} + +RUN sed -ri -e "s!/var/www/html!${APACHE_DOCUMENT_ROOT}!g" /etc/apache2/sites-available/*.conf +RUN sed -ri -e "s!/var/www/!${APACHE_DOCUMENT_ROOT}!g" /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf +RUN a2enmod rewrite + +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin +RUN set -eux; \ + chmod uga+x /usr/local/bin/install-php-extensions && sync + +RUN set -eux;install-php-extensions intl zip + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" +RUN printf "[PHP]\ndate.timezone = \"$TZ\"\n" > /usr/local/etc/php/conf.d/tzone.ini + +COPY docker-resources/apache2-foreground /usr/local/bin/apache2-foreground +COPY docker-resources/entrypoint.sh /usr/local/bin/entrypoint.sh + +RUN set -eux; \ + chmod uga+x /usr/local/bin/entrypoint.sh && \ + chmod uga+x /usr/local/bin/apache2-foreground && sync + +USER www-data +WORKDIR ${WEBSITE_ROOT} +RUN mv .env.sample .env +RUN composer install +RUN php bin/console asset-map:compile + +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] diff --git a/docker-resources/apache2-foreground b/docker-resources/apache2-foreground new file mode 100644 index 0000000..5fe22e2 --- /dev/null +++ b/docker-resources/apache2-foreground @@ -0,0 +1,40 @@ +#!/bin/bash +set -e + +# Note: we don't just use "apache2ctl" here because it itself is just a shell-script wrapper around apache2 which provides extra functionality like "apache2ctl start" for launching apache2 in the background. +# (also, when run as "apache2ctl ", it does not use "exec", which leaves an undesirable resident shell process) + +: "${APACHE_CONFDIR:=/etc/apache2}" +: "${APACHE_ENVVARS:=$APACHE_CONFDIR/envvars}" +if test -f "$APACHE_ENVVARS"; then + . "$APACHE_ENVVARS" +fi + +# Apache gets grumpy about PID files pre-existing +: "${APACHE_RUN_DIR:=/var/run/apache2}" +: "${APACHE_PID_FILE:=$APACHE_RUN_DIR/apache2.pid}" +rm -f "$APACHE_PID_FILE" + +# create missing directories +# (especially APACHE_RUN_DIR, APACHE_LOCK_DIR, and APACHE_LOG_DIR) +for e in "${!APACHE_@}"; do + if [[ "$e" == *_DIR ]] && [[ "${!e}" == /* ]]; then + # handle "/var/lock" being a symlink to "/run/lock", but "/run/lock" not existing beforehand, so "/var/lock/something" fails to mkdir + # mkdir: cannot create directory '/var/lock': File exists + dir="${!e}" + while [ "$dir" != "$(dirname "$dir")" ]; do + dir="$(dirname "$dir")" + if [ -d "$dir" ]; then + break + fi + absDir="$(readlink -f "$dir" 2>/dev/null || :)" + if [ -n "$absDir" ]; then + mkdir -p "$absDir" + fi + done + + mkdir -p "${!e}" + fi +done + +exec apache2 -DFOREGROUND "$@" diff --git a/docker-resources/entrypoint.sh b/docker-resources/entrypoint.sh new file mode 100644 index 0000000..f5e328d --- /dev/null +++ b/docker-resources/entrypoint.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +echo "Now in entrypoint.sh for Website" +echo "Script: 1.0.21 (2022-02-17)" +echo "User: '$(whoami)'" +echo "Group: '$(id -g -n)'" +echo "Working dir: '$(pwd)'" + +# https://github.com/docker-library/wordpress/blob/master/docker-entrypoint.sh +# usage: file_env VAR [DEFAULT] +# ie: file_env 'XYZ_DB_PASSWORD' 'example' +# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of +# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) +file_env() { + local var="$1" + local fileVar="${var}_FILE" + local def="${2:-}" + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then + echo >&2 "error: both $var and $fileVar are set (but are exclusive)" + exit 1 + fi + local val="$def" + if [ "${!var:-}" ]; then + val="${!var}" + elif [ "${!fileVar:-}" ]; then + val="$(< "${!fileVar}")" + fi + export "$var"="$val" + unset "$fileVar" +} + +# envs that can be appended with _FILE +envs=( + APP_ENV + APP_SECRET + DATADIR + TRUSTED_PROXIES +) + +echo "Now parsing _FILE variables." +for e in "${envs[@]}"; do + file_env "$e" +done +echo "done!" + + +echo "Dump envs to .env..." +rm -rf .env +touch .env +for e in "${envs[@]}"; do + echo "$e=\"${!e}\"" >> .env +done +echo "Dump auto load..." +composer dump-autoload + +echo "Current working dir is '$(pwd)'" + + +if [[ $DKR_BUILD_LOCALE == "true" ]]; then + echo "Will build all locales..." + locale-gen +else + echo "Will not build the locales..." +fi + + +echo "Current working dir is '$(pwd)'" + +# there are 0 upgrade commands +if [[ $DKR_RUN_UPGRADE == "false" ]]; then + echo 'Will NOT run upgrade commands.' +else + echo 'Running upgrade commands...' +fi + +# there are 0 verify commands +if [[ $DKR_RUN_VERIFY == "false" ]]; then + echo 'Will NOT run verification commands.' +else + echo 'Running verification commands...' +fi + +# report commands +if [[ $DKR_RUN_REPORT == "false" ]]; then + echo 'Will NOT run report commands.' +else + echo 'Running report commands...' +fi + +# set docker var. +export IS_DOCKER=true + +if [ -z $APACHE_RUN_USER ] +then + APACHE_RUN_USER='www-data' +fi + +if [ -z $APACHE_RUN_GROUP ] +then + APACHE_RUN_GROUP='www-data' +fi + +if [[ $(stat -c '%U' $WEBSITE_ROOT) != $APACHE_RUN_USER ]]; +then + chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $WEBSITE_ROOT +fi + +# refresh commands +if [[ $DKR_RUN_REFRESH == "false" ]]; then + echo 'Will NOT run refresh commands.' +else + echo 'Running refresh commands...' + php $WEBSITE_ROOT/bin/console refresh +fi + +php $WEBSITE_ROOT/bin/console cache:clear +php $WEBSITE_ROOT/bin/console cache:warm + +echo "Go!" + +exec apache2-foreground