-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.production
More file actions
147 lines (118 loc) · 4.63 KB
/
Copy pathDockerfile.production
File metadata and controls
147 lines (118 loc) · 4.63 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
139
140
141
142
143
144
145
146
147
# Cbox Production Multi-Stage Build Template
# Optimized for Laravel/Symfony production deployments
#
# Features:
# - Multi-stage build (frontend assets built separately)
# - Minimal final image (no dev dependencies)
# - Pre-cached Laravel/Symfony configurations
# - Security-hardened (non-root ready)
# - Optimized for both AMD64 and ARM64
# =============================================================================
# ARG declarations (customize these)
# =============================================================================
ARG PHP_VERSION=8.4
ARG NODE_VERSION=20
# =============================================================================
# Stage 1: Frontend Assets Builder
# =============================================================================
FROM node:${NODE_VERSION}-alpine AS frontend-builder
WORKDIR /build
# Install frontend dependencies (cached layer)
COPY package*.json ./
RUN npm ci --prefer-offline --no-audit
# Copy frontend source files
COPY resources/ ./resources/
COPY vite.config.* ./
COPY tsconfig.json* ./
COPY tailwind.config.* ./
COPY postcss.config.* ./
# Build production assets
RUN npm run build
# =============================================================================
# Stage 2: Composer Dependencies
# =============================================================================
FROM composer:2 AS composer-builder
WORKDIR /build
# Copy composer files for dependency caching
COPY composer.json composer.lock ./
# Install production dependencies only
RUN composer install \
--no-dev \
--no-scripts \
--no-autoloader \
--prefer-dist \
--ignore-platform-reqs
# Copy application code for autoloader optimization
COPY . .
# Generate optimized autoloader
RUN composer dump-autoload --optimize --classmap-authoritative
# =============================================================================
# Stage 3: Production Runtime
# =============================================================================
FROM ghcr.io/cboxdk/php-baseimages/php-fpm-nginx:${PHP_VERSION}-bookworm AS production
LABEL maintainer="Cbox <hello@cbox.com>"
LABEL description="Production-optimized PHP application"
WORKDIR /var/www/html
# Copy Composer dependencies from builder
COPY --from=composer-builder /build/vendor ./vendor
# Copy built frontend assets from builder
COPY --from=frontend-builder /build/public/build ./public/build
# Copy application code with correct ownership
COPY --chown=www-data:www-data . .
# Ensure storage and cache directories exist with correct permissions
RUN mkdir -p \
storage/app/public \
storage/framework/cache/data \
storage/framework/sessions \
storage/framework/views \
storage/logs \
bootstrap/cache && \
chown -R www-data:www-data storage bootstrap/cache && \
chmod -R 775 storage bootstrap/cache
# Laravel: Cache configuration, routes, and views for production
# Uncomment for Laravel applications:
# RUN php artisan config:cache && \
# php artisan route:cache && \
# php artisan view:cache && \
# php artisan event:cache
# Symfony: Clear and warm up cache for production
# Uncomment for Symfony applications:
# RUN php bin/console cache:clear --env=prod --no-debug && \
# php bin/console cache:warmup --env=prod --no-debug
# Production environment
ENV APP_ENV=production
ENV APP_DEBUG=false
ENV LOG_CHANNEL=stderr
# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost/health || exit 1
EXPOSE 80
# =============================================================================
# Stage 4: Development (optional, for local development)
# =============================================================================
FROM ghcr.io/cboxdk/php-baseimages/php-fpm-nginx:${PHP_VERSION}-bookworm-dev AS development
WORKDIR /var/www/html
# Development-specific configuration
ENV APP_ENV=local
ENV APP_DEBUG=true
ENV XDEBUG_MODE=debug,develop,coverage
# Expose additional ports for development
EXPOSE 80 5173 9003
# =============================================================================
# Usage Examples:
# =============================================================================
#
# Build production image:
# docker build --target production -t myapp:prod .
#
# Build development image:
# docker build --target development -t myapp:dev .
#
# Build with specific PHP version:
# docker build --build-arg PHP_VERSION=8.3 --target production -t myapp:prod .
#
# Multi-platform build (AMD64 + ARM64):
# docker buildx build --platform linux/amd64,linux/arm64 \
# --target production -t myapp:prod --push .
#
# =============================================================================