moved dockerfile to main repo (#4393)

This commit is contained in:
Toby Batch
2023-11-17 12:07:51 +00:00
committed by GitHub
parent 6a828cc9bd
commit 350875be2a
9 changed files with 705 additions and 0 deletions

22
.docker/000-default.conf Normal file
View File

@@ -0,0 +1,22 @@
<VirtualHost *:8001>
ServerAdmin webmaster@localhost
DocumentRoot /opt/kimai/public
PassEnv DATABASE_PREFIX
PassEnv MAILER_FROM
PassEnv APP_ENV
PassEnv APP_SECRET
PassEnv DATABASE_URL
PassEnv MAILER_URL
PassEnv TRUSTED_PROXIES
PassEnv TRUSTED_HOSTS
<Directory "/opt/kimai/public">
Require all granted
DirectoryIndex index.php
AllowOverride All
</Directory>
</VirtualHost>
ServerName localhost

55
.docker/dbtest.php Normal file
View File

@@ -0,0 +1,55 @@
<?php
$DB_HOST = $argv[1];
$DB_BASE = $argv[2];
$DB_PORT = $argv[3];
$DB_USER = $argv[4];
$DB_PASS = $argv[5];
echo "Testing DB:";
echo "*";
echo "* new \PDO(mysql:host=$DB_HOST;dbname=$DB_BASE;port=$DB_PORT, $DB_USER, $DB_PASS, [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ]);";
echo "*";
try {
$pdo = new \PDO("mysql:host=$DB_HOST;dbname=$DB_BASE;port=$DB_PORT", "$DB_USER", "$DB_PASS", [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
]);
} catch(\Exception $ex) {
switch ($ex->getCode()) {
// we can immediately stop startup here and show the error message
case 1045:
echo 'Access denied (1045)';
die(1);
// we can immediately stop startup here and show the error message
case 1049:
echo 'Unknown database (1049)';
die(2);
// a lot of errors share the same meaningless error code zero
case 0:
// this error includes the database name, so we can only search for the static part of the error message
if (stripos($ex->getMessage(), 'SQLSTATE[HY000] [1049] Unknown database') !== false) {
echo 'Unknown database (0-1049)';
die(3);
}
switch ($ex->getMessage()) {
// eg. no response (fw) - the startup script should retry it a couple of times
case 'SQLSTATE[HY000] [2002] Operation timed out':
echo 'Operation timed out (0-2002)';
die(4);
// special case "localhost" with a stopped db server (should not happen in docker compose setup)
case 'SQLSTATE[HY000] [2002] No such file or directory':
echo 'Connection could not be established (0-2002)';
die(5);
// using IP with stopped db server - the startup script should retry it a couple of times
case 'SQLSTATE[HY000] [2002] Connection refused':
echo 'Connection refused (0-2002)';
die(5);
}
echo $ex->getMessage() . " (0)";
die(7);
default:
// unknown error
echo $ex->getMessage() . " (?)";
die(10);
}
}

48
.docker/monolog.yaml Normal file
View File

@@ -0,0 +1,48 @@
when@prod:
monolog:
channels: ["deprecation"]
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [403, 404]
nested:
type: stream
level: info
path: php://stderr
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
deprecation:
type: stream
channels: ["deprecation"]
path: php://stderr
when@dev:
monolog:
channels: ["deprecation"]
handlers:
main:
type: stream
path: php://stderr
level: info
channels: ["!event"]
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
deprecation:
type: stream
channels: ["deprecation"]
path: php://stderr
when@test:
monolog:
handlers:
main:
type: stream
path: php://stderr
level: info
channels: ["!event"]

59
.docker/self-test.sh Executable file
View File

@@ -0,0 +1,59 @@
#!/bin/sh
/startup.sh 2>&1
echo $$ > /tmp/startup.pid
echo "Waiting for kimai to install"
while [ ! -f /opt/kimai/var/installed ]; do
echo -n ". "
sleep 5
done
echo
# Test FPM CGI
if [ -f /use_fpm ]; then
export SCRIPT_NAME=/opt/kimai/public/index.php
export SCRIPT_FILENAME=/opt/kimai/public/index.php
export REQUEST_METHOD=GET
export SERVER_ADDR=localhost
COUNT=0
until cgi-fcgi -bind -connect 127.0.0.1:9000 &> /dev/null
do
COUNT=$((COUNT+1))
echo "Waiting for FPM Server to start (${COUNT})"
sleep 3
if [ "$COUNT" -gt 5 ]; then
echo "FPM Failed to start."
exit 1
fi
done
fi
# Test Apache/httpd
if [ -f /use_apache ]; then
COUNT=0
until curl -s -o /dev/null http://localhost:8001
do
COUNT=$((COUNT+1))
echo "Waiting for Apache/HTTP to start (${COUNT})" &> /dev/null
sleep 3
if [ "$COUNT" -gt 5 ]; then
echo "Apache/HTTP failed to start."
exit 1
fi
done
fi
# Test PHP/Kimai
/opt/kimai/bin/console kimai:version
if [ $? != 0 ]; then
echo "PHP/Kimai not responding"
exit 1
fi
kill $(cat /tmp/startup.pid)

61
.docker/service.sh Executable file
View File

@@ -0,0 +1,61 @@
#!/bin/bash -x
function waitForDB() {
# Parse sql connection data
if [ ! -z "$DATABASE_URL" ]; then
DB_TYPE=$(awk -F '[/:@]' '{print $1}' <<< "$DATABASE_URL")
DB_USER=$(awk -F '[/:@]' '{print $4}' <<< "$DATABASE_URL")
DB_PASS=$(awk -F '[/:@]' '{print $5}' <<< "$DATABASE_URL")
DB_HOST=$(awk -F '[/:@]' '{print $6}' <<< "$DATABASE_URL")
DB_PORT=$(awk -F '[/:@]' '{print $7}' <<< "$DATABASE_URL")
DB_BASE=$(awk -F '[/?]' '{print $4}' <<< "$DATABASE_URL")
else
DB_TYPE=${DB_TYPE:mysql}
if [ "$DB_TYPE" == "mysql" ]; then
export DATABASE_URL="${DB_TYPE}://${DB_USER:=kimai}:${DB_PASS:=kimai}@${DB_HOST:=sqldb}:${DB_PORT:=3306}/${DB_BASE:=kimai}"
else
echo "Unknown database type, cannot proceed. Only 'mysql' is supported, received: [$DB_TYPE]"
exit 1
fi
fi
re='^[0-9]+$'
if ! [[ $DB_PORT =~ $re ]] ; then
DB_PORT=3306
fi
echo "Wait for MySQL DB connection ..."
until php /dbtest.php $DB_HOST $DB_BASE $DB_PORT $DB_USER $DB_PASS; do
echo Checking DB: $?
sleep 3
done
echo "Connection established"
}
function handleStartup() {
# These are idempotent, run them anyway
/opt/kimai/bin/console -n kimai:install
/opt/kimai/bin/console -n kimai:update
if [ ! -z "$ADMINPASS" ] && [ ! -a "$ADMINMAIL" ]; then
/opt/kimai/bin/console kimai:user:create superadmin "$ADMINMAIL" ROLE_SUPER_ADMIN "$ADMINPASS"
fi
echo "$KIMAI" > /opt/kimai/var/installed
echo "Kimai2 ready"
}
function runServer() {
# Just while I'm fixing things
/opt/kimai/bin/console kimai:reload --env="$APP_ENV"
chown -R $USER_ID:$GROUP_ID /opt/kimai/var
if [ -e /use_apache ]; then
exec /usr/sbin/apache2 -D FOREGROUND
elif [ -e /use_fpm ]; then
exec php-fpm
else
echo "Error, unknown server type"
fi
}
waitForDB
handleStartup
runServer

96
.docker/startup.sh Executable file
View File

@@ -0,0 +1,96 @@
#!/bin/bash -x
KIMAI=$(cat /opt/kimai/version.txt)
echo $KIMAI
function config() {
# set mem limits and copy in custom logger config
if [ -z "$memory_limit" ]; then
memory_limit=256M
fi
# Parse sql connection data
if [ ! -z "$DATABASE_URL" ]; then
DB_TYPE=$(awk -F '[/:@]' '{print $1}' <<< "$DATABASE_URL")
DB_USER=$(awk -F '[/:@]' '{print $4}' <<< "$DATABASE_URL")
DB_PASS=$(awk -F '[/:@]' '{print $5}' <<< "$DATABASE_URL")
DB_HOST=$(awk -F '[/:@]' '{print $6}' <<< "$DATABASE_URL")
DB_PORT=$(awk -F '[/:@]' '{print $7}' <<< "$DATABASE_URL")
DB_BASE=$(awk -F '[/?]' '{print $4}' <<< "$DATABASE_URL")
else
DB_TYPE=${DB_TYPE:mysql}
if [ "$DB_TYPE" == "mysql" ]; then
export DATABASE_URL="${DB_TYPE}://${DB_USER:=kimai}:${DB_PASS:=kimai}@${DB_HOST:=sqldb}:${DB_PORT:=3306}/${DB_BASE:=kimai}"
else
echo "Unknown database type, cannot proceed. Only 'mysql' is supported, received: [$DB_TYPE]"
exit 1
fi
fi
re='^[0-9]+$'
if ! [[ $DB_PORT =~ $re ]] ; then
DB_PORT=3306
fi
echo "Wait for MySQL DB connection ..."
until php /dbtest.php $DB_HOST $DB_BASE $DB_PORT $DB_USER $DB_PASS; do
echo Checking DB: $?
sleep 3
done
echo "Connection established"
}
function handleStartup() {
# set mem limits and copy in custom logger config
sed -i "s/memory_limit.*/memory_limit=$memory_limit/g" /usr/local/etc/php/php.ini
cp /assets/monolog.yaml /opt/kimai/config/packages/monolog.yaml
tar -zx -C /opt/kimai -f /var/tmp/public.tgz
if [ -z "$USER_ID" ]; then
USER_ID=$(id -u www-data)
fi
if [ -z "$GROUP_ID" ]; then
GROUP_ID=$(id -g www-data)
fi
# if group doesn't exist
if grep -w "$GROUP_ID" /etc/group &>/dev/null; then
echo Group already exists
else
echo www-kimai:x:"$GROUP_ID": >> /etc/group
grpconv
fi
# if user doesn't exist
if id "$USER_ID" &>/dev/null; then
echo User already exists
else
echo www-kimai:x:"$USER_ID":"$GROUP_ID":www-kimai:/var/www:/usr/sbin/nologin >> /etc/passwd
pwconv
fi
if [ -e /use_apache ]; then
export APACHE_RUN_USER=$(id -nu "$USER_ID")
# This doesn't _exactly_ run as the specified GID, it runs as the GID of the specified user but WTF
export APACHE_RUN_GROUP=$(id -ng "$USER_ID")
export APACHE_PID_FILE=/var/run/apache2/apache2.pid
export APACHE_RUN_DIR=/var/run/apache2
export APACHE_LOCK_DIR=/var/lock/apache2
export APACHE_LOG_DIR=/var/log/apache2
export LANG=C
elif [ -e /use_fpm ]; then
sed -i "s/user = .*/user = $USER_ID/g" /usr/local/etc/php-fpm.d/www.conf
sed -i "s/group = .*/group = $GROUP_ID/g" /usr/local/etc/php-fpm.d/www.conf
echo "Setting fpm to run as ${USER_ID}:${GROUP_ID}"
else
echo "Error, unknown server type"
fi
}
config
handleStartup
exec /service.sh

17
.docker/test-kimai-version.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/sh
# An reg wizard can probably do this without the cut command
VER=$(echo $KIMAI | sed 's/[^0-9]//g' | cut -c1-3)
if test "${VER}" -lt 111
then
echo "+--------------------------------------------------------------------------+"
echo "| Kimai versions older than 1.11 require composer 1.x |"
echo "| To build older versions you'll need to use a tagged version of this repo |"
echo "| https://github.com/tobybatch/kimai2/releases/tag/EOL-composer-1.x |"
echo "| |"
echo "| See https://github.com/tobybatch/kimai2/issues/180 |"
echo "+--------------------------------------------------------------------------+"
return 1
fi

22
.docker/test-lite.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/sh -e
# Test PHP/Kimai
/opt/kimai/bin/console kimai:version
if [ $? != 0 ]; then
echo "PHP/Kimai not responding"
exit 1
fi
# Test FPM CGI
if [ -f /use_fpm ]; then
echo Testing FPM
php-fpm -t
fi
# Test Apache/httpd
if [ -f /use_apache ]; then
echo Testing Apache
apache2ctl -t
fi
exit 0