Refactor composer dependencies to create the production image

Use docker stages to install dependencies and simplify the building script.
Prepare production image while keeping compatibility with the development image.

Fix typo

Relocate arguments to improve cache

Make single line for each env definition

Remove composer home path

Avoid installing recommended packages
This commit is contained in:
AlMaVizca 2023-12-14 10:59:33 +07:00
parent 3a25ca5d71
commit 635adf1a04
No known key found for this signature in database
GPG Key ID: 95E7D44EAF29704D
3 changed files with 81 additions and 54 deletions

View File

@ -6,12 +6,6 @@ ENV FARMOS_PATH=/var/farmOS
ENV DRUPAL_PATH=/opt/drupal
ENV COMPOSER_HOME=${FARMOS_PATH}
# Set the farmOS and composer project repository URLs and versions.
ARG FARMOS_REPO=https://github.com/farmOS/farmOS.git
ARG FARMOS_VERSION=3.x
ARG PROJECT_VERSION=3.x
ARG PROJECT_REPO=https://github.com/farmOS/composer-project.git
##
# Build PHP extensions, GEOS and bcmath.
FROM baseimage as php-dependencies
@ -31,9 +25,45 @@ RUN apt-get update && apt-get install -y libgeos-dev \
# Install the BCMath PHP extension.
RUN docker-php-ext-install bcmath
# Setup dependencies and sources for composer installations.
FROM baseimage as composer-file
# Set the farmOS and composer project repository URLs and versions.
ARG FARMOS_REPO=https://github.com/farmOS/farmOS.git
ARG FARMOS_VERSION=3.x
ARG PROJECT_VERSION=3.x
ARG PROJECT_REPO=https://github.com/farmOS/composer-project/raw/${PROJECT_VERSION}/composer.json
# Set the COMPOSER_MEMORY_LIMIT environment variable to unlimited.
ENV COMPOSER_MEMORY_LIMIT=-1
# Allow root to install plugins.
ENV COMPOSER_ALLOW_SUPERUSER=1
# Install apt dependencies.
RUN apt-get update && apt-get install -y --no-install-recommends\
# Install git and unzip (needed by Composer).
git unzip
# Add the build-farmOS.sh script.
COPY build-farmOS.sh /usr/local/bin/
# Setup composer file for non interactive installation.
WORKDIR ${FARMOS_PATH}
RUN build-farmOS.sh
##
# Create layer with farmOS sources.
FROM composer-file as farmos-sources
# Install sources.
RUN composer install --no-dev
# Set the version in farm.info.yml.
RUN sed -i "s|version: 3.x|version: ${FARMOS_VERSION}|g" ${FARMOS_PATH}/web/profiles/farm/farm.info.yml
##
# Build final image.
FROM baseimage
FROM baseimage as farmos-baseimage
# Set Apache ServerName directive globally to suppress AH00558 message.
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
@ -45,32 +75,33 @@ RUN docker-php-ext-enable geos bcmath
# Add custom PHP configurations.
COPY conf.d/ /usr/local/etc/php/conf.d
# Install apt dependencies.
# Install apt dependencies and clean up.
RUN apt-get update && apt-get install -y \
# Install git and unzip (needed by Composer).
git unzip \
# Install postgresql-client so Drush can connect to the database.
postgresql-client \
# Install libgeos-c1v5 so geos php extension can use libgeos_c.so.1.
libgeos-c1v5 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Set the COMPOSER_MEMORY_LIMIT environment variable to unlimited.
ENV COMPOSER_MEMORY_LIMIT=-1
# Add the build-farmOS.sh script.
COPY build-farmOS.sh /usr/local/bin/
RUN chmod a+x /usr/local/bin/build-farmOS.sh
# Build the farmOS codebase in ${FARMOS_PATH} with the --no-dev flag.
# Change the ownership of the sites directory and copy the farmOS codebase into /${DRUPAL_PATH}.
RUN mkdir ${FARMOS_PATH} \
&& /usr/local/bin/build-farmOS.sh --no-dev \
&& chown -R www-data:www-data ${FARMOS_PATH}/web/sites \
&& rm -r ${DRUPAL_PATH} && cp -rp ${FARMOS_PATH} ${DRUPAL_PATH}
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean \
# Clean up ${DRUPAL_PATH}.
&& rm -r ${DRUPAL_PATH} \
&& mkdir ${DRUPAL_PATH}
# Set the entrypoint.
COPY docker-entrypoint.sh /usr/local/bin/
COPY --chown=www-data docker-entrypoint.sh /usr/local/bin/
# Set the working directory, entrypoint and command.
WORKDIR ${DRUPAL_PATH}
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
##
# Final image.
FROM farmos-baseimage
# Add farmOS sources.
COPY --from=farmos-sources --chown=www-data:www-data ${FARMOS_PATH} ${DRUPAL_PATH}
# Add the build-farmOS.sh script to keep compatibility with dev image.
COPY build-farmOS.sh /usr/local/bin/

