Improve docker installation (#5115)

* ignore certain connection errors on startup
* remove invalid code that checks for the existence of the migration table
* fetch kimai code via tar archive instead of git clone, to respect .gitattributes
This commit is contained in:
Kevin Papst
2024-10-14 23:09:14 +02:00
committed by GitHub
parent 32a1306394
commit 9e736b26f6
3 changed files with 19 additions and 32 deletions

View File

@@ -13,20 +13,19 @@ try {
]); ]);
} catch(\Exception $ex) { } catch(\Exception $ex) {
switch ($ex->getCode()) { switch ($ex->getCode()) {
// we can immediately stop startup here and show the error message
case 1045: case 1045:
// we can immediately stop here and show the error message
echo 'Access denied (1045)'; echo 'Access denied (1045)';
die(1); die(1);
// we can immediately stop startup here and show the error message
case 1049: case 1049:
echo 'Unknown database (1049)'; // error "Unknown database (1049)" can be ignored, the database will be created by Kimai
die(2); return;
// a lot of errors share the same meaningless error code zero // a lot of errors share the same meaningless error code zero
case 0: case 0:
// this error includes the database name, so we can only search for the static part of the error message // 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) { if (stripos($ex->getMessage(), 'SQLSTATE[HY000] [1049] Unknown database') !== false) {
echo 'Unknown database (0-1049)'; // error "Unknown database (1049)" can be ignored, the database will be created by Kimai
die(3); return;
} }
switch ($ex->getMessage()) { switch ($ex->getMessage()) {
// eg. no response (fw) - the startup script should retry it a couple of times // eg. no response (fw) - the startup script should retry it a couple of times

View File

@@ -218,20 +218,18 @@ COPY --from=php-ext-intl /usr/local/lib/php/extensions/no-debug-non-zts-20230831
COPY --from=php-ext-opcache /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini COPY --from=php-ext-opcache /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
########################### ###########################
# Shared tools # fetch Kimai sources
########################### ###########################
# full kimai source FROM alpine:latest AS git-prod
FROM alpine:latest AS git-dev
ARG KIMAI ARG KIMAI
ARG TIMEZONE ARG TIMEZONE
RUN apk add --no-cache git && \ # the convention in the Kimai repository is: tags are always version numbers, branch names always start with a letter
git clone --depth 1 --branch ${KIMAI} https://github.com/kimai/kimai.git /opt/kimai # if the KIMAI variable starts with a number (e.g. 2.24.0) we assume its a tag, otherwise its a branch
RUN [[ $KIMAI =~ ^[0-9] ]] && export REF='tags' || export REF='heads' && \
# production kimai source wget -O "/opt/kimai.tar.gz" "https://github.com/kimai/kimai/archive/refs/${REF}/${KIMAI}.tar.gz" && \
FROM git-dev AS git-prod tar -xpzf /opt/kimai.tar.gz -C /opt/ && \
WORKDIR /opt/kimai mv /opt/kimai-${KIMAI} /opt/kimai
RUN rm -r tests
########################### ###########################
# global base build # global base build
@@ -286,10 +284,9 @@ CMD [ "/entrypoint.sh" ]
# development build # development build
FROM base AS dev FROM base AS dev
# copy kimai develop source # copy kimai develop source
COPY --from=git-dev --chown=www-data:www-data /opt/kimai /opt/kimai COPY --from=git-prod --chown=www-data:www-data /opt/kimai /opt/kimai
COPY .docker /assets COPY .docker /assets
# do the composer deps installation # do the composer deps installation
RUN echo \$PATH
RUN \ RUN \
export COMPOSER_HOME=/composer && \ export COMPOSER_HOME=/composer && \
composer --no-ansi install --working-dir=/opt/kimai --optimize-autoloader && \ composer --no-ansi install --working-dir=/opt/kimai --optimize-autoloader && \
@@ -305,7 +302,7 @@ ENV APP_ENV=dev
ENV DATABASE_URL= ENV DATABASE_URL=
ENV memory_limit=512M ENV memory_limit=512M
# production build # the "prod" stage (production build) is configured as last stage in the file, as this is the default target in BuildKit
FROM base AS prod FROM base AS prod
# copy kimai production source # copy kimai production source
COPY --from=git-prod --chown=www-data:www-data /opt/kimai /opt/kimai COPY --from=git-prod --chown=www-data:www-data /opt/kimai /opt/kimai

View File

@@ -93,7 +93,7 @@ final class InstallCommand extends Command
private function rebuildCaches(string $environment, SymfonyStyle $io, InputInterface $input, OutputInterface $output): int private function rebuildCaches(string $environment, SymfonyStyle $io, InputInterface $input, OutputInterface $output): int
{ {
$io->text('Rebuilding your cache ...'); $io->text('Rebuilding cache ...');
$command = $this->getApplication()->find('cache:clear'); $command = $this->getApplication()->find('cache:clear');
try { try {
@@ -122,15 +122,7 @@ final class InstallCommand extends Command
private function importMigrations(SymfonyStyle $io, OutputInterface $output): void private function importMigrations(SymfonyStyle $io, OutputInterface $output): void
{ {
try { $io->text('Creating database ...');
if (!$this->connection->createSchemaManager()->tablesExist(['migration_versions'])) {
throw new \RuntimeException('Migration table does not exist');
}
} catch (\Exception $ex) {
$io->error(['Failed to detect migration status, aborting update.', $ex->getMessage()]);
return;
}
$command = $this->getApplication()->find('doctrine:migrations:migrate'); $command = $this->getApplication()->find('doctrine:migrations:migrate');
$cmdInput = new ArrayInput(['--allow-no-migration' => true]); $cmdInput = new ArrayInput(['--allow-no-migration' => true]);
@@ -146,12 +138,11 @@ final class InstallCommand extends Command
{ {
try { try {
if ($this->connection->isConnected()) { if ($this->connection->isConnected()) {
$io->note(\sprintf('Database is existing and connection could be established')); // database exists: we can skip this step
return; return;
} }
} catch (\Exception $ex) { } catch (\Exception $ex) {
// this likely means that the database does not exist and the connection failed // this means the database does not exist and the connection failed
} }
$command = $this->getApplication()->find('doctrine:database:create'); $command = $this->getApplication()->find('doctrine:database:create');