Skip to content

Commit 65a34cd

Browse files
Merge pull request #28 from sherifabdlnaby/v5.0
- Docker's directory restructure. - Make Image Compatible with ReadOnly File Systems by removing the envsubst command running in pre-run script by default. - Update PHP Alpine Version 3.13 -> 3.15 - Update Nginx Version `1.20` -> `1.21`
2 parents 3f3898d + b972054 commit 65a34cd

16 files changed

+56
-61
lines changed

.github/workflows/lint.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ jobs:
2626
steps:
2727
- uses: actions/checkout@v2
2828
- name: Hadolint Action
29-
uses: hadolint/hadolint-action@v1.6.0
29+
uses: hadolint/hadolint-action@v2.0.0
3030
with:
3131
dockerfile: Dockerfile
32-
ignore: DL3018 SC2086 DL3019
32+
ignore: DL3018,SC2086,DL3019
33+
failure-threshold: warning

Dockerfile

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ---------------------------------------------- Build Time Arguments --------------------------------------------------
22
ARG PHP_VERSION="7.4"
3-
ARG NGINX_VERSION="1.20.1"
3+
ARG PHP_ALPINE_VERSION="3.15"
4+
ARG NGINX_VERSION="1.21"
45
ARG COMPOSER_VERSION="2"
56
ARG XDEBUG_VERSION="3.1.3"
67
ARG COMPOSER_AUTH
@@ -15,7 +16,7 @@ FROM composer:${COMPOSER_VERSION} as composer
1516
# --------------- This stage install needed extenstions, plugins and add all needed configurations -------------------
1617
# ======================================================================================================================
1718

18-
FROM php:${PHP_VERSION}-fpm-alpine AS base
19+
FROM php:${PHP_VERSION}-fpm-alpine${PHP_ALPINE_VERSION} AS base
1920

2021
# Required Args ( inherited from start of file, or passed at build )
2122
ARG XDEBUG_VERSION
@@ -80,13 +81,11 @@ RUN apk add --no-cache --virtual .build-deps \
8081

