Commit aade1156 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Reduce the size of the php/Wordpress image

parent 7801bc49
Loading
Loading
Loading
Loading
+36 −20
Original line number Diff line number Diff line
@@ -23,36 +23,52 @@ EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]


## Stage PHP
ARG php_version=
FROM php:${php_version:+$php_version-}fpm-alpine as deps
FROM php:${php_version:+$php_version-}fpm-alpine as php-stage
ENV STAGE=/stage/php
RUN --mount=type=bind,source=scripts/install-deps.sh,target=/step /step

FROM deps as compile
RUN --mount=type=bind,source=scripts/compile.sh,target=/step /step
COPY --from=docker.kodo.org.uk/kodo.org.uk/docker/docker-build-helpers:1.0 \
    /scripts /bin
RUN --mount=type=bind,source=scripts/prepare-stage.sh,target=/step /step

FROM deps as fastcgi

LABEL uk.org.kodo.maintainer "Dom Sekotill <dom.sekotill@kodo.org.uk>"

## Stage Wordpress
FROM php-stage as wordpress-stage
ARG wp_version=latest
WORKDIR /app
ENV WORDPRESS_ROOT=/app

COPY --from=compile /usr/local/etc/php /usr/local/etc/php
COPY --from=compile /usr/local/lib/php /usr/local/lib/php
COPY scripts/wp.sh /usr/local/bin/wp
COPY data/composer.json /app/composer.json
WORKDIR /stage/wordpress/app
ENV STAGE=/stage/wordpress WORDPRESS_ROOT=/stage/wordpress/app
COPY scripts/wp.sh /stage/wordpress/usr/bin/wp
COPY data/composer.json composer.json
COPY plugins wp-content/mu-plugins
RUN --mount=type=bind,source=scripts/install-wp.sh,target=/step \
    /step ${wp_version}

COPY plugins/* wp-content/mu-plugins/
COPY data/fpm.conf /usr/local/etc/php-fpm.d/image.conf
COPY data/opcache.ini /usr/local/etc/php/conf.d/opcache-recommended.ini
COPY data/wp-config.php /usr/share/wordpress/wp-config.php
COPY scripts/entrypoint.sh /bin/entrypoint

# PAGER is used by the wp-cli tool, the default 'less' is not installed
ENV PAGER=more
## Stage configuration
FROM wordpress-stage as config-stage
WORKDIR /stage/config
ENV STAGE=/stage/config
COPY data/fpm.conf usr/local/etc/php-fpm.d/image.conf
COPY data/opcache.ini usr/local/etc/php/conf.d/opcache-recommended.ini
COPY data/wp-config.php usr/share/wordpress/wp-config.php
COPY scripts/entrypoint.sh bin/entrypoint


## Final image composed of three layers:
##  1) PHP runtime and libs
##  2) Wordpress
##  3) Configuration
FROM scratch as fastcgi
LABEL uk.org.kodo.maintainer "Dom Sekotill <dom.sekotill@kodo.org.uk>"
COPY --from=php-stage /stage/php /
COPY --from=wordpress-stage /stage/wordpress /
COPY --from=config-stage /stage/config /
WORKDIR /app
ENV PREFIX=/usr
ENV WORDPRESS_ROOT=/app
ENV PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/sbin
EXPOSE 9000
ENTRYPOINT ["/bin/entrypoint"]
CMD ["php-fpm"]
+0 −16
Original line number Diff line number Diff line
#!/bin/bash
set -eux

# Packaged build dependencies
BUILD_DEPS=(
	autoconf
	build-base
	gmp-dev
	imagemagick-dev
	jpeg-dev
	libpng-dev
	libwebp-dev
	libzip-dev
)

# Distributed extensions
PHP_EXT=(
	bcmath
@@ -30,10 +18,6 @@ php_version() {
		return 1
}

# Install packaged dependencies
apk update
apk add "${BUILD_DEPS[@]}"

# Build & install distributed extensions
if php_version -gt 7.4; then
	GD_ARGS=( --with-jpeg=/usr --with-webp=/usr )
+9 −6
Original line number Diff line number Diff line
@@ -4,11 +4,14 @@ set -eux
# Install packaged dependencies
apk update
apk add \
	autoconf \
	bash \
	imagemagick-libs \
	libgmpxx \
	libjpeg \
	libpng \
	libwebp \
	libzip \
	build-base \
	gmp-dev \
	imagemagick-dev \
	jpeg-dev \
	less \
	libpng-dev \
	libwebp-dev \
	libzip-dev \
	rsync \
+10 −6
Original line number Diff line number Diff line
@@ -5,14 +5,18 @@ COMPOSER_INSTALLER_URL=https://getcomposer.org/installer
WP_CLI_URL=https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
WP_PASSWORD_HASH=https://raw.githubusercontent.com/Ayesh/WordPress-Password-Hash/1.5.1

# Install Composer
# Prepare stage
export PREFIX=$STAGE/usr/
export PATH+=:$PREFIX/bin
mkdir -p $PREFIX/bin $PREFIX/lib

# Install Composer directly to STAGE
curl -sSL ${COMPOSER_INSTALLER_URL} |
	php -- --install-dir=/usr/local/lib --filename=composer.phar
ln -s ../lib/composer.phar /usr/local/bin/composer
	php -- --install-dir=$PREFIX/lib --filename=composer.phar
ln -s ../lib/composer.phar $PREFIX/bin/composer

# Install WP-CLI
curl -sSL ${WP_CLI_URL} \
    >/usr/local/lib/wp-cli.phar
# Install WP-CLI directly to STAGE
curl -sSL ${WP_CLI_URL} >$PREFIX/lib/wp-cli.phar

# Install Wordpress core
wp core download --skip-content --locale=en_GB --version=$1
+39 −0
Original line number Diff line number Diff line
#!/bin/bash
set -eux

UTILS=(
	cat
	date
	less
	rsync
	sort
	xargs
)

UTILS+=(
	ls
	du
)

BASH_BINS=(
	/bin/bash
	/usr/lib/bash/head
	/usr/lib/bash/sleep
	/usr/lib/bash/unlink
)

bootstrap-stage
stage-useradd www-data

{
	# Shell & cmdline utils
	which "${UTILS[@]}"
	printf "%s\n" "${BASH_BINS[@]}"

	# PHP
	echo /usr/local/bin/php
	echo /usr/local/sbin/php-fpm
	find /usr/local/lib/php /usr/local/etc/php -type f
} | collect-binaries

ln -s bash $STAGE/bin/sh
Loading