feat: Add Docker configuration for Laravel application, including services for portal, database, and cache with environment variables and entrypoint script enhancements.

This commit is contained in:
pbao9
2026-03-17 17:01:48 +07:00
parent 017248eeb9
commit c273fc9aea
4 changed files with 159 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
version: '3.8'
# Standalone mode — requires: docker network create web
networks:
web:
external: true
services:
portal:
build:
context: .
dockerfile: docker/Dockerfile
expose:
- "8088"
networks:
- web
environment:
- APP_ENV=production
- APP_URL=https://demo.portal.tgs.com.vn
- DB_HOST=portal-db
- DB_PORT=3306
- DB_DATABASE=${DB_DATABASE:-portal}
- DB_USERNAME=${DB_USERNAME:-portal}
- DB_PASSWORD=${DB_PASSWORD}
- REDIS_HOST=portal-cache
- REDIS_PORT=6379
- REDIS_PASSWORD=${REDIS_PASSWORD}
depends_on:
- portal-db
- portal-cache
restart: unless-stopped
portal-db:
image: mysql:8
networks:
- web
environment:
- MYSQL_DATABASE=${DB_DATABASE:-portal}
- MYSQL_USER=${DB_USERNAME:-portal}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
volumes:
- portal_mysql_data:/var/lib/mysql
restart: unless-stopped
portal-cache:
image: redis:alpine
command: redis-server --requirepass ${REDIS_PASSWORD}
networks:
- web
volumes:
- portal_redis_data:/data
restart: unless-stopped
volumes:
portal_mysql_data:
portal_redis_data:
+14
View File
@@ -0,0 +1,14 @@
:8088 {
root * /var/www/html/public
encode gzip
php_fastcgi 127.0.0.1:9000
file_server
@notStatic {
not file {path} {path}/
}
rewrite @notStatic /index.php
}
+50
View File
@@ -0,0 +1,50 @@
FROM php:8.2-fpm-alpine
# Install system dependencies
RUN apk add --no-cache \
git curl unzip icu-dev libzip-dev libpng-dev libjpeg-turbo-dev \
freetype-dev libxml2-dev oniguruma-dev linux-headers \
imagemagick-dev $PHPIZE_DEPS caddy \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
bcmath ctype fileinfo gd intl mbstring \
pdo pdo_mysql opcache pcntl sockets zip xml \
&& pecl install redis imagick \
&& docker-php-ext-enable redis imagick \
&& apk del $PHPIZE_DEPS linux-headers \
&& rm -rf /tmp/*
# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www/html
# Copy application code
COPY . .
# Install Laravel dependencies
RUN php /usr/bin/composer install \
--no-dev --optimize-autoloader --no-interaction --no-progress \
--no-scripts --ignore-platform-req=ext-ssh2
# Create required directories and set permissions
RUN mkdir -p \
storage/framework/sessions \
storage/framework/views \
storage/framework/cache/data \
storage/logs \
bootstrap/cache \
&& chown -R www-data:www-data storage bootstrap/cache \
&& chmod -R 775 storage bootstrap/cache
# Caddy config for Laravel
COPY docker/Caddyfile /etc/caddy/Caddyfile
# Entrypoint
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
EXPOSE 8088
ENTRYPOINT ["/entrypoint.sh"]
+38
View File
@@ -0,0 +1,38 @@
#!/bin/sh
set -e
cd /var/www/html
# Wait for MySQL to be ready (TCP check)
if [ -n "$DB_HOST" ]; then
echo "Waiting for database at $DB_HOST:${DB_PORT:-3306}..."
until php -r "new PDO('mysql:host=$DB_HOST;port=${DB_PORT:-3306};dbname=$DB_DATABASE', '$DB_USERNAME', '$DB_PASSWORD');" 2>/dev/null; do
sleep 2
done
echo "Database is ready."
fi
# Ensure Laravel storage directories exist
mkdir -p storage/framework/sessions \
storage/framework/views \
storage/framework/cache/data \
storage/logs \
bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache 2>/dev/null || true
chmod -R 775 storage bootstrap/cache
# Run composer post-install scripts (skipped during build)
php /usr/bin/composer dump-autoload --optimize --no-dev 2>/dev/null || true
php artisan package:discover --ansi 2>/dev/null || true
# Laravel optimizations
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan storage:link --force 2>/dev/null || true
# Start PHP-FPM in background
php-fpm -D
# Start Caddy in foreground
exec caddy run --config /etc/caddy/Caddyfile --adapter caddyfile