Docker setup for production

This commit is contained in:
Anna-Sara Sélea 2025-11-20 07:49:23 +01:00
parent daeef2c731
commit 4d261e8cbc
18 changed files with 143 additions and 29 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@
.env
.env.backup
.env.production
.env.local
.phpactor.json
.phpunit.result.cache
/.fleet

View file

@ -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

0
bootstrap/cache/.gitignore vendored Normal file → Executable file
View file

View file

@ -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
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

View file

@ -0,0 +1,11 @@
<VirtualHost *:80>
DocumentRoot /var/www/html/public
<Directory "/var/www/html/public">
AllowOverride all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

44
docker/php/Dockerfile Normal file
View file

@ -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"]

54
docker/scripts/start.sh Normal file
View file

@ -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

0
storage/app/.gitignore vendored Normal file → Executable file
View file

0
storage/app/private/.gitignore vendored Normal file → Executable file
View file

0
storage/app/public/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/cache/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/cache/data/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/sessions/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/testing/.gitignore vendored Normal file → Executable file
View file

0
storage/framework/views/.gitignore vendored Normal file → Executable file
View file

0
storage/logs/.gitignore vendored Normal file → Executable file
View file

View file

@ -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(),