-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
138 lines (114 loc) · 4.76 KB
/
Copy pathDockerfile
File metadata and controls
138 lines (114 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# =============================================================================
FROM php:8.2-apache
# Set working directory
WORKDIR /var/www/html
# =============================================================================
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
curl \
unzip \
libpq-dev \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
libonig-dev \
libxml2-dev \
libzip-dev \
zip \
# Install Node.js 20.x LTS (better than default apt nodejs)
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs \
# Clean up
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Verify Node.js and npm installation
RUN node --version && npm --version
# Configure and install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
pdo \
pdo_pgsql \
pgsql \
mbstring \
exif \
pcntl \
bcmath \
gd \
zip \
opcache
# Enable Apache modules
RUN a2enmod rewrite headers
# Set Apache DocumentRoot to Laravel's public folder
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
# Update Apache configuration
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
# Configure Apache for Laravel (.htaccess support)
RUN echo '<Directory /var/www/html/public>\n\
Options Indexes FollowSymLinks\n\
AllowOverride All\n\
Require all granted\n\
</Directory>' > /etc/apache2/conf-available/laravel.conf \
&& a2enconf laravel
# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Copy application files
COPY . .
# Create required directories and set permissions BEFORE composer install
RUN mkdir -p bootstrap/cache \
&& mkdir -p storage/framework/{sessions,views,cache} \
&& mkdir -p storage/logs \
&& chmod -R 775 bootstrap/cache \
&& chmod -R 775 storage
# Install PHP dependencies (production)
RUN composer install \
--no-dev \
--optimize-autoloader \
--no-interaction \
--prefer-dist \
--no-progress
# Ensure devDependencies are installed during build so Vite is available
ENV NODE_ENV=development
# Install dependencies and build assets (ensure manifest is generated)
RUN export NPM_CONFIG_PRODUCTION=false \
&& echo "Node: $(node --version) npm: $(npm --version)" \
&& npm ci --no-audit --no-fund --include=dev --silent \
&& npm run build --silent || (echo "ERROR: npm run build failed"; echo "node_modules listing:"; ls -la node_modules || true; echo "build dir listing:"; ls -la public || true; exit 1) \
# Handle Vite 7+ writing nested .vite/manifest.json - copy it to public/build/manifest.json when present
&& if [ -f public/build/.vite/manifest.json ] && [ ! -f public/build/manifest.json ]; then echo "Found .vite/manifest.json -> copying to public/build/manifest.json"; cp public/build/.vite/manifest.json public/build/manifest.json; echo "Manifest content:"; head -n 5 public/build/manifest.json || true; fi \
&& if [ ! -f public/build/manifest.json ]; then echo "ERROR: Vite manifest missing"; ls -la public/build || true; exit 1; fi
# Set production environment (optional - keeps environment cleaned for PHP runtime)
ENV NODE_ENV=production
# Remove node_modules after build to reduce image size
RUN rm -rf node_modules
# Configure PHP for production
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# Configure OPcache for maximum performance
RUN echo 'opcache.enable=1\n\
opcache.memory_consumption=256\n\
opcache.interned_strings_buffer=64\n\
opcache.max_accelerated_files=32531\n\
opcache.validate_timestamps=0\n\
opcache.save_comments=1\n\
opcache.fast_shutdown=1' > /usr/local/etc/php/conf.d/opcache.ini
# Configure PHP upload limits
RUN echo 'upload_max_filesize=10M\n\
post_max_size=12M\n\
memory_limit=256M\n\
max_execution_time=60' > /usr/local/etc/php/conf.d/uploads.ini
# Set permissions for Laravel
RUN chown -R www-data:www-data /var/www/html \
&& chmod -R 755 /var/www/html/storage \
&& chmod -R 755 /var/www/html/bootstrap/cache
# Create storage link
RUN php artisan storage:link || true
RUN php artisan view:clear || true
RUN php artisan config:clear || true
# Expose port 80
EXPOSE 80
# Copy and install entrypoint script that copies manifest and clears caches at startup
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh && chmod +x /usr/local/bin/docker-entrypoint.sh
# Start the container through the entrypoint so startup tasks run first
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["apache2-foreground"]