From 10da9c87d782ee08dfdf08dfdf460b6b466f6f55 Mon Sep 17 00:00:00 2001 From: Ben van Hartingsveldt Date: Sun, 25 May 2025 19:36:22 +0200 Subject: [PATCH] Dockerize project --- .github/workflows/docker-image.yml | 16 +++++++++ Dockerfile | 57 ++++++++++++++++++++++++++++++ nginx.conf | 20 +++++++++++ opcache.ini | 7 ++++ php.ini | 1 + preload.php | 7 ++++ 6 files changed, 108 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 Dockerfile create mode 100644 nginx.conf create mode 100644 opcache.ini create mode 100644 php.ini create mode 100644 preload.php diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..70eac1b --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,16 @@ +name: Docker Image CI + +on: + push: + branches: ["**"] + pull_request: + branches: ["**"] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..573dd5c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +FROM php:8.4.7-fpm-alpine + +# Install PHPize packages +RUN apk add --no-cache --virtual .phpize $PHPIZE_DEPS + +# Install Source Packages +ENV SRC_DEPS="gmp-dev icu-dev" +RUN apk add --no-cache --virtual .source $SRC_DEPS + +# Install Binary Packages +ENV BIN_DEPS="gmp git icu nginx" +RUN apk add --no-cache --virtual .binary $BIN_DEPS + +RUN docker-php-ext-install opcache + +# Delete PHPize packages +RUN apk del --no-network --no-cache --purge .phpize + +# Delete Source packages +RUN apk del --no-network --no-cache --purge .source + +# Remove files +RUN rm -rf /tmp/pear +RUN rm -rf ~/.pearrc +RUN rm -rf /var/cache/apk/* + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Copy files +WORKDIR /var/www/html +COPY . . +RUN mv nginx.conf /etc/nginx/http.d/default.conf + +# Install project using Composer +RUN --mount=type=cache,target=/root/.composer composer install --no-interaction --optimize-autoloader --no-dev + +# Change permissions +RUN chown -R www-data:www-data storage/ + +# Setup Process Manager +RUN echo "pm = ondemand" >> /usr/local/etc/php-fpm.d/zz-docker.conf +RUN echo "pm.process_idle_timeout = 10s" >> /usr/local/etc/php-fpm.d/zz-docker.conf + +# Setup PHP +RUN mv php.ini /usr/local/etc/php/conf.d/ + +# Setup Opcache +RUN mv opcache.ini /usr/local/etc/php/conf.d/ + +# Setup CRON +RUN echo -e "*\t*\t*\t*\t*\tcd /var/www/html && php artisan schedule:run >> /dev/null 2>&1" >> /var/spool/cron/crontabs/root + +VOLUME /var/www/html/storage/framework/cache/data + +# Cache project and Start PHP-FPM and NGINX +CMD php artisan optimize; php artisan event:cache; php artisan view:cache; sh -c "php artisan queue:work &"; n crond; nginx -g "daemon off;" diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..99f3d4c --- /dev/null +++ b/nginx.conf @@ -0,0 +1,20 @@ +server{ + listen 80; + + root /var/www/html/public; + index index.php; + + gzip on; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ \.php$ { + fastcgi_buffers 16 1024k; + fastcgi_buffer_size 1024k; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_pass localhost:9000; + include /etc/nginx/fastcgi_params; + } +} diff --git a/opcache.ini b/opcache.ini new file mode 100644 index 0000000..5b2655a --- /dev/null +++ b/opcache.ini @@ -0,0 +1,7 @@ +opcache.jit_buffer_size=256M +opcache.max_accelerated_files=20000 +opcache.memory_consumption=256 +opcache.preload = '/var/www/html/preload.php' +;opcache.preload = '/app/preload.php' +opcache.preload_user = 'www-data' +opcache.validate_timestamps=0 diff --git a/php.ini b/php.ini new file mode 100644 index 0000000..907d80b --- /dev/null +++ b/php.ini @@ -0,0 +1 @@ +memory_limit = ${PHP_MEMORY_LIMIT} diff --git a/preload.php b/preload.php new file mode 100644 index 0000000..2d6ea39 --- /dev/null +++ b/preload.php @@ -0,0 +1,7 @@ +