diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..fea0076 --- /dev/null +++ b/.env.example @@ -0,0 +1,23 @@ +# Change these values to your liking +# Timezone for the containers +TZ = Europe/Berlin + +# The passwords must match +MODX_DB_PASSWORD = "my-strong-random-mysql-password" +MARIADB_PASSWORD = "my-strong-random-mysql-password" + +# These are the login details for the modx manager +MODX_ADMIN_USER=my-username +MODX_ADMIN_PASSWORD=my-password +MODX_ADMIN_EMAIL=my@email.com + +# Do not change the values below unless you know what you are doing + +MODX_DB_HOST = "modx_mysql:3306" + +MODX_DB_USER = "modx" +MARIADB_USER = modx + + +MODX_DB_NAME = "modx" +MARIADB_DATABASE = modx diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..5c7e46e --- /dev/null +++ b/Caddyfile @@ -0,0 +1,20 @@ +localhost { + root * /srv + @rewritable { + not file + not path /assets/* /manager/* /connectors/* + } + rewrite @rewritable /index.php?{query}&q={path} + rewrite /manager/ /manager/index.php + @no_exec { + not file + not path /assets/* /setup/* + } + reverse_proxy @no_exec modx_php:9000 { + transport fastcgi { + root /modx/public + split .php + } + } + file_server +} diff --git a/README.md b/README.md index ba39f00..913027e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # ModX Revolution Docker -A docker setup that can autoupdate ModX Revolution. \ No newline at end of file +A docker setup that can autoupdate ModX Revolution. + +## Simplified Usage +1. Copy the `.env.example` file to `.env` +2. Run `docker-compose up` +3. The Webserver will now listen on Ports `80` and `443` \ No newline at end of file diff --git a/build/Dockerfile b/build/Dockerfile index 9e5165f..8db5bec 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -15,37 +15,45 @@ FROM php:7-fpm-alpine LABEL org.opencontainers.image.authors="Jan Giesenberg <749@github.com>" -RUN mkdir -p /modx/core/config /modx/core/packages /modx/core/components /modx/public/assets - -VOLUME /modx/core/config -VOLUME /modx/core/packages -VOLUME /modx/core/components -VOLUME /modx/public/assets - EXPOSE 9000 -WORKDIR /modx/public - -RUN apk add --no-cache freetype-dev libpng-dev jpeg-dev unzip sudo bash \ +RUN apk add --no-cache rsync freetype-dev libpng-dev jpeg-dev unzip sudo bash gettext \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ - && docker-php-ext-install -j$(nproc) gd opcache mysqli pdo pdo_mysql + && docker-php-ext-install -j$(nproc) gd opcache mysqli pdo pdo_mysql \ + && rm -rf /usr/src/php/ ARG MODX_VERSION 2.8.2 ENV MODX_VERSION ${MODX_VERSION} # upstream tarballs include ./modx-${MODX_VERSION}/ -RUN echo ${MODX_VERSION} \ +RUN cd / \ + && echo ${MODX_VERSION} \ && curl -o modx.zip -SL http://modx.com/download/direct/modx-${MODX_VERSION}-pl.zip \ && unzip -q modx.zip -d /usr/src \ - && mv /usr/src/modx-${MODX_VERSION}-pl /usr/src/modx \ - && find /usr/src/modx -name 'ht.access' -exec bash -c 'rm $0' {} \; \ + && rm -rf /modx \ + && mkdir -p /modx /usr/src/modx \ + && mv /usr/src/modx-${MODX_VERSION}-pl/ /modx/public \ + && find /modx -name 'ht.access' -exec bash -c 'rm $0' {} \; \ + && mv /modx/public/core /modx/ \ + && cp -r /modx/public/setup /usr/src/modx \ && rm modx.zip \ - && chown -R www-data:www-data /usr/src/modx \ && chown -R www-data:www-data /modx -COPY files/docker-entrypoint.sh /entrypoint.sh +COPY --chown=www-data files/docker-entrypoint.sh /entrypoint.sh +COPY --chown=www-data:www-data files/docker-entrypoint /docker-entrypoint COPY files/php-config/ /usr/local/etc/php/conf.d/ -COPY files/config.core.php /modx/config.core.php.tmpl +COPY --chown=www-data:www-data files/config.core.php /modx/config.core.php.tmpl +RUN chmod +x /entrypoint.sh \ + && mkdir -p /modx/static/manager /modx/core/config /modx/core/packages /modx/core/components /modx/public/assets + +VOLUME /modx/static/manager +VOLUME /modx/core/config +VOLUME /modx/core/packages +VOLUME /modx/core/components +VOLUME /modx/public/assets + + +WORKDIR /modx/public ENTRYPOINT ["/entrypoint.sh"] CMD ["php-fpm"] \ No newline at end of file diff --git a/build/files/docker-entrypoint.sh b/build/files/docker-entrypoint.sh index 01ec8ae..22c20ee 100644 --- a/build/files/docker-entrypoint.sh +++ b/build/files/docker-entrypoint.sh @@ -1,14 +1,17 @@ -#!/bin/bash +#!/usr/bin/env bash set -ex PATH_MODX_CORE=/modx/core PATH_MODX_PUBLIC=/modx/public +PATH_MODX_SETUP=/modx/public/setup PATH_MODX_CORE_CONFIG_PHP=/modx/config.core.php.tmpl setup_config_core() { cd $PATH_MODX_PUBLIC - CONFIGS=`find -name "config.core.php" | tr '\n' ' '` - tee $CONFIGS <$PATH_MODX_CORE_CONFIG_PHP >/dev/null + find -name "config.core.php" | while read line ; do + rm $line; + cp $PATH_MODX_CORE_CONFIG_PHP $line + done } if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then @@ -45,134 +48,70 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then exit 1 fi - TERM=dumb php -- "$MODX_DB_HOST" "$MODX_DB_USER" "$MODX_DB_PASSWORD" "$MODX_DB_NAME" <<'EOPHP' -connect_error) { - fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n"); - --$maxTries; - if ($maxTries <= 0) { - exit(1); - } - sleep(3); - } -} while ($mysql->connect_error); - -if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '` ' . - 'DEFAULT CHARACTER SET = \'utf8\' DEFAULT COLLATE \'utf8_general_ci\'')) { - - fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n"); - $mysql->close(); - exit(1); -} + TERM=dumb php -- "$MODX_DB_HOST" "$MODX_DB_USER" "$MODX_DB_PASSWORD" "$MODX_DB_NAME" close(); -EOPHP + setup_config_core if ! [ -e index.php -a -e ../core/config/config.inc.php ]; then - echo "MODX not found in $(pwd) - copying now..." >&2 - - if [ "$(ls -A)" ]; then - echo "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!" >&2 - ( set -x; ls -A; sleep 10 ) - fi - - tar cf - --one-file-system -C /usr/src/modx . | tar xf - - mkdir -p $PATH_MODX_CORE - cd $PATH_MODX_CORE - tar cf - --one-file-system -C "$PATH_MODX_PUBLIC/core" . | tar xf - - cd $PATH_MODX_PUBLIC - rm -rf core - setup_config_core + echo "MODX not installed yet, installing..." >&2 touch /modx/core/config/config.inc.php chown -R www-data:www-data /modx - echo "Complete! MODX has been successfully copied to $(pwd)" >&2 - : ${MODX_ADMIN_USER:='admin'} : ${MODX_ADMIN_PASSWORD:='admin'} - cat > setup/config.xml < - mysql - $MODX_DB_HOST - $MODX_DB_NAME - $MODX_DB_USER - $MODX_DB_PASSWORD - utf8 - utf8 - utf8_general_ci - $MODX_TABLE_PREFIX - 443 - localhost - 0 - - 1 - 0 - en - - $MODX_ADMIN_USER - $MODX_ADMIN_PASSWORD - $MODX_ADMIN_EMAIL - - /modx/core/ - /modx/public/manager/ - /manager/ - /modx/public/connectors/ - /connectors/ - /modx/public/ - / - - 1 - -EOF + envsubst >setup/config.xml getVersionData()['full_version'], $argv[1]) == -1) { - echo 1; -} -EOPHP -) + echo "$MODX_VERSION" >/modx/core/config/install_version.txt + + setup_config_core + + echo "Complete! MODX has been successfully installed" >&2 + else + UPGRADE=$(TERM=dumb php -- "$MODX_VERSION" &2 "Upgrading MODX..." - sudo -u www-data mkdir setup + if [ ! -d "$PATH_MODX_SETUP" ]; then + sudo -u www-data mkdir "$PATH_MODX_SETUP" - tar cf - --one-file-system -C /usr/src/modx/setup . | tar xf - -C setup + tar cf - --one-file-system -C /usr/src/modx/setup . | tar xf - -C $PATH_MODX_SETUP - setup_config_core + setup_config_core + fi - chown -R www-data:www-data /modx + envsubst > config.xml setup/config.xml < - 1 - 0 - en - 1 - -EOF - chown www-data:www-data setup/config.xml + chown -R www-data:www-data /modx sudo -u www-data php setup/index.php --installmode=upgrade + + echo "$MODX_VERSION" >/modx/core/config/install_version.txt + + setup_config_core + + echo "Complete! MODX has been successfully upgraded to $MODX_VERSION" >&2 + elif [ "$UPGRADE" -eq "0" ]; then + echo "ModX already up to date." + else + echo "Unexpected version check output: $UPGRADE" fi fi fi +#export static files +chown -R www-data:www-data /modx +cd /modx/public +rm -rf setup/ + +rsync -a --exclude='*.php' manager /modx/static + + echo "Starting up $@" exec "$@" diff --git a/build/files/docker-entrypoint/compare-version.php b/build/files/docker-entrypoint/compare-version.php new file mode 100644 index 0000000..8edd195 --- /dev/null +++ b/build/files/docker-entrypoint/compare-version.php @@ -0,0 +1,9 @@ +connect_error) { + fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n"); + --$maxTries; + if ($maxTries <= 0) { + exit(1); + } + sleep(3); + } +} while ($mysql->connect_error); + +if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '` ' . + 'DEFAULT CHARACTER SET = \'utf8\' DEFAULT COLLATE \'utf8_general_ci\'')) { + + fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n"); + $mysql->close(); + exit(1); +} + +$mysql->close(); \ No newline at end of file diff --git a/build/files/docker-entrypoint/install.xml b/build/files/docker-entrypoint/install.xml new file mode 100644 index 0000000..6d42680 --- /dev/null +++ b/build/files/docker-entrypoint/install.xml @@ -0,0 +1,32 @@ + + mysql + $MODX_DB_HOST + $MODX_DB_NAME + $MODX_DB_USER + $MODX_DB_PASSWORD + utf8 + utf8 + utf8_general_ci + $MODX_TABLE_PREFIX + 443 + localhost + 0 + + 1 + 0 + en + + $MODX_ADMIN_USER + $MODX_ADMIN_PASSWORD + $MODX_ADMIN_EMAIL + + /modx/core/ + /modx/public/manager/ + /manager/ + /modx/public/connectors/ + /connectors/ + /modx/public/ + / + + 1 + \ No newline at end of file diff --git a/build/files/docker-entrypoint/upgrade.xml b/build/files/docker-entrypoint/upgrade.xml new file mode 100644 index 0000000..dbbf319 --- /dev/null +++ b/build/files/docker-entrypoint/upgrade.xml @@ -0,0 +1,6 @@ + + 1 + 0 + en + 1 + \ No newline at end of file diff --git a/build/files/php-config/port.ini b/build/files/php-config/port.ini deleted file mode 100644 index 323bc62..0000000 --- a/build/files/php-config/port.ini +++ /dev/null @@ -1 +0,0 @@ -listen = 9000 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index f01732c..b374c15 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,26 +12,58 @@ # along with this program. If not, see . version: '3' services: - web: - image: g749/modx-revolution:edge + modx_web: + image: caddy:2 + restart: unless-stopped env_file: .env + depends_on: + - modx_php + ports: + - 80:80 + - 443:443 + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile:ro + - caddy_data:/data:rw + - modx_static_manager:/srv/manager:ro + - modx_assets:/srv/assets:ro + networks: + - http_net + modx_php: + image: g749/modx-revolution:2 + restart: unless-stopped + env_file: .env + depends_on: + - modx_mysql volumes: - modx_config:/modx/core/config:rw - modx_packages:/modx/core/packages:rw - modx_components:/modx/core/components:rw - modx_assets:/modx/public/assets:rw - mysql: + - modx_static_manager:/modx/static/manager:rw + networks: + - http_net + - db_net + modx_mysql: image: 'mariadb:10' + restart: unless-stopped env_file: .env environment: MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: false MARIADB_RANDOM_ROOT_PASSWORD: true volumes: - 'mariadb_data:/var/lib/mysql:rw' + networks: + - db_net volumes: + caddy_data: mariadb_data: modx_config: modx_packages: modx_components: - modx_assets: \ No newline at end of file + modx_assets: + modx_static_manager: + +networks: + db_net: + http_net: \ No newline at end of file diff --git a/example.env b/example.env deleted file mode 100644 index 211ebce..0000000 --- a/example.env +++ /dev/null @@ -1,17 +0,0 @@ -TZ = Europe/Berlin - -MODX_DB_HOST = "mysql:3306" - -MODX_DB_USER = "modx" -MARIADB_USER = modx - -MODX_DB_PASSWORD = "my-strong-random-mysql-password" -MARIADB_PASSWORD = "my-strong-random-mysql-password" - -MODX_DB_NAME = "modx" -MARIADB_DATABASE = modx - -# for fresh install -#MODX_ADMIN_USER=adminuser -#MODX_ADMIN_PASSWORD=adminpassword -#MODX_ADMIN_EMAIL=adminemail \ No newline at end of file