Skip to content

Commit a9a69c8

Browse files
Simplify multi-stage build with composer-deps stage
Co-authored-by: delicatacurtis <247246500+delicatacurtis@users.noreply.github.com>
1 parent 9f352c3 commit a9a69c8

File tree

1 file changed

+26
-63
lines changed

1 file changed

+26
-63
lines changed

Dockerfile

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
# Accepted values: 8.3 - 8.2
22
ARG PHP_VERSION=8.3
3-
ARG NODE_VERSION=20
43

54
###########################################
6-
# Dependencies stage - Install Composer dependencies
5+
# Composer dependencies stage
76
###########################################
8-
FROM php:${PHP_VERSION}-cli-alpine AS dependencies
7+
FROM composer:latest AS composer-deps
98

109
WORKDIR /app
1110

12-
# Install Composer
13-
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
14-
15-
# Install minimal PHP extensions needed for composer
16-
RUN apk add --no-cache libzip-dev && docker-php-ext-install zip
17-
1811
# Copy composer files
1912
COPY composer.json composer.lock ./
2013

21-
# Install composer dependencies
14+
# Install composer dependencies (no autoloader yet, will optimize in final stage)
2215
RUN composer install \
2316
--no-dev \
2417
--no-interaction \
@@ -27,37 +20,6 @@ RUN composer install \
2720
--no-scripts \
2821
--prefer-dist
2922

30-
# Copy application code for autoloader
31-
COPY . .
32-
33-
# Generate optimized autoloader
34-
RUN composer dump-autoload --classmap-authoritative --no-dev
35-
36-
###########################################
37-
# Node.js build stage for frontend assets
38-
###########################################
39-
FROM node:${NODE_VERSION}-slim AS node-builder
40-
41-
WORKDIR /app
42-
43-
# Copy package files
44-
COPY package.json package-lock.json ./
45-
46-
# Install npm dependencies
47-
RUN npm install
48-
49-
# Copy necessary files for building
50-
COPY vite.config.js postcss.config.cjs tailwind.config.js* ./
51-
COPY resources ./resources
52-
COPY public ./public
53-
54-
# Copy vendor directory from dependencies stage (needed for Filament theme)
55-
COPY --from=dependencies /app/vendor ./vendor
56-
COPY --from=dependencies /app/app ./app
57-
58-
# Build frontend assets
59-
RUN npm run build
60-
6123
###########################################
6224
# Main application stage
6325
###########################################
@@ -91,8 +53,9 @@ RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime \
9153

9254
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
9355

94-
RUN apk update; \
95-
apk upgrade; \
56+
# Install system dependencies and PHP extensions in one layer
57+
RUN apk update && \
58+
apk upgrade && \
9659
apk add --no-cache \
9760
curl \
9861
wget \
@@ -101,9 +64,8 @@ RUN apk update; \
10164
procps \
10265
ca-certificates \
10366
supervisor \
104-
libsodium-dev \
105-
# Install PHP extensions
106-
&& install-php-extensions \
67+
libsodium-dev && \
68+
install-php-extensions \
10769
bz2 \
10870
pcntl \
10971
mbstring \
@@ -122,9 +84,9 @@ RUN apk update; \
12284
memcached \
12385
igbinary \
12486
ldap \
125-
swoole \
126-
&& docker-php-source delete \
127-
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/*
87+
swoole && \
88+
docker-php-source delete && \
89+
rm -rf /var/cache/apk/* /tmp/* /var/tmp/*
12890

12991
RUN arch="$(apk --print-arch)" \
13092
&& case "$arch" in \
@@ -151,44 +113,45 @@ RUN cp ${PHP_INI_DIR}/php.ini-production ${PHP_INI_DIR}/php.ini
151113

152114
USER ${USER}
153115

154-
# Install Composer
116+
# Install Composer from official image
155117
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
156118

119+
# Copy vendor from composer-deps stage for better caching
120+
COPY --chown=${USER}:${USER} --from=composer-deps /app/vendor ./vendor
121+
157122
# Copy application code
158123
COPY --chown=${USER}:${USER} . .
159124

160-
# Copy vendor dependencies from dependencies stage
161-
COPY --chown=${USER}:${USER} --from=dependencies /app/vendor ./vendor
162-
163-
# Copy built frontend assets from node-builder stage
164-
COPY --chown=${USER}:${USER} --from=node-builder /app/public/build ./public/build
165-
125+
# Create necessary Laravel directories
166126
RUN mkdir -p \
167127
storage/framework/sessions \
168128
storage/framework/views \
169129
storage/framework/cache \
170130
storage/framework/testing \
171131
storage/logs \
172-
bootstrap/cache && chmod -R a+rw storage
132+
bootstrap/cache && \
133+
chmod -R a+rw storage
173134

135+
# Copy configuration files
174136
COPY --chown=${USER}:${USER} .docker/supervisord.conf /etc/supervisor/
175137
COPY --chown=${USER}:${USER} .docker/octane/Swoole/supervisord.swoole.conf /etc/supervisor/conf.d/
176138
COPY --chown=${USER}:${USER} .docker/supervisord.*.conf /etc/supervisor/conf.d/
177139
COPY --chown=${USER}:${USER} .docker/php.ini ${PHP_INI_DIR}/conf.d/99-octane.ini
178140
COPY --chown=${USER}:${USER} .docker/start-container /usr/local/bin/start-container
179141

180-
# Generate optimized autoloader (vendor is already installed from dependencies stage)
181-
RUN composer dump-autoload --classmap-authoritative --no-dev \
182-
&& composer clear-cache
142+
# Generate optimized autoloader
143+
RUN composer dump-autoload --classmap-authoritative --no-dev && \
144+
composer clear-cache
183145

146+
# Copy environment file
184147
COPY --chown=${USER}:${USER} .env.example ./.env
185148

186-
RUN chmod +x /usr/local/bin/start-container
187-
188-
RUN cat .docker/utilities.sh >> ~/.bashrc
149+
RUN chmod +x /usr/local/bin/start-container && \
150+
cat .docker/utilities.sh >> ~/.bashrc
189151

190152
EXPOSE 8000
191153

192154
ENTRYPOINT ["start-container"]
193155

194156
HEALTHCHECK --start-period=5s --interval=2s --timeout=5s --retries=8 CMD php artisan octane:status || exit 1
157+

0 commit comments

Comments
 (0)