From 4d261e8cbcd3d8aa83a5f39d2388403747b8c101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna-Sara=20S=C3=A9lea?= Date: Thu, 20 Nov 2025 07:49:23 +0100 Subject: [PATCH] Docker setup for production --- .gitignore | 1 + Dockerfile | 17 -------- bootstrap/cache/.gitignore | 0 docker-compose.yml | 43 +++++++++++++++----- docker/apache/default.conf | 11 +++++ docker/php/Dockerfile | 44 ++++++++++++++++++++ docker/scripts/start.sh | 54 +++++++++++++++++++++++++ storage/app/.gitignore | 0 storage/app/private/.gitignore | 0 storage/app/public/.gitignore | 0 storage/framework/.gitignore | 0 storage/framework/cache/.gitignore | 0 storage/framework/cache/data/.gitignore | 0 storage/framework/sessions/.gitignore | 0 storage/framework/testing/.gitignore | 0 storage/framework/views/.gitignore | 0 storage/logs/.gitignore | 0 vite.config.js | 2 +- 18 files changed, 143 insertions(+), 29 deletions(-) delete mode 100644 Dockerfile mode change 100644 => 100755 bootstrap/cache/.gitignore create mode 100644 docker/apache/default.conf create mode 100644 docker/php/Dockerfile create mode 100644 docker/scripts/start.sh mode change 100644 => 100755 storage/app/.gitignore mode change 100644 => 100755 storage/app/private/.gitignore mode change 100644 => 100755 storage/app/public/.gitignore mode change 100644 => 100755 storage/framework/.gitignore mode change 100644 => 100755 storage/framework/cache/.gitignore mode change 100644 => 100755 storage/framework/cache/data/.gitignore mode change 100644 => 100755 storage/framework/sessions/.gitignore mode change 100644 => 100755 storage/framework/testing/.gitignore mode change 100644 => 100755 storage/framework/views/.gitignore mode change 100644 => 100755 storage/logs/.gitignore diff --git a/.gitignore b/.gitignore index b71b1ea..f333fe2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .env .env.backup .env.production +.env.local .phpactor.json .phpunit.result.cache /.fleet diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index ab33816..0000000 --- a/Dockerfile +++ /dev/null @@ -1,17 +0,0 @@ -FROM php:8.2-apache-buster -RUN usermod -u 1000 www-data -RUN a2enmod rewrite -RUN apt-get update \ - && apt-get install -y gnupg2 zlib1g-dev libzip-dev zlib1g-dev libpng-dev libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libxml2-dev -RUN docker-php-ext-install zip mysqli pdo pdo_mysql && docker-php-ext-enable mysqli pdo pdo_mysql sodium -RUN docker-php-ext-configure gd -RUN docker-php-ext-install gd -RUN docker-php-ext-configure intl -RUN docker-php-ext-install intl - -# Install Composer -RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - -ENV APACHE_DOCUMENT_ROOT /var/www/html/public -RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf -RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf \ No newline at end of file diff --git a/bootstrap/cache/.gitignore b/bootstrap/cache/.gitignore old mode 100644 new mode 100755 diff --git a/docker-compose.yml b/docker-compose.yml index 5eeadca..c6b22d8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,32 +1,53 @@ -version: '2.2' services: - api: - image: php - build: . + web: + image: laravel-www + container_name: laravel-www + build: + context: . + dockerfile: docker/php/Dockerfile + depends_on: + - db volumes: - .:/var/www/html ports: - "8080:80" + environment: + CONTAINER_ROLE: app networks: - app db: + container_name: laravel-mysql image: "mariadb" ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD: "password" - MYSQL_DATABASE: "vbytes_hub" - MYSQL_USER: "root" - MYSQL_PASSWORD: "password" + MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} + MYSQL_DATABASE: ${DB_DATABASE} + MYSQL_USER: ${DB_USERNAME} + MYSQL_PASSWORD: ${DB_PASSWORD} MYSQL_ALLOW_EMPTY_PASSWORD: "yes" volumes: - "appdb:/var/lib/mysql" networks: - - app + - app + queue: + image: laravel-www + container_name: laravel-queue + depends_on: + - web + volumes: + - .:/var/www/html + networks: + - app + environment: + APP_ENV: ${APP_ENV} + CONTAINER_ROLE: queue + CACHE_STORE: ${CACHE_STORE} + SESSION_DRIVER: ${SESSION_DRIVER} + QUEUE_CONNECTION: ${QUEUE_CONNECTION} networks: app: driver: bridge volumes: appdb: - driver: local - + driver: local \ No newline at end of file diff --git a/docker/apache/default.conf b/docker/apache/default.conf new file mode 100644 index 0000000..829b473 --- /dev/null +++ b/docker/apache/default.conf @@ -0,0 +1,11 @@ + + DocumentRoot /var/www/html/public + + + AllowOverride all + Require all granted + + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + \ No newline at end of file diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile new file mode 100644 index 0000000..bae1a86 --- /dev/null +++ b/docker/php/Dockerfile @@ -0,0 +1,44 @@ +FROM php:8.4-apache + + +## Copy the entire Laravel application code into the container +COPY . /var/www/html + +# Apache vhost & entry script +COPY docker/apache/default.conf /etc/apache2/sites-available/000-default.conf +COPY docker/scripts/start.sh /usr/local/bin/start + +RUN usermod -u 1000 www-data + +RUN chown -R www-data:www-data /var/www/html \ + && chmod u+x /usr/local/bin/start \ + && a2enmod rewrite + +RUN apt-get update \ + && apt-get install -y gnupg2 zlib1g-dev libzip-dev zlib1g-dev libicu-dev libpng-dev libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libxml2-dev +RUN docker-php-ext-install zip mysqli pdo pdo_mysql && docker-php-ext-enable mysqli pdo pdo_mysql sodium +RUN docker-php-ext-configure gd +RUN docker-php-ext-install gd +RUN docker-php-ext-configure intl +RUN docker-php-ext-install intl + + +# Add Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ + && composer install --no-interaction --no-progress + +# Install Node.js and build tools +RUN apt-get update && apt-get install -y --no-install-recommends \ + curl \ + nodejs \ + npm \ + && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +# Install Node.js dependencies and build assets +RUN npm install && npm run build + + +WORKDIR /var/www/html + +#EXPOSE 80 +CMD ["/usr/local/bin/start"] diff --git a/docker/scripts/start.sh b/docker/scripts/start.sh new file mode 100644 index 0000000..cb736f6 --- /dev/null +++ b/docker/scripts/start.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +set -e + +role=${CONTAINER_ROLE:-app} +env=${APP_ENV:-production} + +chown -R www-data:www-data /var/www/html +chmod -R 775 /var/www/html/storage +chmod -R 775 /var/www/html/bootstrap/cache + +if [ ! -d "/var/www/html/vendor" ]; then + composer install --no-interaction --no-progress +fi + +if [ -f "/var/www/html/.env" ]; then + if ! grep -q "^APP_KEY=" .env || grep -q "^APP_KEY=$" .env; then + php artisan key:generate + fi +fi + +if [ "${RUN_MIGRATIONS:-false}" = "true" ]; then + php artisan migrate --force +fi + +if [ "$env" != "local" ]; then + echo "Caching configuration..." + (cd /var/www/html && + php artisan optimize && + php artisan filament:optimize && + php artisan config:cache && + php artisan route:cache && + php artisan view:cache) +fi + + +if [ "$role" = "app" ]; then + + exec apache2-foreground + +elif [ "$role" = "queue" ]; then + + echo "Running the queue..." + php /var/www/html/artisan queue:work --verbose --tries=3 --timeout=90 + +elif [ "$role" = "scheduler" ]; then + + echo "Scheduler role" + exit 1 + +else + echo "Could not match the container role \"$role\"" + exit 1 +fi \ No newline at end of file diff --git a/storage/app/.gitignore b/storage/app/.gitignore old mode 100644 new mode 100755 diff --git a/storage/app/private/.gitignore b/storage/app/private/.gitignore old mode 100644 new mode 100755 diff --git a/storage/app/public/.gitignore b/storage/app/public/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/.gitignore b/storage/framework/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/cache/.gitignore b/storage/framework/cache/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/cache/data/.gitignore b/storage/framework/cache/data/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/sessions/.gitignore b/storage/framework/sessions/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/testing/.gitignore b/storage/framework/testing/.gitignore old mode 100644 new mode 100755 diff --git a/storage/framework/views/.gitignore b/storage/framework/views/.gitignore old mode 100644 new mode 100755 diff --git a/storage/logs/.gitignore b/storage/logs/.gitignore old mode 100644 new mode 100755 diff --git a/vite.config.js b/vite.config.js index 19f2908..482b2e2 100644 --- a/vite.config.js +++ b/vite.config.js @@ -5,7 +5,7 @@ import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [ laravel({ - input: 'resources/js/app.jsx', + input: 'resources/js/app.js', refresh: true, }), react(),