-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
132 lines (111 loc) · 4.08 KB
/
Dockerfile
File metadata and controls
132 lines (111 loc) · 4.08 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
# Multi-stage Dockerfile for Laravel Web Application (Production)
# Stage 1: Build stage
FROM php:8.2-fpm-alpine AS builder
# Install system dependencies and PHP extensions
RUN apk add --no-cache \
build-base \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
libzip-dev \
zip \
unzip \
curl \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
bcmath \
gd \
pdo_mysql \
zip \
pcntl \
posix \
opcache \
exif \
&& rm -rf /var/cache/apk/*
# Install Composer
COPY --from=composer:2.6 /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
# Copy composer files
COPY composer.json composer.lock ./
# Install PHP dependencies (production optimized)
RUN composer install --no-dev --no-scripts --optimize-autoloader
# Copy application code
COPY . .
# Run composer scripts and generate optimized autoloader for production
RUN composer run-script post-autoload-dump --no-dev
RUN composer dump-autoload --optimize --classmap-authoritative --no-dev
# Set proper permissions
RUN chown -R www-data:www-data /var/www \
&& chmod -R 755 /var/www/storage \
&& chmod -R 755 /var/www/bootstrap/cache
# Stage 2: Production stage
FROM php:8.2-fpm-alpine AS production
# Install runtime dependencies and PHP extensions
RUN apk add --no-cache \
freetype \
libjpeg-turbo \
libpng \
libzip \
curl \
&& apk add --no-cache --virtual .build-deps \
build-base \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
libzip-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) \
bcmath \
gd \
pdo_mysql \
zip \
pcntl \
posix \
opcache \
exif \
&& apk del .build-deps \
&& rm -rf /var/cache/apk/*
# Production OPcache configuration
RUN echo 'opcache.memory_consumption=128' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.interned_strings_buffer=8' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.max_accelerated_files=4000' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.revalidate_freq=2' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.fast_shutdown=1' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.enable_cli=0' >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo 'opcache.validate_timestamps=0' >> /usr/local/etc/php/conf.d/opcache.ini
# Production PHP configuration
RUN echo 'memory_limit=256M' >> /usr/local/etc/php/conf.d/php.ini \
&& echo 'max_execution_time=30' >> /usr/local/etc/php/conf.d/php.ini \
&& echo 'max_input_vars=3000' >> /usr/local/etc/php/conf.d/php.ini \
&& echo 'post_max_size=32M' >> /usr/local/etc/php/conf.d/php.ini \
&& echo 'upload_max_filesize=32M' >> /usr/local/etc/php/conf.d/php.ini \
&& echo 'expose_php=Off' >> /usr/local/etc/php/conf.d/php.ini
# Set working directory
WORKDIR /var/www
# Copy application from builder stage
COPY --from=builder --chown=www-data:www-data /var/www /var/www
# Copy scripts and entrypoint
COPY scripts/ /var/www/scripts/
RUN chmod +x /var/www/scripts/*.sh
# Copy entrypoint script from scripts folder
COPY scripts/docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Create necessary directories
RUN mkdir -p /var/www/storage/logs \
&& mkdir -p /var/www/storage/framework/cache \
&& mkdir -p /var/www/storage/framework/sessions \
&& mkdir -p /var/www/storage/framework/views \
&& mkdir -p /var/www/bootstrap/cache \
&& chown -R www-data:www-data /var/www \
&& chmod -R 755 /var/www/storage \
&& chmod -R 755 /var/www/bootstrap/cache
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=60s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:8000/api/healthz || exit 1
# Set entrypoint
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
# Start Laravel's built-in server
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]