From f3dafd1f01f24e23068810da8f2a93d5644dfa3e Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:17:14 -0400 Subject: [PATCH 01/12] refactor: Streamline initialization script and permissions setup for Yii2 directories; update PHPStan tmpDir configuration and add .gitignore for runtime. --- docker/frankenphp/Dockerfile | 7 ---- docker/init.sh | 50 +++++++++++++++++++++++--- phpstan.neon | 2 +- {public/runtime => runtime}/.gitignore | 0 4 files changed, 47 insertions(+), 12 deletions(-) rename {public/runtime => runtime}/.gitignore (100%) diff --git a/docker/frankenphp/Dockerfile b/docker/frankenphp/Dockerfile index f46ab27..132fb18 100644 --- a/docker/frankenphp/Dockerfile +++ b/docker/frankenphp/Dockerfile @@ -53,13 +53,6 @@ COPY docker/frankenphp/supervisord/conf.d/frankenphp.conf /etc/supervisor/conf.d # Copy queue worker config uncommented for use with yii2-queue #COPY docker/frankenphp/supervisord/conf.d/queue.conf /etc/supervisor/conf.d/queue.conf -# Create necessary directories and set permissions -RUN mkdir -p /app/runtime/cache /app/runtime/logs && \ - chown -R ${USER_NAME}:${GROUP_NAME} /var/run /app && \ - chown -R ${USER_NAME}:${GROUP_NAME} /app/public /app/runtime && \ - chmod -R 755 /app && \ - chmod -R 775 /app/runtime /app/public - # Copy init script COPY docker/init.sh /usr/local/bin/init.sh diff --git a/docker/init.sh b/docker/init.sh index 8c3de96..0adfab1 100644 --- a/docker/init.sh +++ b/docker/init.sh @@ -1,6 +1,46 @@ +#!/bin/bash + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +echo -e "${GREEN}Starting container setup...${NC}" + +# Create necessary Yii2 directories if they don't exist +echo -e "${YELLOW}Creating Yii2 directories...${NC}" +mkdir -p /app/runtime/cache +mkdir -p /app/runtime/logs +mkdir -p /app/public/assets + +# Configure permissions for Yii2 directories +echo -e "${YELLOW}Setting up permissions...${NC}" +chmod -R 775 /app/runtime +chmod -R 775 /app/public/assets + +# Ensure www-data has proper ownership +chown -R www-data:www-data /app/runtime +chown -R www-data:www-data /app/public/assets + +# Verify directories exist and have correct permissions +if [ -d "/app/runtime" ] && [ -w "/app/runtime" ]; then + echo -e "${GREEN}✓ Runtime directory configured correctly${NC}" +else + echo -e "${RED}✗ Error: Could not configure runtime directory${NC}" +fi + +if [ -d "/app/public/assets" ] && [ -w "/app/public/assets" ]; then + echo -e "${GREEN}✓ Assets directory configured correctly${NC}" +else + echo -e "${RED}✗ Error: Could not configure assets directory${NC}" +fi + +echo -e "${GREEN}Setup completed.${NC}" + # Check if composer.json exists and vendor directory doesn't exist if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then - echo "Installing Composer dependencies..." + echo -e "${YELLOW}Installing Composer dependencies...${NC}" # Install dependencies based on environment if [ "$YII_ENV" = "prod" ]; then @@ -12,10 +52,12 @@ if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then fi # Set proper ownership for vendor directory - chown -R $USER_NAME:$GROUP_NAME /app/vendor + chown -R www-data:www-data /app/vendor - echo "Composer dependencies installed successfully." + echo -e "${GREEN}✓ Composer dependencies installed successfully.${NC}" fi +echo -e "${GREEN}Starting supervisord...${NC}" + # Start supervisord -exec supervisord -c /etc/supervisor/supervisord.conf +exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf diff --git a/phpstan.neon b/phpstan.neon index b0c30f3..ced0425 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -14,7 +14,7 @@ parameters: - tests/Functional - tests/Unit - tmpDir: %currentWorkingDirectory%/public/runtime + tmpDir: %currentWorkingDirectory%/runtime yii2: config_path: %currentWorkingDirectory%/config/web/app.php diff --git a/public/runtime/.gitignore b/runtime/.gitignore similarity index 100% rename from public/runtime/.gitignore rename to runtime/.gitignore From f8167d150c8eb909c2b08e34b31b1555595733b1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:24:47 -0400 Subject: [PATCH 02/12] fix: Update init script to improve error messages and ensure proper permission checks for Yii2 directories. --- docker/init.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docker/init.sh b/docker/init.sh index 0adfab1..4107e5b 100644 --- a/docker/init.sh +++ b/docker/init.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash +set -euo pipefail # Colors for output RED='\033[0;31m' @@ -24,16 +25,16 @@ chown -R www-data:www-data /app/runtime chown -R www-data:www-data /app/public/assets # Verify directories exist and have correct permissions -if [ -d "/app/runtime" ] && [ -w "/app/runtime" ]; then +if [ -d "/app/runtime" ] && sudo -u www-data test -w "/app/runtime"; then echo -e "${GREEN}✓ Runtime directory configured correctly${NC}" else - echo -e "${RED}✗ Error: Could not configure runtime directory${NC}" + echo -e "${RED}✗ Error: www-data cannot write to /app/runtime directory${NC}" fi -if [ -d "/app/public/assets" ] && [ -w "/app/public/assets" ]; then +if [ -d "/app/public/assets" ] && sudo -u www-data test -w "/app/public/assets"; then echo -e "${GREEN}✓ Assets directory configured correctly${NC}" else - echo -e "${RED}✗ Error: Could not configure assets directory${NC}" + echo -e "${RED}✗ Error: www-data cannot write to /app/public/assets directory${NC}" fi echo -e "${GREEN}Setup completed.${NC}" From e17877bbbb9e13bec2bcae306785902efe296271 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:30:33 -0400 Subject: [PATCH 03/12] fix: Remove vendor package update step from Nginx CI workflow to streamline the process. --- .github/workflows/docker.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index ca39572..6bae2fd 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -79,9 +79,6 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.nginx.yml build --no-cache && docker-compose -f docker-compose.nginx.yml up -d - - name: Update vendor packages. - run: docker exec yii2-nginx composer update --prefer-dist -vvv - - name: Codeception build. run: docker exec yii2-nginx vendor/bin/codecept build From 2df5183a4d2f60ae637b58a49655889babf059a8 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:32:07 -0400 Subject: [PATCH 04/12] fix: Move vendor package update step to Nginx CI workflow for consistency across environments. --- .github/workflows/docker.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 6bae2fd..2fbd53b 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -54,9 +54,6 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.frankenphp.yml build --no-cache && docker-compose -f docker-compose.frankenphp.yml up -d - - name: Update vendor packages. - run: docker exec yii2-frankenphp composer update --prefer-dist -vvv - - name: Codeception build. run: docker exec yii2-frankenphp vendor/bin/codecept build @@ -79,6 +76,9 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.nginx.yml build --no-cache && docker-compose -f docker-compose.nginx.yml up -d + - name: Update vendor packages. + run: docker exec yii2-nginx composer update --prefer-dist -vvv + - name: Codeception build. run: docker exec yii2-nginx vendor/bin/codecept build From dee56c7e506ce556d684e5b03712594d25b421b1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:42:00 -0400 Subject: [PATCH 05/12] fix: Add wait step for container initialization and enhance debugging with logs. --- .github/workflows/docker.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 2fbd53b..4918537 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -54,6 +54,20 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.frankenphp.yml build --no-cache && docker-compose -f docker-compose.frankenphp.yml up -d + - name: Wait for container initialization + run: | + echo "Waiting for container to be ready..." + # Wait for the init script to complete + timeout 60 bash -c 'until docker exec yii2-frankenphp test -d /app/vendor; do sleep 2; done' + + # Additional check: wait for supervisord to be running + timeout 30 bash -c 'until docker exec yii2-frankenphp pgrep supervisord > /dev/null; do sleep 2; done' + + echo "Container is ready!" + + - name: Show container logs for debugging + run: docker logs yii2-frankenphp + - name: Codeception build. run: docker exec yii2-frankenphp vendor/bin/codecept build From 32c0ee1d45a00c1b133054bd2546e2434afdac09 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:46:12 -0400 Subject: [PATCH 06/12] fix: Update container initialization step to improve readiness checks and enhance debugging with logs. --- .github/workflows/docker.yml | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 4918537..57a538e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -52,21 +52,13 @@ jobs: docker-compose --version - name: Build and start containers. - run: docker-compose -f docker-compose.frankenphp.yml build --no-cache && docker-compose -f docker-compose.frankenphp.yml up -d + run: docker-compose -f && docker-compose -f docker-compose.frankenphp.yml up -d --build - - name: Wait for container initialization + - name: Wait for container to be ready run: | - echo "Waiting for container to be ready..." - # Wait for the init script to complete - timeout 60 bash -c 'until docker exec yii2-frankenphp test -d /app/vendor; do sleep 2; done' - - # Additional check: wait for supervisord to be running - timeout 30 bash -c 'until docker exec yii2-frankenphp pgrep supervisord > /dev/null; do sleep 2; done' - - echo "Container is ready!" - - - name: Show container logs for debugging - run: docker logs yii2-frankenphp + echo "Waiting 30 seconds for container initialization..." + sleep 30 + docker logs yii2-frankenphp - name: Codeception build. run: docker exec yii2-frankenphp vendor/bin/codecept build From eb57a3f9b6e3a98f961c0eb10f1613dcf1e48beb Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:47:45 -0400 Subject: [PATCH 07/12] fix: Correct docker-compose command syntax for frankenphp container startup. --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 57a538e..5520e05 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -52,7 +52,7 @@ jobs: docker-compose --version - name: Build and start containers. - run: docker-compose -f && docker-compose -f docker-compose.frankenphp.yml up -d --build + run: docker-compose -f docker-compose.frankenphp.yml up -d --build - name: Wait for container to be ready run: | From b4faea2ecb20dc46741dacda20490da33548817f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:53:30 -0400 Subject: [PATCH 08/12] fix: Refactor init script to improve directory permission handling and ownership checks. --- docker/init.sh | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/docker/init.sh b/docker/init.sh index 4107e5b..6fd8671 100644 --- a/docker/init.sh +++ b/docker/init.sh @@ -1,5 +1,4 @@ -#!/usr/bin/env bash -set -euo pipefail +#!/bin/bash # Colors for output RED='\033[0;31m' @@ -17,24 +16,30 @@ mkdir -p /app/public/assets # Configure permissions for Yii2 directories echo -e "${YELLOW}Setting up permissions...${NC}" -chmod -R 775 /app/runtime -chmod -R 775 /app/public/assets -# Ensure www-data has proper ownership -chown -R www-data:www-data /app/runtime -chown -R www-data:www-data /app/public/assets - -# Verify directories exist and have correct permissions -if [ -d "/app/runtime" ] && sudo -u www-data test -w "/app/runtime"; then +# Try to set permissions and ownership - handle both mounted volumes and container-only scenarios +if chown -R www-data:www-data /app/runtime 2>/dev/null; then + chmod -R 775 /app/runtime echo -e "${GREEN}✓ Runtime directory configured correctly${NC}" else - echo -e "${RED}✗ Error: www-data cannot write to /app/runtime directory${NC}" + # If chown fails (mounted volume), try chmod only + if chmod -R 777 /app/runtime 2>/dev/null; then + echo -e "${YELLOW}⚠ Runtime directory permissions set to 777 (mounted volume)${NC}" + else + echo -e "${RED}✗ Error: Could not configure runtime directory${NC}" + fi fi -if [ -d "/app/public/assets" ] && sudo -u www-data test -w "/app/public/assets"; then +if chown -R www-data:www-data /app/public/assets 2>/dev/null; then + chmod -R 775 /app/public/assets echo -e "${GREEN}✓ Assets directory configured correctly${NC}" else - echo -e "${RED}✗ Error: www-data cannot write to /app/public/assets directory${NC}" + # If chown fails (mounted volume), try chmod only + if chmod -R 777 /app/public/assets 2>/dev/null; then + echo -e "${YELLOW}⚠ Assets directory permissions set to 777 (mounted volume)${NC}" + else + echo -e "${RED}✗ Error: Could not configure assets directory${NC}" + fi fi echo -e "${GREEN}Setup completed.${NC}" @@ -52,8 +57,10 @@ if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then composer install --optimize-autoloader --no-interaction fi - # Set proper ownership for vendor directory - chown -R www-data:www-data /app/vendor + # Set proper ownership for vendor directory if possible + if chown -R www-data:www-data /app/vendor 2>/dev/null; then + echo -e "${GREEN}✓ Vendor directory ownership set${NC}" + fi echo -e "${GREEN}✓ Composer dependencies installed successfully.${NC}" fi From fae6fe7be761d051b80c9c50315005c8be8a0b40 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 11:59:23 -0400 Subject: [PATCH 09/12] fix: Add entrypoint script for improved initialization and validation of scripts. --- docker/entrypoint.sh | 19 +++++++++++++++++++ docker/frankenphp/Dockerfile | 17 ++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 docker/entrypoint.sh diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..e0124da --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -e + +echo "=== Container Starting ===" +echo "Running initialization script..." + +# Ensure init script is executable +chmod +x /usr/local/bin/init.sh + +# Execute init script +if /usr/local/bin/init.sh; then + echo "=== Initialization completed successfully ===" +else + echo "=== Initialization failed ===" + exit 1 +fi + +# If we get here, everything went well +echo "=== Container ready ===" diff --git a/docker/frankenphp/Dockerfile b/docker/frankenphp/Dockerfile index 132fb18..80f0b97 100644 --- a/docker/frankenphp/Dockerfile +++ b/docker/frankenphp/Dockerfile @@ -53,11 +53,18 @@ COPY docker/frankenphp/supervisord/conf.d/frankenphp.conf /etc/supervisor/conf.d # Copy queue worker config uncommented for use with yii2-queue #COPY docker/frankenphp/supervisord/conf.d/queue.conf /etc/supervisor/conf.d/queue.conf -# Copy init script +# Copy scripts COPY docker/init.sh /usr/local/bin/init.sh +COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh -# Make init script executable -RUN chmod +x /usr/local/bin/init.sh +# Make scripts executable and validate +RUN chmod +x /usr/local/bin/init.sh /usr/local/bin/entrypoint.sh && \ + # Convert any Windows line endings + sed -i 's/\r$//' /usr/local/bin/init.sh /usr/local/bin/entrypoint.sh && \ + # Test that scripts have valid syntax + bash -n /usr/local/bin/init.sh && \ + bash -n /usr/local/bin/entrypoint.sh && \ + echo "Scripts validated successfully" -# Run initialization script and then supervisord -CMD ["/usr/local/bin/init.sh"] +# Use ENTRYPOINT to guarantee execution +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] From 812489321aa2413f2cc016b14e645bf12b09045a Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 12:06:11 -0400 Subject: [PATCH 10/12] fix: Enhance entrypoint script with strict error handling and improved execution flow. --- docker/entrypoint.sh | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index e0124da..c22af76 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -e ++set -euo pipefail echo "=== Container Starting ===" echo "Running initialization script..." @@ -7,13 +7,8 @@ echo "Running initialization script..." # Ensure init script is executable chmod +x /usr/local/bin/init.sh -# Execute init script -if /usr/local/bin/init.sh; then - echo "=== Initialization completed successfully ===" -else - echo "=== Initialization failed ===" - exit 1 -fi +# Execute init script; replace the PID 1 shell +exec /usr/local/bin/init.sh # If we get here, everything went well echo "=== Container ready ===" From 5c998f535eef19e463f5dece8d1e0876e4bc3a68 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 12:13:06 -0400 Subject: [PATCH 11/12] fix: Remove unnecessary wait step from frankenphp container startup and ensure entrypoint is set. --- .github/workflows/docker.yml | 6 ------ docker-compose.frankenphp.yml | 1 + 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 5520e05..2df765c 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -54,12 +54,6 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.frankenphp.yml up -d --build - - name: Wait for container to be ready - run: | - echo "Waiting 30 seconds for container initialization..." - sleep 30 - docker logs yii2-frankenphp - - name: Codeception build. run: docker exec yii2-frankenphp vendor/bin/codecept build diff --git a/docker-compose.frankenphp.yml b/docker-compose.frankenphp.yml index b1f2171..2f4a97d 100644 --- a/docker-compose.frankenphp.yml +++ b/docker-compose.frankenphp.yml @@ -9,6 +9,7 @@ services: context: . dockerfile: docker/frankenphp/Dockerfile container_name: yii2-frankenphp + entrypoint: ["/usr/local/bin/entrypoint.sh"] env_file: - .env restart: always From caa2a325f8fb15eaf8e2c0b712d8b82e8031fd17 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 11 Jul 2025 12:21:51 -0400 Subject: [PATCH 12/12] fix: Add wait step for frankenphp container initialization and log output. --- .github/workflows/docker.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 2df765c..64b7e7e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -54,6 +54,12 @@ jobs: - name: Build and start containers. run: docker-compose -f docker-compose.frankenphp.yml up -d --build + - name: Wait for container to be ready. + run: | + echo "Waiting 30 seconds for container initialization..." + sleep 30 + docker logs yii2-frankenphp + - name: Codeception build. run: docker exec yii2-frankenphp vendor/bin/codecept build