8182
# - Clean bundled config/users & recreate them with UID 1000 for docker compatability in dev container.
8283
# - Create composer directories (since we run as non-root later)
84+
# - Add Default Config
8385
RUN deluser --remove-home www-data && adduser -u1000 -D www-data && rm -rf /var/www /usr/local/etc/php-fpm.d/* && \
84-
mkdir -p /var/www/.composer /app && chown -R www-data:www-data /app /var/www/.composer
85-
86+
mkdir -p /var/www/.composer /app && chown -R www-data:www-data /app /var/www/.composer; \
8687
# ------------------------------------------------ PHP Configuration ---------------------------------------------------
87-
88-
# Add Default Config
89-
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
88+
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
9089

9190
# Add in Base PHP Config
9291
COPY docker/php/base-* $PHP_INI_DIR/conf.d
@@ -99,13 +98,13 @@ COPY docker/fpm/*.conf /usr/local/etc/php-fpm.d/
9998

10099
# --------------------------------------------------- Scripts ----------------------------------------------------------
101100

102-
COPY docker/*-base \
103-
docker/healthcheck-* \
104-
docker/command-loop \
101+
COPY docker/entrypoint/*-base docker/post-build/*-base docker/pre-run/*-base \
102+
docker/fpm/healthcheck-fpm \
103+
docker/command-loop \
105104
# to
106105
/usr/local/bin/
107106

108-
RUN chmod +x /usr/local/bin/*-base /usr/local/bin/healthcheck-* /usr/local/bin/command-loop
107+
RUN chmod +x /usr/local/bin/*-base /usr/local/bin/healthcheck-fpm /usr/local/bin/command-loop
109108

110109
# ---------------------------------------------------- Composer --------------------------------------------------------
111110

@@ -125,7 +124,7 @@ RUN php-fpm -t
125124

126125
# ---------------------------------------------------- HEALTH ----------------------------------------------------------
127126

128-
HEALTHCHECK CMD ["healthcheck-liveness"]
127+
HEALTHCHECK CMD ["healthcheck-fpm"]
129128

130129
# -------------------------------------------------- ENTRYPOINT --------------------------------------------------------
131130

@@ -153,7 +152,7 @@ WORKDIR /app
153152
COPY $APP_BASE_DIR/composer.json composer.json
154153
COPY $APP_BASE_DIR/composer.lock composer.lock
155154

156-
# Set PHP Version of the Image
155+
# Set PHP Version of the Image
157156
RUN composer config platform.php ${PHP_VERSION}; \
158157
# Install Dependencies
159158
composer install -n --no-progress --ignore-platform-reqs --no-dev --prefer-dist --no-scripts --no-autoloader
@@ -169,11 +168,11 @@ ARG APP_BASE_DIR
169168
USER root
170169

171170
# Copy Prod Scripts && delete xdebug
172-
COPY docker/*-prod /usr/local/bin/
173-
RUN chmod +x /usr/local/bin/*-prod && pecl uninstall xdebug
171+
COPY docker/entrypoint/*-prod docker/post-build/*-prod docker/pre-run/*-prod \
172+
# to
173+
/usr/local/bin/
174174

175-
# Copy PHP Production Configuration
176-
COPY docker/php/prod-* $PHP_INI_DIR/conf.d/
175+
RUN chmod +x /usr/local/bin/*-prod && pecl uninstall xdebug
177176

178177
USER www-data
179178

@@ -187,7 +186,9 @@ COPY --chown=www-data:www-data $APP_BASE_DIR/ .
187186

188187
## Run Composer Install again
189188
## ( this time to run post-install scripts, autoloader, and post-autoload scripts using one command )
190-
RUN post-build-base && post-build-prod
189+
RUN composer install --optimize-autoloader --apcu-autoloader --no-dev -n --no-progress && \
190+
composer check-platform-reqs && \
191+
post-build-base && post-build-prod
191192

192193
ENTRYPOINT ["entrypoint-prod"]
193194
CMD ["php-fpm"]
@@ -207,7 +208,7 @@ ENV APP_DEBUG 1
207208
USER root
208209

209210
# For Composer Installs
210-
RUN apk --no-cache add git openssh; \
211+
RUN apk --no-cache add git openssh bash; \
211212
# Enable Xdebug
212213
docker-php-ext-enable xdebug
213214

@@ -219,13 +220,13 @@ ENV XDEBUG_CLIENT_HOST="host.docker.internal"
219220
# ---------------------------------------------------- Scripts ---------------------------------------------------------
220221

221222
# Copy Dev Scripts
222-
COPY docker/*-dev /usr/local/bin/
223-
RUN chmod +x /usr/local/bin/*-dev; \
224-
# ------------------------------------------------------ PHP -----------------------------------------------------------
225-
226-
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
227-
228223
COPY docker/php/dev-* $PHP_INI_DIR/conf.d/
224+
COPY docker/entrypoint/*-dev docker/post-build/*-dev docker/pre-run/*-dev \
225+
# to
226+
/usr/local/bin/
227+
228+
RUN chmod +x /usr/local/bin/*-dev; \
229+
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
229230

230231
USER www-data
231232
# ------------------------------------------------- Entry Point --------------------------------------------------------

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,17 @@ OR
114114
However, in an environment where CI/CD pipelines will build the image, they will need to supply some build-time arguments for the image. (tho defaults exist.)
115115

116116
#### Build Time Arguments
117-
| **ARG** | **Description** | **Default** |
118-
|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
119-
| `PHP_VERSION` | PHP Version used in the Image | `7.4` |
120-
| `NGINX_VERSION` | Nginx Version | `1.17.4` |
121-
| `COMPOSER_VERSION` | Composer Version used in Image | `2.0` |
122-
| `COMPOSER_AUTH` | A Json Object with Bitbucket or Github token to clone private Repos with composer.</br>[Reference](https://getcomposer.org/doc/03-cli.md#composer-auth) | `{}` |
123-
| `RUNTIME_DEPS` | List of all OS Packages needed for PHP Runtime | `zip` |
124-
| `XDEBUG_VERSION` | Xdebug Version to use in Development Image | `3.0.3` |
125-
126-
#### Image Targets
117+
| **ARG** | **Description**
118+
| **Default** |
119+
--------------------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
120+
| `PHP_VERSION` | PHP Version used in the Image | `7.4` | | `PHP_ALPINE_VERSION` | Alpine
121+
Version for the PHP Image | `3.15` | | `NGINX_VERSION` | Nginx Version | `1.21` | | `COMPOSER_VERSION` |
122+
Composer Version used in Image | `2.0` | | `COMPOSER_AUTH` | A Json Object with Bitbucket or Github token to
123+
clone private Repos with composer.</br>[Reference](https://getcomposer.org/doc/03-cli.md#composer-auth) | `{}`
124+
| | `RUNTIME_DEPS` | List of all OS Packages needed for PHP Runtime | `zip` | | `XDEBUG_VERSION` | Xdebug
125+
Version to use in Development Image | `3.0.3` |
126+
127+
#### Image Targets
127128

128129
| **Target** | Env | Desc | Size | Based On |
129130
|------------|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|-------------------------------|
@@ -136,7 +137,7 @@ However, in an environment where CI/CD pipelines will build the image, they will
136137
- The image is to be used as a base for your PHP application image, you should modify its Dockerfile to your needs.
137138

138139
1. Install System Packages in the following section in the Dockerfile.
139-
- List OS Packages needed in `RUNTIME_DEPS` ARG in Dockerfile header.
140+
- Add OS Packages needed in `RUNTIME_DEPS` in Dockerfile.
140141
2. Install PHP Extensions In the following section in the Dockerfile.
141142
```dockerfile
142143
# ---------------------------------------- Install / Enable PHP Extensions ---------------------------------------------
@@ -191,16 +192,21 @@ In `docker/` directory there is `post-build-*` and `pre-run-*` scripts that are
191192
- In containerized environment, you need to only run one process inside the container. This allows us to better instrument our application for many reasons like separation of health status, metrics, logs, etc.
192193
193194
2. Image Build Fails as it try to connect to DB.
194-
195-
- 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.](https://symfony.com/doc/current/reference/configuration/doctrine.html#:~:text=The-,server_version,-option%20was%20added)
195+
196+
- A typical scenario in most frameworks that comes with `Doctrine` ORM is that if Doctrine not configured with a DB
197+
Version, will try to access the DB at php's script initialization (even at the post-install cmd's), and it will
198+
fail when it cannot connect to
199+
DB. [Make sure you configure doctrine to avoid this extra DB Check connection.](https://symfony.com/doc/current/reference/configuration/doctrine.html#:~:text=The-,server_version,-option%20was%20added)
196200
197201
3. Xdebug not working
198202
199-
- Xdebug is configured to work with Linux, to make it work for Mac/Windows, please change Xdebug config in `/docker/php/dev-xdebug.ini` >> `xdebug.client_host` to `host.docker.internal`.
203+
- Xdebug is configured to work with Linux, to make it work for Mac/Windows, please change `XDEBUG_CLIENT_HOST` env
204+
variable to `host.docker.internal` in `docker-compose.yml` file.
200205
201206
# License
207+
202208
[MIT License](https://raw.githubusercontent.com/sherifabdlnaby/kubephp/blob/master/LICENSE)
203-
Copyright (c) 2021 Sherif Abdel-Naby
209+
Copyright (c) 2022 Sherif Abdel-Naby
204210
205211
# Contribution
206212

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ services:
3434
volumes:
3535
- ${APP_BASE_DIR-.}/public:/app/public
3636
depends_on:
37-
app:
38-
condition: service_healthy
37+
- app
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docker/healthcheck-liveness

Lines changed: 0 additions & 6 deletions
This file was deleted.

docker/post-build-prod

Lines changed: 0 additions & 9 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

docker/post-build/post-build-prod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
set -eu
3+
# Put Custom Ad-hoc scripts after build. Like framework specific checks, etc.
File renamed without changes.
File renamed without changes.

docker/pre-run-prod renamed to docker/pre-run/pre-run-prod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ set -e
2020
# Put Custom Ad-hoc scripts below:
2121

2222
## Run Envsubst on .env to expand embedded Env Variables
23-
echo "► Expanding Dotenv files with Environment Variables..."
24-
for f in $(find . -name ".env*"); do cat $f | envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')" > "$f.tmp"; mv "$f.tmp" "$f"; done
23+
#echo "► Expanding Dotenv files with Environment Variables..."
24+
#for f in $(find . -name ".env*"); do cat $f | envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')" > "$f.tmp"; mv "$f.tmp" "$f"; done

0 commit comments

Comments
 (0)