Skip to content

Commit

Permalink
feat: autoupdating works now also added working example docker-compos…
Browse files Browse the repository at this point in the history
…e.yml
  • Loading branch information
Giesenberg, Jan committed Nov 25, 2021
1 parent 443c523 commit 9063197
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 145 deletions.
23 changes: 23 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=[email protected]

# 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
20 changes: 20 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# ModX Revolution Docker
A docker setup that can autoupdate ModX Revolution.
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`
42 changes: 25 additions & 17 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,45 @@ FROM php:7-fpm-alpine

LABEL org.opencontainers.image.authors="Jan Giesenberg <[email protected]>"

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"]
149 changes: 44 additions & 105 deletions build/files/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
<?php
$stderr = fopen('php://stderr', 'w');
list($host, $port) = explode(':', $argv[1], 2);
$maxTries = 10;
do {
$mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
if ($mysql->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" </docker-entrypoint/create-database.php

$mysql->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 <<EOF
<modx>
<database_type>mysql</database_type>
<database_server>$MODX_DB_HOST</database_server>
<database>$MODX_DB_NAME</database>
<database_user>$MODX_DB_USER</database_user>
<database_password>$MODX_DB_PASSWORD</database_password>
<database_connection_charset>utf8</database_connection_charset>
<database_charset>utf8</database_charset>
<database_collation>utf8_general_ci</database_collation>
<table_prefix>$MODX_TABLE_PREFIX</table_prefix>
<https_port>443</https_port>
<http_host>localhost</http_host>
<cache_disabled>0</cache_disabled>
<inplace>1</inplace>
<unpacked>0</unpacked>
<language>en</language>
<cmsadmin>$MODX_ADMIN_USER</cmsadmin>
<cmspassword>$MODX_ADMIN_PASSWORD</cmspassword>
<cmsadminemail>$MODX_ADMIN_EMAIL</cmsadminemail>
<core_path>/modx/core/</core_path>
<context_mgr_path>/modx/public/manager/</context_mgr_path>
<context_mgr_url>/manager/</context_mgr_url>
<context_connectors_path>/modx/public/connectors/</context_connectors_path>
<context_connectors_url>/connectors/</context_connectors_url>
<context_web_path>/modx/public/</context_web_path>
<context_web_url>/</context_web_url>
<remove_setup_directory>1</remove_setup_directory>
</modx>
EOF
envsubst >setup/config.xml </docker-entrypoint/install.xml

chown www-data:www-data setup/config.xml

sudo -u www-data php setup/index.php --installmode=new
else
UPGRADE=$(TERM=dumb php -- "$MODX_VERSION" <<'EOPHP'
<?php
define('MODX_API_MODE', true);
require_once 'index.php';

if (version_compare($modx->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" </docker-entrypoint/compare-version.php)

if [ $UPGRADE ]; then
if [ "$UPGRADE" -eq "1" ]; then
echo >&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 </docker-entrypoint/upgrade.xml

cat > setup/config.xml <<EOF
<modx>
<inplace>1</inplace>
<unpacked>0</unpacked>
<language>en</language>
<remove_setup_directory>1</remove_setup_directory>
</modx>
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 "$@"
9 changes: 9 additions & 0 deletions build/files/docker-entrypoint/compare-version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$currentInstallVersion = trim(file_get_contents('/modx/core/config/install_version.txt'));
if($currentInstallVersion == '') {
echo '/modx/core/config/install_version.txt does not contain a valid version number!';
} else if (version_compare($currentInstallVersion, $argv[1]) < 0) {
echo '1';
} else {
echo '0';
}
27 changes: 27 additions & 0 deletions build/files/docker-entrypoint/create-database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$stderr = fopen('php://stderr', 'w');

list($host, $port) = explode(':', $argv[1], 2);

$maxTries = 10;
do {
$mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
if ($mysql->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();
32 changes: 32 additions & 0 deletions build/files/docker-entrypoint/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<modx>
<database_type>mysql</database_type>
<database_server>$MODX_DB_HOST</database_server>
<database>$MODX_DB_NAME</database>
<database_user>$MODX_DB_USER</database_user>
<database_password>$MODX_DB_PASSWORD</database_password>
<database_connection_charset>utf8</database_connection_charset>
<database_charset>utf8</database_charset>
<database_collation>utf8_general_ci</database_collation>
<table_prefix>$MODX_TABLE_PREFIX</table_prefix>
<https_port>443</https_port>
<http_host>localhost</http_host>
<cache_disabled>0</cache_disabled>

<inplace>1</inplace>
<unpacked>0</unpacked>
<language>en</language>

<cmsadmin>$MODX_ADMIN_USER</cmsadmin>
<cmspassword>$MODX_ADMIN_PASSWORD</cmspassword>
<cmsadminemail>$MODX_ADMIN_EMAIL</cmsadminemail>

<core_path>/modx/core/</core_path>
<context_mgr_path>/modx/public/manager/</context_mgr_path>
<context_mgr_url>/manager/</context_mgr_url>
<context_connectors_path>/modx/public/connectors/</context_connectors_path>
<context_connectors_url>/connectors/</context_connectors_url>
<context_web_path>/modx/public/</context_web_path>
<context_web_url>/</context_web_url>

<remove_setup_directory>1</remove_setup_directory>
</modx>
6 changes: 6 additions & 0 deletions build/files/docker-entrypoint/upgrade.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<modx>
<inplace>1</inplace>
<unpacked>0</unpacked>
<language>en</language>
<remove_setup_directory>1</remove_setup_directory>
</modx>
1 change: 0 additions & 1 deletion build/files/php-config/port.ini

This file was deleted.

Loading

0 comments on commit 9063197

Please sign in to comment.