Skip to content

Commit

Permalink
Merge pull request #28 from sherifabdlnaby/v5.0
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
sherifabdlnaby authored May 23, 2022
2 parents 3f3898d + b972054 commit 65a34cd
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 61 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Hadolint Action
uses: hadolint/hadolint-action@v1.6.0
uses: hadolint/hadolint-action@v2.0.0
with:
dockerfile: Dockerfile
ignore: DL3018 SC2086 DL3019
ignore: DL3018,SC2086,DL3019
failure-threshold: warning
51 changes: 26 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ---------------------------------------------- Build Time Arguments --------------------------------------------------
ARG PHP_VERSION="7.4"
ARG NGINX_VERSION="1.20.1"
ARG PHP_ALPINE_VERSION="3.15"
ARG NGINX_VERSION="1.21"
ARG COMPOSER_VERSION="2"
ARG XDEBUG_VERSION="3.1.3"
ARG COMPOSER_AUTH
Expand All @@ -15,7 +16,7 @@ FROM composer:${COMPOSER_VERSION} as composer
# --------------- This stage install needed extenstions, plugins and add all needed configurations -------------------
# ======================================================================================================================

FROM php:${PHP_VERSION}-fpm-alpine AS base
FROM php:${PHP_VERSION}-fpm-alpine${PHP_ALPINE_VERSION} AS base

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

# - Clean bundled config/users & recreate them with UID 1000 for docker compatability in dev container.
# - Create composer directories (since we run as non-root later)
# - Add Default Config
RUN deluser --remove-home www-data && adduser -u1000 -D www-data && rm -rf /var/www /usr/local/etc/php-fpm.d/* && \
mkdir -p /var/www/.composer /app && chown -R www-data:www-data /app /var/www/.composer

mkdir -p /var/www/.composer /app && chown -R www-data:www-data /app /var/www/.composer; \
# ------------------------------------------------ PHP Configuration ---------------------------------------------------

# Add Default Config
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

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

# --------------------------------------------------- Scripts ----------------------------------------------------------

COPY docker/*-base \
docker/healthcheck-* \
docker/command-loop \
COPY docker/entrypoint/*-base docker/post-build/*-base docker/pre-run/*-base \
docker/fpm/healthcheck-fpm \
docker/command-loop \
# to
/usr/local/bin/

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

# ---------------------------------------------------- Composer --------------------------------------------------------

Expand All @@ -125,7 +124,7 @@ RUN php-fpm -t

# ---------------------------------------------------- HEALTH ----------------------------------------------------------

HEALTHCHECK CMD ["healthcheck-liveness"]
HEALTHCHECK CMD ["healthcheck-fpm"]

# -------------------------------------------------- ENTRYPOINT --------------------------------------------------------

Expand Down Expand Up @@ -153,7 +152,7 @@ WORKDIR /app
COPY $APP_BASE_DIR/composer.json composer.json
COPY $APP_BASE_DIR/composer.lock composer.lock

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

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

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

USER www-data

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

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

ENTRYPOINT ["entrypoint-prod"]
CMD ["php-fpm"]
Expand All @@ -207,7 +208,7 @@ ENV APP_DEBUG 1
USER root

# For Composer Installs
RUN apk --no-cache add git openssh; \
RUN apk --no-cache add git openssh bash; \
# Enable Xdebug
docker-php-ext-enable xdebug

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

# Copy Dev Scripts
COPY docker/*-dev /usr/local/bin/
RUN chmod +x /usr/local/bin/*-dev; \
# ------------------------------------------------------ PHP -----------------------------------------------------------

mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

COPY docker/php/dev-* $PHP_INI_DIR/conf.d/
COPY docker/entrypoint/*-dev docker/post-build/*-dev docker/pre-run/*-dev \
# to
/usr/local/bin/

RUN chmod +x /usr/local/bin/*-dev; \
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

USER www-data
# ------------------------------------------------- Entry Point --------------------------------------------------------
Expand Down
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ OR
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.)

#### Build Time Arguments
| **ARG** | **Description** | **Default** |
|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
| `PHP_VERSION` | PHP Version used in the Image | `7.4` |
| `NGINX_VERSION` | Nginx Version | `1.17.4` |
| `COMPOSER_VERSION` | Composer Version used in Image | `2.0` |
| `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) | `{}` |
| `RUNTIME_DEPS` | List of all OS Packages needed for PHP Runtime | `zip` |
| `XDEBUG_VERSION` | Xdebug Version to use in Development Image | `3.0.3` |

#### Image Targets
| **ARG** | **Description**
| **Default** |
--------------------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
| `PHP_VERSION` | PHP Version used in the Image | `7.4` | | `PHP_ALPINE_VERSION` | Alpine
Version for the PHP Image | `3.15` | | `NGINX_VERSION` | Nginx Version | `1.21` | | `COMPOSER_VERSION` |
Composer Version used in Image | `2.0` | | `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) | `{}`
| | `RUNTIME_DEPS` | List of all OS Packages needed for PHP Runtime | `zip` | | `XDEBUG_VERSION` | Xdebug
Version to use in Development Image | `3.0.3` |

#### Image Targets

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

1. Install System Packages in the following section in the Dockerfile.
- List OS Packages needed in `RUNTIME_DEPS` ARG in Dockerfile header.
- Add OS Packages needed in `RUNTIME_DEPS` in Dockerfile.
2. Install PHP Extensions In the following section in the Dockerfile.
```dockerfile
# ---------------------------------------- Install / Enable PHP Extensions ---------------------------------------------
Expand Down Expand Up @@ -191,16 +192,21 @@ In `docker/` directory there is `post-build-*` and `pre-run-*` scripts that are
- 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.
2. Image Build Fails as it try to connect to DB.
- 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)
- A typical scenario in most frameworks that comes with `Doctrine` ORM is that if Doctrine 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)
3. Xdebug not working
- 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`.
- Xdebug is configured to work with Linux, to make it work for Mac/Windows, please change `XDEBUG_CLIENT_HOST` env
variable to `host.docker.internal` in `docker-compose.yml` file.
# License
[MIT License](https://raw.githubusercontent.com/sherifabdlnaby/kubephp/blob/master/LICENSE)
Copyright (c) 2021 Sherif Abdel-Naby
Copyright (c) 2022 Sherif Abdel-Naby
# Contribution
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ services:
volumes:
- ${APP_BASE_DIR-.}/public:/app/public
depends_on:
app:
condition: service_healthy
- app
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions docker/healthcheck-liveness

This file was deleted.

9 changes: 0 additions & 9 deletions docker/post-build-prod

This file was deleted.

File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions docker/post-build/post-build-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
# Put Custom Ad-hoc scripts after build. Like framework specific checks, etc.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions docker/pre-run-prod → docker/pre-run/pre-run-prod
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ set -e
# Put Custom Ad-hoc scripts below:

## Run Envsubst on .env to expand embedded Env Variables
echo "► Expanding Dotenv files with Environment Variables..."
for f in $(find . -name ".env*"); do cat $f | envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')" > "$f.tmp"; mv "$f.tmp" "$f"; done
#echo "► Expanding Dotenv files with Environment Variables..."
#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 comments on commit 65a34cd

Please sign in to comment.