From ad9cca36b1d44184974e9a2a00fd5fa1c5e4491a Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 22 Mar 2020 15:23:23 +0000 Subject: [PATCH] improvements to run inside docker --- .dockerignore | 1 + .github/workflows/test_and_publish.yml | 9 ++-- Dockerfile | 46 ++++++++++++----- README.md | 22 ++++---- core/install.php | 70 ++++++++++++-------------- ext/index/theme.php | 2 +- ext/static_files/style.css | 2 +- tests/bootstrap.php | 2 +- tests/docker-init.sh | 7 ++- 9 files changed, 89 insertions(+), 72 deletions(-) diff --git a/.dockerignore b/.dockerignore index 828d2ff8..9fead95e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,3 +5,4 @@ data images thumbs *.sqlite +Dockerfile diff --git a/.github/workflows/test_and_publish.yml b/.github/workflows/test_and_publish.yml index fe42b875..ba67576f 100644 --- a/.github/workflows/test_and_publish.yml +++ b/.github/workflows/test_and_publish.yml @@ -64,13 +64,13 @@ jobs: - name: Run test suite run: | if [[ "${{ matrix.database }}" == "pgsql" ]]; then - export DSN="pgsql:user=shimmie;password=shimmie;host=127.0.0.1;dbname=shimmie" + export TEST_DSN="pgsql:user=shimmie;password=shimmie;host=127.0.0.1;dbname=shimmie" fi if [[ "${{ matrix.database }}" == "mysql" ]]; then - export DSN="mysql:user=root;password=root;host=127.0.0.1;dbname=shimmie" + export TEST_DSN="mysql:user=root;password=root;host=127.0.0.1;dbname=shimmie" fi if [[ "${{ matrix.database }}" == "sqlite" ]]; then - export DSN="sqlite:data/shimmie.sqlite" + export TEST_DSN="sqlite:data/shimmie.sqlite" fi vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-clover=data/coverage.clover @@ -82,7 +82,7 @@ jobs: name: Publish runs-on: ubuntu-latest needs: test - if: github.ref == 'refs/heads/master' + if: github.event_name == 'push' steps: - uses: actions/checkout@master - name: Publish to Registry @@ -92,3 +92,4 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} cache: ${{ github.event_name != 'schedule' }} + buildoptions: "--build-arg RUN_TESTS=false" diff --git a/Dockerfile b/Dockerfile index 93b8194d..869cabd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,39 @@ +# "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) FROM debian:stable-slim -ENV DEBIAN_FRONTEND=noninteractive -EXPOSE 8000 -HEALTHCHECK --interval=5m --timeout=3s CMD curl --fail http://127.0.0.1:8000/ || exit 1 -RUN apt update && apt install -y curl \ - php7.3-cli php7.3-gd php7.3-pgsql php7.3-mysql php7.3-sqlite3 php7.3-zip php7.3-dom php7.3-mbstring php-xdebug \ - composer imagemagick vim zip unzip - +RUN apt update && apt install -y composer php7.3-gd php7.3-dom php7.3-sqlite3 imagemagick COPY composer.json composer.lock /app/ WORKDIR /app RUN composer install - COPY . /app/ -#RUN echo '=== Installing ===' && mkdir -p data/config && echo " data/config/auto_install.conf.php && php index.php && \ -# echo '=== Smoke Test ===' && php index.php get-page /post/list && \ -# echo '=== Unit Tests ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml && \ -# echo '=== Coverage ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-text && \ -# echo '=== Cleaning ===' && rm -rf data +ARG RUN_TESTS=true +RUN [ $RUN_TESTS = false ] || (\ + 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 '=== Unit Tests ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml && \ + echo '=== Coverage ===' && ./vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-text && \ + echo '=== Cleaning ===' && rm -rf data) + +# Build su-exec so that our final image can be nicer +FROM debian:stable-slim +RUN apt-get update && apt-get 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 +FROM debian:stable-slim +EXPOSE 8000 +HEALTHCHECK --interval=5m --timeout=3s CMD curl --fail http://127.0.0.1:8000/ || exit 1 +ENV UID=1000 \ + GID=1000 +RUN apt update && apt install -y curl \ + php7.3-cli php7.3-gd php7.3-pgsql php7.3-mysql php7.3-sqlite3 php7.3-zip php7.3-dom php7.3-mbstring php-xdebug \ + composer imagemagick vim zip unzip && \ + rm -rf /var/lib/apt/lists/* +COPY --from=0 /app /app +COPY --from=1 /usr/local/bin/su-exec /usr/local/bin/su-exec + +WORKDIR /app CMD ["/bin/sh", "/app/tests/docker-init.sh"] diff --git a/README.md b/README.md index 3c1190d2..3630e01a 100644 --- a/README.md +++ b/README.md @@ -48,23 +48,21 @@ check out one of the versioned branches. 5. Follow instructions noted in "Installation" starting from step 3. # Docker +If you just want to run shimmie inside docker, there's a pre-built image +in dockerhub - `shish2k/shimmie2` - which can be used like: +``` +docker run -p 8000 -v /my/hard/drive:/app/data shish2k/shimmie2 +``` -Useful for testing in a known-good environment, this command will build a -simple debian image and run all the unit tests inside it: - +If you want to build your own image from source: ``` docker build -t shimmie . ``` -Once you have an image which has passed all tests, you can then run it to get -a live system: - -``` -docker run -p 0.0.0.0:8123:8000 -v /mnt/shimmie-data:/app/data shimmie -``` - -Then you can visit your server on port 8123 to see the site, with data -stored in /mnt/shimmie-data on your local drive. +There are various options settable with environment variables: +- `UID` / `GID` - which user ID to run as (default 1000/1000) +- `INSTALL_DSN` - specify a data source to install into, to skip the installer screen, eg + `-e INSTALL_DSN="pgsql:user=shimmie;password=6y5erdfg;host=127.0.0.1;dbname=shimmie"` ### Upgrade from earlier versions diff --git a/core/install.php b/core/install.php index e85299e4..66668bba 100644 --- a/core/install.php +++ b/core/install.php @@ -18,7 +18,6 @@ function install() { date_default_timezone_set('UTC'); - define("DATABASE_TIMEOUT", 10000); if (is_readable("data/config/shimmie.conf.php")) { exit_with_page( @@ -59,10 +58,8 @@ function install() function get_dsn() { - if (file_exists("data/config/auto_install.conf.php")) { - $dsn = null; - /** @noinspection PhpIncludeInspection */ - require_once "data/config/auto_install.conf.php"; + if (getenv("INSTALL_DSN")) { + $dsn = getenv("INSTALL_DSN");; } elseif (@$_POST["database_type"] == DatabaseDriver::SQLITE) { /** @noinspection PhpUnhandledExceptionInspection */ $id = bin2hex(random_bytes(5)); @@ -136,37 +133,34 @@ function ask_questions() $warn_msg $err_msg -

Database Install

-
- - - - - - - - - - - - - - - - - - - - - - -
Type:
Host:
Username:
Password:
DB Name:
-
+ + + + + + + + + + + + + + + + + + + + + + +
Type:
Host:
Username:
Password:
DB Name: