more efficient docker build

This commit is contained in:
Shish 2023-06-27 11:27:11 +01:00
parent 639341bbeb
commit 133f805302
2 changed files with 17 additions and 24 deletions

View file

@ -1,10 +1,20 @@
ARG PHP_VERSION=8.2 ARG PHP_VERSION=8.2
# Install base packages which all stages (build, test, run) need
FROM debian:bookworm AS base
RUN apt update && apt upgrade -y && apt install -y \
php${PHP_VERSION}-cli php${PHP_VERSION}-gd php${PHP_VERSION}-zip php${PHP_VERSION}-xml php${PHP_VERSION}-mbstring \
php${PHP_VERSION}-pgsql php${PHP_VERSION}-mysql php${PHP_VERSION}-sqlite3 \
gosu curl imagemagick ffmpeg zip unzip && \
rm -rf /var/lib/apt/lists/*
# Composer has 100MB of dependencies, and we only need that during build and test
FROM base AS composer
RUN apt update && apt upgrade -y && apt install -y composer php${PHP_VERSION}-xdebug && rm -rf /var/lib/apt/lists/*
# "Build" shimmie (composer install - done in its own stage so that we don't # "Build" shimmie (composer install - done in its own stage so that we don't
# need to include all the composer fluff in the final image) # need to include all the composer fluff in the final image)
FROM debian:bookworm AS app FROM composer AS app
RUN apt update && apt upgrade -y
RUN apt install -y composer php${PHP_VERSION}-gd php${PHP_VERSION}-xml php${PHP_VERSION}-sqlite3 php${PHP_VERSION}-xdebug imagemagick
COPY composer.json composer.lock /app/ COPY composer.json composer.lock /app/
WORKDIR /app WORKDIR /app
RUN composer install --no-dev RUN composer install --no-dev
@ -13,9 +23,7 @@ COPY . /app/
# Tests in their own image. Really we should inherit from app and then # Tests in their own image. Really we should inherit from app and then
# `composer install` phpunit on top of that; but for some reason # `composer install` phpunit on top of that; but for some reason
# `composer install --no-dev && composer install` doesn't install dev # `composer install --no-dev && composer install` doesn't install dev
FROM debian:bookworm AS tests FROM composer AS tests
RUN apt update && apt upgrade -y
RUN apt install -y composer php${PHP_VERSION}-gd php${PHP_VERSION}-xml php${PHP_VERSION}-sqlite3 php${PHP_VERSION}-xdebug imagemagick
COPY composer.json composer.lock /app/ COPY composer.json composer.lock /app/
WORKDIR /app WORKDIR /app
RUN composer install RUN composer install
@ -25,32 +33,17 @@ RUN [ $RUN_TESTS = false ] || (\
echo '=== Installing ===' && mkdir -p data/config && INSTALL_DSN="sqlite:data/shimmie.sqlite" php index.php && \ echo '=== Installing ===' && mkdir -p data/config && INSTALL_DSN="sqlite:data/shimmie.sqlite" php index.php && \
echo '=== Smoke Test ===' && php index.php get-page /post/list && \ echo '=== Smoke Test ===' && php index.php get-page /post/list && \
echo '=== Unit Tests ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml && \ echo '=== Unit Tests ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml && \
echo '=== Coverage ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-text && \ echo '=== Coverage ===' && XDEBUG_MODE=coverage ./vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-text && \
echo '=== Cleaning ===' && rm -rf data) echo '=== Cleaning ===' && rm -rf data)
# Build su-exec so that our final image can be nicer
FROM debian:bookworm AS suexec
RUN apt update && apt upgrade -y
RUN apt install -y --no-install-recommends gcc libc-dev curl
RUN curl -k -o /usr/local/bin/su-exec.c https://raw.githubusercontent.com/ncopa/su-exec/master/su-exec.c; \
gcc -Wall /usr/local/bin/su-exec.c -o/usr/local/bin/su-exec; \
chown root:root /usr/local/bin/su-exec; \
chmod 0755 /usr/local/bin/su-exec;
# Actually run shimmie # Actually run shimmie
FROM debian:bookworm FROM base
EXPOSE 8000 EXPOSE 8000
HEALTHCHECK --interval=1m --timeout=3s CMD curl --fail http://127.0.0.1:8000/ || exit 1 HEALTHCHECK --interval=1m --timeout=3s CMD curl --fail http://127.0.0.1:8000/ || exit 1
ENV UID=1000 \ ENV UID=1000 \
GID=1000 \ GID=1000 \
UPLOAD_MAX_FILESIZE=50M UPLOAD_MAX_FILESIZE=50M
RUN apt update && apt upgrade -y && apt install -y \
php${PHP_VERSION}-cli php${PHP_VERSION}-gd php${PHP_VERSION}-zip php${PHP_VERSION}-xml php${PHP_VERSION}-mbstring \
php${PHP_VERSION}-pgsql php${PHP_VERSION}-mysql php${PHP_VERSION}-sqlite3 \
curl imagemagick ffmpeg zip unzip && \
rm -rf /var/lib/apt/lists/*
COPY --from=app /app /app COPY --from=app /app /app
COPY --from=suexec /usr/local/bin/su-exec /usr/local/bin/su-exec
WORKDIR /app WORKDIR /app
CMD ["/bin/sh", "/app/tests/docker-init.sh"] CMD ["/bin/sh", "/app/tests/docker-init.sh"]

View file

@ -4,7 +4,7 @@ useradd -ms /bin/bash -u $UID -g $GID shimmie
mkdir -p /app/data mkdir -p /app/data
chown $UID:$GID /app/data chown $UID:$GID /app/data
export PHP_CLI_SERVER_WORKERS=8 export PHP_CLI_SERVER_WORKERS=8
exec /usr/local/bin/su-exec shimmie:shimmie \ exec gosu shimmie:shimmie \
/usr/bin/php \ /usr/bin/php \
-d upload_max_filesize=$UPLOAD_MAX_FILESIZE \ -d upload_max_filesize=$UPLOAD_MAX_FILESIZE \
-d post_max_size=$UPLOAD_MAX_FILESIZE \ -d post_max_size=$UPLOAD_MAX_FILESIZE \