Skip to content

Commit e9d6154

Browse files
Merge pull request #11 | 4.0 Release ( Project Rename, Alpine Images Switch, and More Optimizations )
4.0 Release ( Project Rename, Alpine Images Switch, and More Optimizations )
2 parents 8b56fe6 + 56c80f9 commit e9d6154

25 files changed

+340
-234
lines changed

Dockerfile

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ ARG NGINX_VERSION="1.17.4"
44
ARG COMPOSER_VERSION="2.0"
55
ARG XDEBUG_VERSION="3.0.3"
66
ARG COMPOSER_AUTH
7+
ARG IMAGE_DEPS="fcgi tini icu-dev gettext curl"
8+
ARG RUNTIME_DEPS="zip"
79

810
# -------------------------------------------------- Composer Image ----------------------------------------------------
911

@@ -14,39 +16,44 @@ FROM composer:${COMPOSER_VERSION} as composer
1416
# --------------- This stage install needed extenstions, plugins and add all needed configurations -------------------
1517
# ======================================================================================================================
1618

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
1824

1925
# Maintainer label
2026
LABEL maintainer="sherifabdlnaby@gmail.com"
2127

2228
# ------------------------------------- Install Packages Needed Inside Base Image --------------------------------------
2329

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}
3531

3632
# ---------------------------------------- Install / Enable PHP Extensions ---------------------------------------------
3733

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
4037
# head to: https://github.com/docker-library/docs/tree/master/php#how-to-install-more-php-extensions
4138
# EX: RUN docker-php-ext-install curl pdo pdo_mysql mysqli
4239
# 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) \
4441
opcache \
4542
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
4945
# 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
5057

5158
# ------------------------------------------------ PHP Configuration ---------------------------------------------------
5259

@@ -58,45 +65,39 @@ COPY docker/php/base-* $PHP_INI_DIR/conf.d
5865

5966
# ---------------------------------------------- PHP FPM Configuration -------------------------------------------------
6067

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/
6470

