Skip to content

Commit

Permalink
[shopsys] refactor Dockerfiles for PHP-FPM (#3518)
Browse files Browse the repository at this point in the history
  • Loading branch information
henzigo authored Nov 25, 2024
1 parent 9653469 commit 1a5af5d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ build:
script:
- cp -R ./docker/nginx/ ./app/docker/nginx/
- cd ./app/
- docker build -f ./docker/php-fpm/Dockerfile --target production-project --compress -t ${TAG} .
- docker build -f ./docker/php-fpm/Dockerfile --target production --compress -t ${TAG} .
- docker push ${TAG}
interruptible: true

Expand Down
92 changes: 60 additions & 32 deletions app/docker/php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM shopsys/php-image:16.0 as base
FROM shopsys/php-image:16.0 AS base

ARG project_root=.

Expand All @@ -9,50 +9,78 @@ COPY ${project_root}/docker/php-fpm/php-ini-overrides.ini /usr/local/etc/php/php

########################################################################################################################

FROM base as development

FROM base AS production_builder
USER root

# install Blackfire probe
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION.(PHP_ZTS ? '-zts' : '');") \
&& architecture=$(uname -m) \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s "https://blackfire.io/api/v1/releases/probe/php/linux/$architecture/$version" \
&& mkdir -p /tmp/blackfire \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get ('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8307\n" > "$PHP_INI_DIR/conf.d/blackfire.ini" \
&& curl -A "Docker" -L "https://blackfire.io/api/v1/releases/cli/linux/$architecture" | tar zxp -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
&& rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz

# allow overwriting UID and GID o the user "www-data" to help solve issues with permissions in mounted volumes
# if the GID is already in use, we will assign GID 33 instead (33 is the standard uid/gid for "www-data" in Debian)
ARG www_data_uid
ARG www_data_gid
RUN if [ -n "$www_data_uid" ]; then deluser www-data && (addgroup --gid $www_data_gid www-data || addgroup --gid 33 www-data) && adduser --system --home /home/www-data --uid $www_data_uid --disabled-password --group www-data; fi;

# as the UID and GID might have changed, change the ownership of the home directory workdir again
RUN chown -R www-data:www-data /home/www-data /var/www/html
# Copy project source code
COPY --chown=www-data:www-data / /var/www/html

USER www-data
RUN composer install --optimize-autoloader --no-interaction --no-progress --no-dev \
&& php phing build-deploy-part-1-db-independent clean

########################################################################################################################
RUN rm -rf /var/www/html/node_modules \
/var/www/html/tests

FROM base as production
RUN chmod +x ./deploy/deploy-project.sh && ./deploy/deploy-project.sh merge || true # Hack for monorepo to continue build

ARG project_root=.
########################################################################################################################
##################################### Production stage #################################################################
########################################################################################################################
FROM base AS production

# copy custom FPM pool configuration
# Copy PHP-FPM configuration
COPY ${project_root}/docker/php-fpm/production-www.conf /usr/local/etc/php-fpm.d/www.conf

COPY --chown=www-data:www-data / /var/www/html
USER www-data
COPY --from=production_builder /var/www/html/ /var/www/html/

RUN composer install --optimize-autoloader --no-interaction --no-progress --no-dev
########################################################################################################################
##################################### Development stage ################################################################
########################################################################################################################
FROM base AS development
USER root
ARG www_data_uid
ARG www_data_gid

RUN php phing build-deploy-part-1-db-independent clean
RUN apk add --no-cache \
git \
linux-headers \
mc \
nodejs \
npm \
shadow

RUN if [ -n "${www_data_gid}" ] && ! getent group ${www_data_gid} >/dev/null; then \
if getent group www-data >/dev/null; then groupmod -g ${www_data_gid} www-data; fi; \
fi && \
if [ -n "${www_data_uid}" ] && ! id -u ${www_data_uid} >/dev/null; then \
if id -u www-data >/dev/null; then usermod -u ${www_data_uid} www-data; fi; \
fi

USER www-data

########################################################################################################################
##################################### Development with blackfire ##########################
########################################################################################################################
FROM development AS development_blackfire
USER root
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& architecture=$(uname -m) \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/alpine/$architecture/$version \
&& mkdir -p /tmp/blackfire \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get ('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8307\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
&& rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz

RUN mkdir -p /tmp/blackfire \
&& architecture=$(uname -m) \
&& curl -A "Docker" -L https://blackfire.io/api/v1/releases/cli/linux/$architecture | tar zxp -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
&& rm -Rf /tmp/blackfire

FROM production as production-project

RUN chmod +x ./deploy/deploy-project.sh && ./deploy/deploy-project.sh merge
# Please note that the Blackfire Probe is dependent on the session module.
# If it isn't present in your install, you will need to enable it yourself.
USER www-data
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1a5af5d

Please sign in to comment.