Add Docker setup for PHP application

- Created a Dockerfile to set up a PHP 8.3 environment with Apache.
- Configured document root and timezone settings.
- Installed Composer and necessary PHP extensions.
- Added custom entrypoint and foreground scripts for Apache.
- Set up environment variable handling in the entrypoint script.
- Included commands for cache clearing and warming during startup.
This commit is contained in:
Jeroen De Meerleer 2025-01-20 17:28:43 +01:00
parent e733bfe123
commit f8f3b55382
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 200 additions and 0 deletions

39
Dockerfile Normal file
View File

@ -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"]

View File

@ -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 <apache args>", 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 "$@"

View File

@ -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