@@ -4,6 +4,8 @@ ARG NGINX_VERSION="1.17.4"
4
4
ARG COMPOSER_VERSION="2.0"
5
5
ARG XDEBUG_VERSION="3.0.3"
6
6
ARG COMPOSER_AUTH
7
+ ARG IMAGE_DEPS="fcgi tini icu-dev gettext curl"
8
+ ARG RUNTIME_DEPS="zip"
7
9
8
10
# -------------------------------------------------- Composer Image ----------------------------------------------------
9
11
@@ -14,39 +16,44 @@ FROM composer:${COMPOSER_VERSION} as composer
14
16
# --------------- This stage install needed extenstions, plugins and add all needed configurations -------------------
15
17
# ======================================================================================================================
16
18
17
- FROM php:${PHP_VERSION}-fpm AS base
19
+ FROM php:${PHP_VERSION}-fpm-alpine AS base
20
+
21
+ # Required Args ( inherited from start of file, or passed at build )
22
+ ARG IMAGE_DEPS
23
+ ARG RUNTIME_DEPS
18
24
19
25
# Maintainer label
20
26
LABEL maintainer="sherifabdlnaby@gmail.com"
21
27
22
28
# ------------------------------------- Install Packages Needed Inside Base Image --------------------------------------
23
29
24
- RUN apt-get update && apt-get -y --no-install-recommends install \
25
- # Needed for Image
26
- tini=0.18.0-1 \
27
- libfcgi-bin=2.4.0-10 \
28
- libicu-dev=63.1-6+deb10u1 \
29
- gettext-base \
30
- # Needed for Application Runtime
31
-
32
- # Clean metadata and clear caches
33
- && apt-get autoremove --purge -y && apt-get clean \
34
- && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
30
+ RUN apk add --no-cache ${IMAGE_DEPS} ${RUNTIME_DEPS}
35
31
36
32
# ---------------------------------------- Install / Enable PHP Extensions ---------------------------------------------
37
33
38
- # - base image has helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to
39
- # more easily install PHP extensions.
34
+ # # Needed to add Extensions to PHP ( will be deleted after install PHP Extenstions )
35
+ RUN apk add --virtual .buildtime-deps ${PHPIZE_DEPS} \
36
+ # install PHP Extensions
40
37
# head to: https://github.com/docker-library/docs/tree/master/php#how-to-install-more-php-extensions
41
38
# EX: RUN docker-php-ext-install curl pdo pdo_mysql mysqli
42
39
# EX: RUN pecl install memcached && docker-php-ext-enable memcached
43
- RUN docker-php-ext-install -j$(nproc) \
40
+ && docker-php-ext-install -j$(nproc) \
44
41
opcache \
45
42
intl \
46
- pdo_mysql
47
- # Pecl Extentions
48
- RUN pecl install apcu-5.1.20 && docker-php-ext-enable apcu
43
+ pdo_mysql \
44
+ # Pecl Extentions
49
45
# EX: RUN pecl install memcached && docker-php-ext-enable memcached
46
+ && pecl install apcu-5.1.20 \
47
+ && docker-php-ext-enable apcu \
48
+ # Delete buildtime-deps
49
+ && apk del -f .buildtime-deps
50
+
51
+ # ------------------------------------------------- Permissions --------------------------------------------------------
52
+
53
+ # - Clean bundled config/users & recreate them with UID 1000 for docker compatability in dev container.
54
+ # - Create composer directories (since we run as non-root later)
55
+ RUN deluser --remove-home www-data && adduser -u1000 -D www-data && rm -rf /var/www /usr/local/etc/php-fpm.d/* && \
56
+ mkdir -p /var/www/.composer /app && chown -R www-data:www-data /app /var/www/.composer
50
57
51
58
# ------------------------------------------------ PHP Configuration ---------------------------------------------------
52
59
@@ -58,45 +65,39 @@ COPY docker/php/base-* $PHP_INI_DIR/conf.d
58
65
59
66
# ---------------------------------------------- PHP FPM Configuration -------------------------------------------------
60
67
61
- # Clean bundled config & create composer directories (since we run as non-root later)
62
- RUN usermod -u 1000 www-data && rm -rf /var/www /usr/local/etc/php-fpm.d/* && \
63
- mkdir -p /var/www/.composer /var/www/app && chown -R www-data:www-data /var/www/app /var/www/.composer
68
+ # PHP-FPM config
69
+ COPY docker/fpm/*.conf /usr/local/etc/php-fpm.d/
64
70
65
- # Copy scripts and PHP-FPM config
66
- COPY docker/fpm/*.conf /usr/local/etc/php-fpm.d/
67
71
68
72
# --------------------------------------------------- Scripts ----------------------------------------------------------
69
73
70
- COPY docker/entrypoints /usr/local/bin/
71
- COPY docker/healthcheck /usr/local/bin/
72
- COPY docker/post-build /usr/local/bin/
73
- COPY docker/pre-run /usr/local/bin/
74
- COPY docker/fpm/fpm-healthcheck /usr/local/bin/
75
- RUN chmod +x /usr/local/bin/entrypoint-* /usr/local/bin/post-build /usr/local/bin/pre-run /usr/local/bin/*healthcheck
74
+ COPY docker/*-base \
75
+ docker/healthcheck-* \
76
+ docker/command-loop \
77
+ # to
78
+ /usr/local/bin/
79
+
80
+ RUN chmod +x /usr/local/bin/*-base /usr/local/bin/healthcheck-* /usr/local/bin/command-loop
76
81
77
82
# ---------------------------------------------------- Composer --------------------------------------------------------
78
83
79
84
COPY --from=composer /usr/bin/composer /usr/bin/composer
80
85
81
- # ----------------------------------------------- NON-ROOT SWITCH ------------------------------------------------------
86
+ # ----------------------------------------------------- MISC ----- ------------------------------------------------------
82
87
88
+ WORKDIR /app
83
89
USER www-data
84
90
85
- # ----------------------------------------------------- MISC -----------------------------------------------------------
86
-
87
- WORKDIR /var/www/app
91
+ # Common PHP Frameworks Env Variables
88
92
ENV APP_ENV prod
89
93
ENV APP_DEBUG 0
90
94
91
- # Run as non-root
92
- USER www-data
93
-
94
- # Validate FPM config
95
+ # Validate FPM config (must use the non-root user)
95
96
RUN php-fpm -t
96
97
97
98
# ---------------------------------------------------- HEALTH ----------------------------------------------------------
98
99
99
- HEALTHCHECK CMD ["healthcheck" ]
100
+ HEALTHCHECK CMD ["healthcheck-liveness " ]
100
101
101
102
# -------------------------------------------------- ENTRYPOINT --------------------------------------------------------
102
103
@@ -124,9 +125,7 @@ COPY composer.lock composer.lock
124
125
RUN composer config platform.php ${PHP_VERSION}
125
126
126
127
# Install Dependeinces
127
- # # * Platform requirments are checked at the later steps.
128
- # # * Scripts and Autoload are run at later steps.
129
- RUN composer install -n --no-progress --ignore-platform-reqs --no-plugins --no-scripts --no-dev --no-autoloader --prefer-dist
128
+ RUN composer install -n --no-progress --ignore-platform-reqs --no-dev --prefer-dist --no-scripts --no-autoloader
130
129
131
130
# ======================================================================================================================
132
131
# ============================================== PRODUCTION IMAGE ====================================================
@@ -135,20 +134,28 @@ RUN composer install -n --no-progress --ignore-platform-reqs --no-plugins --no-s
135
134
136
135
FROM base AS app
137
136
137
+ USER root
138
+
139
+ # Copy Prod Scripts
140
+ COPY docker/*-prod /usr/local/bin/
141
+ RUN chmod +x /usr/local/bin/*-prod
142
+
143
+ # Copy PHP Production Configuration
138
144
COPY docker/php/prod-* $PHP_INI_DIR/conf.d/
139
145
146
+ USER www-data
147
+
148
+ # ----------------------------------------------- Production Config -----------------------------------------------------
149
+
140
150
# Copy Vendor
141
- COPY --chown=www-data:www-data --from=vendor /app/vendor /var/www/ app/vendor
151
+ COPY --chown=www-data:www-data --from=vendor /app/vendor /app/vendor
142
152
143
153
# Copy App Code
144
154
COPY --chown=www-data:www-data . .
145
155
146
- # 1. Dump optimzed autoload for vendor and app classes.
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 && \
149
- composer check-platform-reqs && \
150
- composer run-script -n post-install-cmd && \
151
- post-build
156
+ # Run Composer Install again
157
+ # ( this time to run post-install scripts, autoloader, and post-autoload scripts using one command )
158
+ RUN post-build-base && post-build-prod
152
159
153
160
ENTRYPOINT ["entrypoint-prod" ]
154
161
CMD ["php-fpm" ]
@@ -164,31 +171,28 @@ ARG XDEBUG_VERSION
164
171
ENV APP_ENV dev
165
172
ENV APP_DEBUG 1
166
173
167
- # Switch to root to install stuff
174
+ # Switch root to install stuff
168
175
USER root
169
176
170
- # Packages
171
- RUN apt-get update && apt-get -y --no-install-recommends install \
172
- # Needed for Dev luxery when you shell inside the container for debugging
173
- curl \
174
- htop \
175
- dnsutils \
176
- && apt-get autoremove --purge -y && apt-get clean \
177
- && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
178
-
179
177
# ---------------------------------------------------- Xdebug ----------------------------------------------------------
180
178
181
- RUN pecl install xdebug-${XDEBUG_VERSION} && docker-php-ext-enable xdebug
179
+ RUN apk add --virtual .buildtime-deps ${PHPIZE_DEPS} \
180
+ && pecl install xdebug-${XDEBUG_VERSION} && docker-php-ext-enable xdebug \
181
+ && apk del -f .buildtime-deps
182
+
183
+ # ---------------------------------------- ---------- Scripts ---------------------------------------------------------
184
+
185
+ # Copy Dev Scripts
186
+ COPY docker/*-dev /usr/local/bin/
187
+ RUN chmod +x /usr/local/bin/*-dev
182
188
183
189
# ------------------------------------------------------ PHP -----------------------------------------------------------
184
190
185
191
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
186
192
COPY docker/php/dev-* $PHP_INI_DIR/conf.d/
187
193
188
- # ------------------------------------------------- Entry Point --------------------------------------------------------
189
-
190
- # Run as non-root
191
194
USER www-data
195
+ # ------------------------------------------------- Entry Point --------------------------------------------------------
192
196
193
197
# Entrypoints
194
198
ENTRYPOINT ["entrypoint-dev" ]
@@ -203,25 +207,21 @@ CMD ["php-fpm"]
203
207
FROM nginx:${NGINX_VERSION}-alpine AS nginx
204
208
205
209
RUN rm -rf /var/www/* /etc/nginx/conf.d/* && adduser -u 1000 -D -S -G www-data www-data
206
-
207
210
COPY docker/nginx/nginx-* /usr/local/bin/
208
211
COPY docker/nginx/ /etc/nginx/
209
- RUN chmod +x /usr/local/bin/nginx-*
212
+ RUN chown -R www-data /etc/nginx/ && chmod +x /usr/local/bin/nginx-*
210
213
211
214
# The PHP-FPM Host
212
215
# # Localhost is the sensible default assuming image run on a k8S Pod
213
216
ENV PHP_FPM_HOST "localhost"
214
217
ENV PHP_FPM_PORT "9000"
215
218
216
- # Allow Nginx to run as non-root.
217
- RUN chown -R www-data:www-data /var/cache/nginx /etc/nginx/ /etc/nginx/conf.d/
218
-
219
- # Change to non root user
220
- USER www-data
221
-
222
219
# For Documentation
223
220
EXPOSE 8080
224
221
222
+ # Switch User
223
+ USER www-data
224
+
225
225
# Add Healthcheck
226
226
HEALTHCHECK CMD ["nginx-healthcheck" ]
227
227
@@ -235,8 +235,7 @@ ENTRYPOINT ["nginx-entrypoint"]
235
235
FROM nginx AS web
236
236
237
237
# Copy Public folder + Assets that's going to be served from Nginx
238
- COPY public /var/www/app/public
239
-
238
+ COPY --chown=www-data:www-data --from=app /app/public /app/public
240
239
241
240
# ----------------------------------------------------- NGINX ----------------------------------------------------------
242
241
FROM nginx AS web-dev
0 commit comments