1
0
Fork 0

Compare commits

...

2 commits

Author SHA1 Message Date
thibaud-leclere
645d8af22d build: add service healthchecks 2026-05-05 11:54:59 +02:00
thibaud-leclere
3b322c9ee0 fix: run boot console commands as runtime user 2026-05-05 11:46:50 +02:00
4 changed files with 84 additions and 3 deletions

View file

@ -13,6 +13,12 @@ services:
volumes: volumes:
- app-data:/app/var/data - app-data:/app/var/data
- app-cache:/app/var/bootstrap-cache - app-cache:/app/var/bootstrap-cache
healthcheck:
test: ["CMD-SHELL", "php -r 'exit(@fsockopen(\"127.0.0.1\", 9000) ? 0 : 1);'"]
interval: 10s
timeout: 3s
retries: 3
start_period: 20s
nginx: nginx:
build: build:
@ -20,9 +26,16 @@ services:
dockerfile: docker/Dockerfile dockerfile: docker/Dockerfile
target: web target: web
depends_on: depends_on:
- app app:
condition: service_healthy
ports: ports:
- "${HTTP_PORT:-8080}:80" - "${HTTP_PORT:-8080}:80"
healthcheck:
test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1/ || exit 1"]
interval: 10s
timeout: 3s
retries: 3
start_period: 20s
volumes: volumes:
app-data: app-data:

View file

@ -5,8 +5,8 @@ mkdir -p /app/var/data /app/var/bootstrap-cache
chown -R www-data:www-data /app/var chown -R www-data:www-data /app/var
if [ "${APP_ENV:-prod}" != "test" ]; then if [ "${APP_ENV:-prod}" != "test" ]; then
php /app/bin/console cache:clear --no-warmup --no-interaction su -m www-data -s /bin/sh -c 'cd /app && php /app/bin/console cache:clear --no-warmup --no-interaction'
php /app/bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration su -m www-data -s /bin/sh -c 'cd /app && php /app/bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration'
fi fi
if [ "${APP_ENV:-prod}" = "prod" ] && [ "${1:-}" = "php-fpm" ]; then if [ "${APP_ENV:-prod}" = "prod" ] && [ "${1:-}" = "php-fpm" ]; then

View file

@ -0,0 +1,45 @@
<?php
namespace App\Tests\Docker;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;
final class ComposeHealthcheckTest extends TestCase
{
public function testServicesDeclareHealthchecks(): void
{
$compose = Yaml::parseFile(__DIR__.'/../../compose.yaml');
self::assertSame(
[
'CMD-SHELL',
'php -r \'exit(@fsockopen("127.0.0.1", 9000) ? 0 : 1);\'',
],
$compose['services']['app']['healthcheck']['test'] ?? null
);
self::assertSame('10s', $compose['services']['app']['healthcheck']['interval'] ?? null);
self::assertSame('3s', $compose['services']['app']['healthcheck']['timeout'] ?? null);
self::assertSame(3, $compose['services']['app']['healthcheck']['retries'] ?? null);
self::assertSame('20s', $compose['services']['app']['healthcheck']['start_period'] ?? null);
self::assertSame(
['CMD-SHELL', 'wget -q --spider http://127.0.0.1/ || exit 1'],
$compose['services']['nginx']['healthcheck']['test'] ?? null
);
self::assertSame('10s', $compose['services']['nginx']['healthcheck']['interval'] ?? null);
self::assertSame('3s', $compose['services']['nginx']['healthcheck']['timeout'] ?? null);
self::assertSame(3, $compose['services']['nginx']['healthcheck']['retries'] ?? null);
self::assertSame('20s', $compose['services']['nginx']['healthcheck']['start_period'] ?? null);
}
public function testNginxWaitsForHealthyApp(): void
{
$compose = Yaml::parseFile(__DIR__.'/../../compose.yaml');
self::assertSame(
['app' => ['condition' => 'service_healthy']],
$compose['services']['nginx']['depends_on'] ?? null
);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Tests\Docker;
use PHPUnit\Framework\TestCase;
final class EntrypointTest extends TestCase
{
public function testBootConsoleCommandsRunAsRuntimeUser(): void
{
$entrypoint = file_get_contents(__DIR__.'/../../docker/entrypoint.sh');
self::assertIsString($entrypoint);
self::assertStringContainsString(
"su -m www-data -s /bin/sh -c 'cd /app && php /app/bin/console cache:clear --no-warmup --no-interaction'",
$entrypoint
);
self::assertStringContainsString(
"su -m www-data -s /bin/sh -c 'cd /app && php /app/bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration'",
$entrypoint
);
}
}