diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index ca39572..64b7e7e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -52,10 +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.frankenphp.yml up -d --build - - name: Update vendor packages. - run: docker exec yii2-frankenphp composer update --prefer-dist -vvv + - 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 diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..c22af76 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/bash ++set -euo pipefail + +echo "=== Container Starting ===" +echo "Running initialization script..." + +# Ensure init script is executable +chmod +x /usr/local/bin/init.sh + +# Execute init script; replace the PID 1 shell +exec /usr/local/bin/init.sh + +# If we get here, everything went well +echo "=== Container ready ===" diff --git a/docker/frankenphp/Dockerfile b/docker/frankenphp/Dockerfile index f46ab27..80f0b97 100644 --- a/docker/frankenphp/Dockerfile +++ b/docker/frankenphp/Dockerfile @@ -53,18 +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 -# 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 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"] diff --git a/docker/init.sh b/docker/init.sh index 8c3de96..6fd8671 100644 --- a/docker/init.sh +++ b/docker/init.sh @@ -1,6 +1,52 @@ +#!/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}" + +# 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 + # 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 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 + # 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}" + # 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 @@ -11,11 +57,15 @@ if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then composer install --optimize-autoloader --no-interaction fi - # Set proper ownership for vendor directory - chown -R $USER_NAME:$GROUP_NAME /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 "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