View File

@ -8,21 +8,12 @@ set -e
# If ${FARMOS_PATH} is not empty, bail.
if [ "$(ls -A ${FARMOS_PATH})" ]; then
echo "The ${FARMOS_PATH} is not empty, terminate."
exit 1
fi
# Make ${FARMOS_PATH} the working directory.
cd ${FARMOS_PATH}
# Generate an empty Composer project project and checkout a specific version.
git clone ${PROJECT_REPO} project
mv project/.git ./.git
rm -rf project
git checkout ${PROJECT_VERSION}
git reset --hard
# Create a temporary Composer cache directory.
export COMPOSER_HOME="$(mktemp -d)"
# Fetch composer template
curl -L ${PROJECT_REPO} -o composer.json
# If FARMOS_VERSION is a valid semantic versioning string, we assume that it is
# a tagged version.
@ -63,15 +54,6 @@ for plugin in ${allowedPlugins[@]}; do
composer config --no-plugins allow-plugins.$plugin true
done
# Run composer install with optional arguments passed into this script.
if [ $# -eq 0 ]; then
composer install
else
composer install "$*"
fi
# Set the version in farm.info.yml.
sed -i "s|version: 3.x|version: ${FARMOS_VERSION}|g" ${FARMOS_PATH}/web/profiles/farm/farm.info.yml
# Remove the Composer cache directory.
rm -rf "$COMPOSER_HOME"
# Create folder for composer installations.
mkdir -p ${FARMOS_PATH}/web/sites
mkdir -p ${FARMOS_PATH}/vendor

View File

@ -4,8 +4,8 @@ FROM farmos/farmos:3.x
# Set the farmOS and composer project repository URLs and versions.
ARG FARMOS_REPO=https://github.com/farmOS/farmOS.git
ARG FARMOS_VERSION=3.x
ARG PROJECT_REPO=https://github.com/farmOS/composer-project.git
ARG PROJECT_VERSION=3.x
ARG PROJECT_REPO=https://github.com/farmOS/composer-project/raw/${PROJECT_VERSION}/composer.json
# Install and enable XDebug extension.
RUN yes | pecl install xdebug \
@ -27,14 +27,24 @@ RUN usermod -u ${WWW_DATA_ID} www-data && groupmod -g ${WWW_DATA_ID} www-data
# Create a fresh ${FARMOS_PATH} directory owned by www-data.
# We do this in two steps because of a known issue with Moby.
# @see https://github.com/farmOS/farmOS/pull/440
RUN rm -r ${FARMOS_PATH}
RUN rm -rf ${FARMOS_PATH}
RUN mkdir ${FARMOS_PATH} && chown www-data:www-data ${FARMOS_PATH}
# Install apt dependencies, they are not part of baseimage after decouple composer.
RUN apt-get update && apt-get install -y \
# Install git and unzip (needed by Composer).
git unzip
# Change to the www-data user.
USER www-data
# Work on FarmOS folder.
WORKDIR ${FARMOS_PATH}
# Build the farmOS codebase in ${FARMOS_PATH}.
RUN /usr/local/bin/build-farmOS.sh
RUN /usr/local/bin/build-farmOS.sh && composer install
RUN sed -i "s|version: 3.x|version: ${FARMOS_VERSION}|g" ${FARMOS_PATH}/web/profiles/farm/farm.info.yml
# Add Configurartions for PHP CodeSniffer, PHPStan.
COPY --chown=www-data ./files/ ${FARMOS_PATH}/
@ -61,3 +71,7 @@ RUN rm -r ${DRUPAL_PATH} && cp -rp ${FARMOS_PATH} ${DRUPAL_PATH}
# Create a Composer config directory for the www-data user.
RUN mkdir /var/www/.composer && chown www-data:www-data /var/www/.composer
# Setup DRUPAL working path.
WORKDIR ${DRUPAL_PATH}