Refactor Docker Entrypoint and Environment Variables

- Removed the `IS_API_WORKER` environment variable from `docker-compose.yml`, simplifying the configuration for API services and aligning with the new role-based command handling.
- Updated the `php-fpm-entrypoint` script to determine the role (API, worker, or scheduler) based on the command being executed, enhancing flexibility and clarity in service initialization.
- Streamlined the setup process for different roles, ensuring that appropriate commands are executed based on the determined role, which improves maintainability and reduces potential errors.

These changes aim to enhance the Docker configuration by adopting a more dynamic approach to service roles, improving the overall deployment process.
This commit is contained in:
Julien Nahum 2025-05-22 13:21:56 +02:00
parent 3200253163
commit 7d7aba10b1
2 changed files with 46 additions and 31 deletions

View File

@ -7,7 +7,6 @@ services:
- ./api/storage:/usr/share/nginx/html/storage:rw - ./api/storage:/usr/share/nginx/html/storage:rw
environment: &api-env environment: &api-env
APP_ENV: production APP_ENV: production
IS_API_WORKER: "false"
# Database settings # Database settings
DB_HOST: db DB_HOST: db
REDIS_HOST: redis REDIS_HOST: redis
@ -41,7 +40,6 @@ services:
environment: environment:
<<: *api-env <<: *api-env
APP_ENV: production APP_ENV: production
IS_API_WORKER: "true"
healthcheck: healthcheck:
test: ["CMD-SHELL", "pgrep -f 'php artisan queue:work' > /dev/null || exit 1"] test: ["CMD-SHELL", "pgrep -f 'php artisan queue:work' > /dev/null || exit 1"]
interval: 60s interval: 60s
@ -56,8 +54,6 @@ services:
environment: environment:
<<: *api-env <<: *api-env
APP_ENV: production APP_ENV: production
IS_API_WORKER: "true" # This might not be strictly true for scheduler, but consistent with setup
CONTAINER_ROLE: scheduler
healthcheck: healthcheck:
test: ["CMD-SHELL", "php /usr/share/nginx/html/artisan app:scheduler-status --mode=check --max-minutes=3 || exit 1"] test: ["CMD-SHELL", "php /usr/share/nginx/html/artisan app:scheduler-status --mode=check --max-minutes=3 || exit 1"]
interval: 60s interval: 60s

View File

@ -1,26 +1,50 @@
#!/bin/bash #!/bin/bash
main() { main() {
if [ "$IS_API_WORKER" = "true" ]; then local COMMAND_AS_STRING="$*"
# This is the API worker, skip setup and just run the command local ROLE="api" # Default role
apply_php_configuration
exec "$@" case "$COMMAND_AS_STRING" in
else *"artisan queue:work"*)
# This is the API service or scheduler, run full setup ROLE="worker"
apply_php_configuration ;;
prep_file_permissions *"artisan schedule:work"*)
prep_storage ROLE="scheduler"
wait_for_db ;;
*)
# Defaults to "api" for any other command (e.g., php-fpm)
ROLE="api"
;;
esac
echo "Determined role: $ROLE for command: $COMMAND_AS_STRING"
apply_php_configuration
prep_file_permissions
prep_storage
wait_for_db
if [ "$ROLE" = "api" ]; then
echo "Running setup for API role..."
apply_db_migrations apply_db_migrations
run_init_project run_init_project
optimize_application optimize_application
echo "Starting server for API role with command: $@"
if [ "$CONTAINER_ROLE" = "scheduler" ]; then exec "$@"
echo "Initializing scheduler status for first run (entrypoint)" elif [ "$ROLE" = "scheduler" ]; then
./artisan app:scheduler-status --mode=record echo "Running setup for Scheduler role..."
fi echo "Initializing scheduler status for first run (entrypoint)"
php ./artisan app:scheduler-status --mode=record
run_server "$@" echo "Starting scheduler with command: $@"
exec "$@"
elif [ "$ROLE" = "worker" ]; then
echo "Running setup for Worker role..."
echo "Starting worker with command: $@"
exec "$@"
else
# This case should ideally not be reached if ROLE defaults to "api"
echo "Error: Unknown role '$ROLE' determined from command '$COMMAND_AS_STRING'. Exiting."
exit 1
fi fi
} }
@ -78,34 +102,29 @@ prep_storage() {
# Run Laravel's storage link command (ensure script is run from app root or adjust path to artisan) # Run Laravel's storage link command (ensure script is run from app root or adjust path to artisan)
echo "Creating public storage symlink" echo "Creating public storage symlink"
./artisan storage:link php ./artisan storage:link
} }
wait_for_db() { wait_for_db() {
echo "Waiting for DB to be ready" echo "Waiting for DB to be ready"
until ./artisan migrate:status 2>&1 | grep -q -E "(Migration table not found|Migration name)"; do until php ./artisan migrate:status 2>&1 | grep -q -E "(Migration table not found|Migration name)"; do
sleep 1 sleep 1
done done
} }
apply_db_migrations() { apply_db_migrations() {
echo "Running DB Migrations" echo "Running DB Migrations"
./artisan migrate --force php ./artisan migrate --force
} }
run_init_project() { run_init_project() {
echo "Running app:init-project command" echo "Running app:init-project command"
./artisan app:init-project php ./artisan app:init-project
} }
optimize_application() { optimize_application() {
echo "Optimizing application" echo "Optimizing application"
./artisan optimize php ./artisan optimize
}
run_server() {
echo "Starting server $@"
exec "$@"
} }
main "$@" main "$@"