65-
# Copy scripts and PHP-FPM config
66-
COPY docker/fpm/*.conf /usr/local/etc/php-fpm.d/
6771

6872
# --------------------------------------------------- Scripts ----------------------------------------------------------
6973

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
7681

7782
# ---------------------------------------------------- Composer --------------------------------------------------------
7883

7984
COPY --from=composer /usr/bin/composer /usr/bin/composer
8085

81-
# ----------------------------------------------- NON-ROOT SWITCH ------------------------------------------------------
86+
# ----------------------------------------------------- MISC -----------------------------------------------------------
8287

88+
WORKDIR /app
8389
USER www-data
8490

85-
# ----------------------------------------------------- MISC -----------------------------------------------------------
86-
87-
WORKDIR /var/www/app
91+
# Common PHP Frameworks Env Variables
8892
ENV APP_ENV prod
8993
ENV APP_DEBUG 0
9094

91-
# Run as non-root
92-
USER www-data
93-
94-
# Validate FPM config
95+
# Validate FPM config (must use the non-root user)
9596
RUN php-fpm -t
9697

9798
# ---------------------------------------------------- HEALTH ----------------------------------------------------------
9899

99-
HEALTHCHECK CMD ["healthcheck"]
100+
HEALTHCHECK CMD ["healthcheck-liveness"]
100101

101102
# -------------------------------------------------- ENTRYPOINT --------------------------------------------------------
102103

@@ -124,9 +125,7 @@ COPY composer.lock composer.lock
124125
RUN composer config platform.php ${PHP_VERSION}
125126

126127
# 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
130129

131130
# ======================================================================================================================
132131
# ============================================== PRODUCTION IMAGE ====================================================
@@ -135,20 +134,28 @@ RUN composer install -n --no-progress --ignore-platform-reqs --no-plugins --no-s
135134

136135
FROM base AS app
137136

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
138144
COPY docker/php/prod-* $PHP_INI_DIR/conf.d/
139145

146+
USER www-data
147+
148+
# ----------------------------------------------- Production Config -----------------------------------------------------
149+
140150
# 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
142152

143153
# Copy App Code
144154
COPY --chown=www-data:www-data . .
145155

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
152159

153160
ENTRYPOINT ["entrypoint-prod"]
154161
CMD ["php-fpm"]
@@ -164,31 +171,28 @@ ARG XDEBUG_VERSION
164171
ENV APP_ENV dev
165172
ENV APP_DEBUG 1
166173

167-
# Switch to root to install stuff
174+
# Switch root to install stuff
168175
USER root
169176

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-
179177
# ---------------------------------------------------- Xdebug ----------------------------------------------------------
180178

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
182188

183189
# ------------------------------------------------------ PHP -----------------------------------------------------------
184190

185191
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
186192
COPY docker/php/dev-* $PHP_INI_DIR/conf.d/
187193

188-
# ------------------------------------------------- Entry Point --------------------------------------------------------
189-
190-
# Run as non-root
191194
USER www-data
195+
# ------------------------------------------------- Entry Point --------------------------------------------------------
192196

193197
# Entrypoints
194198
ENTRYPOINT ["entrypoint-dev"]
@@ -203,25 +207,21 @@ CMD ["php-fpm"]
203207
FROM nginx:${NGINX_VERSION}-alpine AS nginx
204208

205209
RUN rm -rf /var/www/* /etc/nginx/conf.d/* && adduser -u 1000 -D -S -G www-data www-data
206-
207210
COPY docker/nginx/nginx-* /usr/local/bin/
208211
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-*
210213

211214
# The PHP-FPM Host
212215
## Localhost is the sensible default assuming image run on a k8S Pod
213216
ENV PHP_FPM_HOST "localhost"
214217
ENV PHP_FPM_PORT "9000"
215218

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-
222219
# For Documentation
223220
EXPOSE 8080
224221

222+
# Switch User
223+
USER www-data
224+
225225
# Add Healthcheck
226226
HEALTHCHECK CMD ["nginx-healthcheck"]
227227

@@ -235,8 +235,7 @@ ENTRYPOINT ["nginx-entrypoint"]
235235
FROM nginx AS web
236236

237237
# 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
240239

241240
# ----------------------------------------------------- NGINX ----------------------------------------------------------
242241
FROM nginx AS web-dev

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
COMPOSE_PREFIX_CMD := DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1
44

5-
COMMAND ?= /bin/bash
5+
COMMAND ?= /bin/sh
66

77
# --------------------------
88

99
.PHONY: build deploy start stop logs restart shell up rm help
1010

11-
deploy: ## Deploy Prod Image
11+
deploy: ## Start using Prod Image in Prod Mode
1212
${COMPOSE_PREFIX_CMD} docker-compose -f docker-compose.prod.yml up --build -d
1313

1414
up: ## Start service
15+
@echo "Starting Application \n (note: Web container will wait App container to start before starting)"
16+
${COMPOSE_PREFIX_CMD} docker-compose up -d
17+
18+
build-up: ## Start service, rebuild if necessary
1519
${COMPOSE_PREFIX_CMD} docker-compose up --build -d
1620

1721
build: ## Build The Image
@@ -55,5 +59,5 @@ rm: ## Remove current container
5559

5660
help: ## Show this help.
5761
@echo "\n\nMake Application Docker Images and Containers using Docker-Compose files"
58-
@echo "Make sure you are using \033[0;32mDocker Version >= 20.1\033[0m & \033[0;32mDocker-Compose >= 18.06\033[0m "
62+
@echo "Make sure you are using \033[0;32mDocker Version >= 20.1\033[0m & \033[0;32mDocker-Compose >= 1.27\033[0m "
5963
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m ENV=<prod|dev> (default: dev)\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-12s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

0 commit comments

Comments
 (0)