Skip to content

Commit d3ac722

Browse files
1. Fix possible Autoload Bug by using ACPU optimization over Authoritative Classmap.
2. In Development, use the mounted `vendor` folder instead of having to reinstall everything when introducing a new package 📦 3. Move Composer's Post-Install, and Post-Autoload command to build time, added an entry to FAQ to how to fix build failure at post-install-cmd in most frameworks.
1 parent 4c7571d commit d3ac722

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

Dockerfile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ RUN apt-get update && apt-get -y --no-install-recommends install \
2626
tini=0.18.0-1 \
2727
libfcgi-bin=2.4.0-10 \
2828
libicu-dev=63.1-6+deb10u1 \
29+
gettext-base \
2930
# Needed for Application Runtime
3031

3132
# Clean metadata and clear caches
@@ -125,7 +126,7 @@ RUN composer config platform.php ${PHP_VERSION}
125126
# Install Dependeinces
126127
## * Platform requirments are checked at the later steps.
127128
## * Scripts and Autoload are run at later steps.
128-
RUN composer install -n --no-progress --ignore-platform-reqs --no-plugins --no-scripts --no-autoloader --prefer-dist
129+
RUN composer install -n --no-progress --ignore-platform-reqs --no-plugins --no-scripts --no-dev --no-autoloader --prefer-dist
129130

130131
# ======================================================================================================================
131132
# ============================================== PRODUCTION IMAGE ====================================================
@@ -143,10 +144,10 @@ COPY --chown=www-data:www-data --from=vendor /app/vendor /var/www/app/vendor
143144
COPY --chown=www-data:www-data . .
144145

145146
# 1. Dump optimzed autoload for vendor and app classes.
146-
# 2. --no-scripts as scripts are run on runtime via entrypoint.
147-
# 3. checks that PHP and extensions versions match the platform requirements of the installed packages.
148-
RUN composer dump-autoload -n --optimize --no-scripts --no-dev --classmap-authoritative && \
147+
# 2. checks that PHP and extensions versions match the platform requirements of the installed packages.
148+
RUN composer dump-autoload -n --optimize --no-dev --apcu && \
149149
composer check-platform-reqs && \
150+
composer run-script -n post-install-cmd && \
150151
post-build
151152

152153
ENTRYPOINT ["entrypoint-prod"]
@@ -189,14 +190,6 @@ COPY docker/php/dev-* $PHP_INI_DIR/conf.d/
189190
# Run as non-root
190191
USER www-data
191192

192-
# Copy Vendor And Generate Autoload ( Needs Composer.* to check reqs and generate autoload)
193-
COPY --chown=www-data:www-data composer.json composer.json
194-
COPY --chown=www-data:www-data composer.lock composer.lock
195-
COPY --chown=www-data:www-data --from=vendor /app/vendor /var/www/app/vendor
196-
RUN composer install --no-scripts && composer dump-autoload -n --no-scripts && \
197-
composer check-platform-reqs && \
198-
post-build
199-
200193
# Entrypoints
201194
ENTRYPOINT ["entrypoint-dev"]
202195
CMD ["php-fpm"]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ In `docker/` directory there is `post-build` and `post-install` scripts that are
206206
2. Why `debian` based image not `alpine` ?
207207
208208
1. While a smaller image is very desired, and PHP image is infamous of being big. Alpine lacks a lot of packages (Available via `apk`) that a typical PHP would need. Some packages are not even available for alpine as they link to glibc not musl.
209+
2. The image is `alpine` compatible, even the entrypoint and helper scripts are `sh` compatible, modifying the image to use `alpine` variant is possible with minimal changes to the image, you'll need to install the same package you're already using but for `alpine` and using `apk`.
210+
3. Image Build Fails as it try to connect to DB.
211+
212+
- A typical application in most Frameworks comes with `Doctrine` ORM, Doctrine if not configured with a DB Version, will try to access the DB at php's script initialization (even at the post-install cmd's), and it will fail when it cannot connect to DB. Make sure you configure doctrine to avoid this extra DB Check connection.
209213
210214
211215
# License

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ services:
1111
COMPOSER_AUTH: "{}"
1212
restart: unless-stopped
1313
volumes:
14-
- /var/www/app/vendor
1514
- .:/var/www/app
1615

1716
web:

docker/entrypoints/entrypoint-base

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
#!/bin/sh
22
set -eu
33

4-
5-
64
# --------------------------------------------------------------------------------------------------------------------
75

86
# !-- We want to only print in logs if something wrong happened, hence how below code is written. --!
97

10-
# Run post-install-cmd (if exists)
11-
if (composer run-script -l 2> /dev/null | command grep --silent "post-install-cmd" > /dev/null)
12-
then
13-
echo "► Running composer's post-install-cmd..."
14-
output=$(composer run-script --no-interaction post-install-cmd 2>&1) || { echo "error running composer post-install-cmd script: $output"; exit 1; }
15-
echo "✔ Successful"
16-
fi
17-
18-
# Run post-autoload-dump (if exists)
19-
if (composer run-script -l 2> /dev/null | command grep --silent "post-autoload-dump" > /dev/null)
20-
then
21-
echo "► Running composer's post-autoload-dump..."
22-
output=$(composer run-script --no-interaction post-autoload-dump 2>&1) || { echo "error running composer post-autoload-dump script: $output"; exit 1; }
23-
echo "✔ Successful"
24-
fi
25-
268
# Run custom ad-hoc pre-run script
279
echo "► Running custom pre-run script..."
28-
output=$(pre-run 2>&1) || { echo "error running custom pre-run script: $output"; exit 1; }
10+
pre-run
2911
echo "✔ Successful"
3012

3113
# ----------------------------------------------------------------------------------------------------------------------

docker/entrypoints/entrypoint-dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ set -eu
33

44
# ----------------------------------------------------------------------------------------------------------------------
55

6+
echo "Checking if 'vendor' folder exists..."
7+
if [ -d "vendor" ]; then
8+
echo "'Vendor' Folder exists, Skipping running Composer Install."
9+
echo "To install new package, either run 'composer install' from inside the container via 'exec', or remove the vendor folder all together."
10+
else
11+
echo "Vendor Folder does not exist, running composer install..."
12+
composer install -n
13+
fi
614

715
# Run Entrypoint and pass CMD to it (Don't forget exec)
816
exec entrypoint-base "${@}"

0 commit comments

Comments
 (0)