From 2f182ba55ceffb2f8062873c1a973e0796dc1f9a Mon Sep 17 00:00:00 2001 From: Rob Thomas Date: Fri, 16 Mar 2018 04:37:08 +0000 Subject: [PATCH 1/4] Rewrite docker setup This now allows ANY Stikked configuration variable to be overridden by environment variables. --- .dockerignore | 1 + Dockerfile | 29 +++++++++++----- docker-compose.yml | 11 +++--- docker/docker-php-entrypoint | 66 +++++++++++++++++++++++++++++++++--- docker/replace-envvars.sh | 7 ---- docker/stikked-envvars.txt | 33 ++++++++++++++---- 6 files changed, 114 insertions(+), 33 deletions(-) create mode 100644 .dockerignore delete mode 100644 docker/replace-envvars.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c1c9f4d --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git* diff --git a/Dockerfile b/Dockerfile index 781a08a..8a3acea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,23 @@ -FROM php:7.0-apache -COPY htdocs /var/www/html -COPY htdocs/application/config/stikked.php.dist /var/www/html/application/config/stikked.php -COPY docker/replace-envvars.sh /bin/ -COPY docker/docker-php-entrypoint /usr/local/bin/ -RUN chmod +x /usr/local/bin/docker-php-entrypoint +FROM php:7.1-apache EXPOSE 80 -RUN a2enmod rewrite -RUN docker-php-ext-install mysqli + +# Note that 'vim' and 'mysql-client' are changed to an echo, +# as they're only useful when debugging, and leaving them in +# the standard container only increases its size. +RUN apt-get -y update && \ + apt-get -y install libpng-dev zlib1g-dev cron && \ + echo apt-get -y install vim mysql-client && \ + a2enmod rewrite && \ + docker-php-ext-install mysqli gd && \ + rm -rf /var/lib/apt/lists/* + +COPY htdocs /var/www/html +COPY htdocs/application/config/stikked.php.dist /var/www/html/application/config/stikked.php + +# This overwrites the entrypoint from the php container with ours, which updates the +# stikked config file based on environment variables +COPY docker/docker-php-entrypoint /usr/local/bin/ + +RUN chmod +x /usr/local/bin/docker-php-entrypoint + diff --git a/docker-compose.yml b/docker-compose.yml index a2eec94..2c48307 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,12 @@ -version: '3' +version: '2' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql - environment: - MYSQL_RANDOM_ROOT_PASSWORD: 1 - MYSQL_DATABASE: stikked - MYSQL_USER: stikked - MYSQL_PASSWORD: stikked + env_file: + docker/stikked-envvars.txt stikked: depends_on: @@ -18,7 +15,7 @@ services: env_file: docker/stikked-envvars.txt ports: - - 80:80 + - 8070:80 volumes: db_data: diff --git a/docker/docker-php-entrypoint b/docker/docker-php-entrypoint index f291fe4..24b6327 100644 --- a/docker/docker-php-entrypoint +++ b/docker/docker-php-entrypoint @@ -1,12 +1,70 @@ -#!/bin/sh +#!/bin/bash + +# This is copied from the original docker-php-entrypoint and was updated +# by the Stikkit container + set -e -# custom script to overwrite stikked config variables -bash /bin/replace-envvars.sh +# Check to see where Stikkit might be - If you added Stikkit to this +# container with something like: +# ADD https://github.com/claudehohl/Stikked/archive/0.12.0.tar.gz /usr/local +# then it will be in /usr/local/stikked/Stikked-0.12.0/htdocs/application/config/stikked.php.dist + +# If you're using the standard Dockerfile from Stikkit, it will be in +# /var/www/html/htdocs/applcation/config/stikked.php.dist + +if [ -e /var/www/html/application/config/stikked.php.dist ]; then + CFG=/var/www/html/application/config/stikked.php + cp /var/www/html/application/config/stikked.php.dist $CFG +elif [ -e /usr/local/stikked/Stikked-*/htdocs/application/config/stikked.php.dist ]; then + CFG=$(echo /usr/local/stikked/Stikked-*/htdocs/application/config/stikked.php.dist | sed 's/\.dist//') + cp /usr/local/stikked/Stikked-*/htdocs/application/config/stikked.php.dist $CFG +else + echo I can not find the stikked.php.dist file, which means docker-php-entrypoint + echo needs to be updated. Sorry. I can not continue. Exiting. + exit -1 +fi + +# Set some default variables +STIKKIT_SITE_NAME="${STIKKIT_SITE_NAME:-Dockerised Stikkit Container}" +STIKKIT_BASE_URL="${STIKKIT_BASE_URL:-https://bogus.example.com/}" +STIKKIT_DB_HOSTNAME="${STIKKIT_DB_HOSTNAME:-db}" + +# If these aren't set, use MYSQL_ values. If they're not set, then +# just guess. +STIKKIT_DB_DATABASE="${STIKKIT_DB_DATABASE:-${MYSQL_DATABASE:-stikked}}" +STIKKIT_DB_USERNAME="${STIKKIT_DB_USERNAME:-${MYSQL_USER:-stikked}}" +STIKKIT_DB_PASS="${STIKKIT_DB_PASSWORD:-${MYSQL_PASSWORD:-stikked}}" + +# If there's not a cron key, set a random one. +if [ ! "$STIKKIT_CRON_KEY" ]; then + # Note - this is not very random. But it'll do in a pinch. + STIKKIT_CRON_KEY=$RANDOM.$RANDOM.$RANDOM.$RANDOM +fi + +# Put the cron file in place +echo "*/5 * * * * root curl --silent http://localhost/cron/$STIKKIT_CRON_KEY" > /etc/cron.d/stikkit + +# This gets all environment variables that start with STIKKIT_ +svars=$(set | grep \^STIKKIT_ | cut -d= -f1) +for svar in $svars; do + # Remove STIKKIT_ from the front, and convert it to lower + # case (STIKKIT_CRON_KEY is now cron_key) + val=$(echo $svar | sed 's/STIKKIT_\(.*\)/\L\1/') + # if it has a /, escape it - for example, in a path or URL. + FIXED=$(echo ${!svar} | sed 's_/_\\/_g') + # Tell the user what's going on + echo Setting $val to be $FIXED + # And actually update the file + sed -i "s/\['$val'\].*/['$val'] = '$FIXED';/" $CFG +done + +# Start Cron, if it exists +[ -e /usr/sbin/cron ] && /usr/sbin/cron # first arg is `-f` or `--some-option` if [ "${1#-}" != "$1" ]; then - set -- php "$@" + set -- apache2-foreground "$@" fi exec "$@" diff --git a/docker/replace-envvars.sh b/docker/replace-envvars.sh deleted file mode 100644 index edf28b4..0000000 --- a/docker/replace-envvars.sh +++ /dev/null @@ -1,7 +0,0 @@ -sed -i "s/\['site_name'\].*/['site_name'] = '$SITENAME';/" /var/www/html/application/config/stikked.php -sed -i "s/\['base_url'\].*/['base_url'] = '$BASEURL';/" /var/www/html/application/config/stikked.php -sed -i "s/\['db_hostname'\].*/['db_hostname'] = '$DBHOST';/" /var/www/html/application/config/stikked.php -sed -i "s/\['db_database'\].*/['db_database'] = '$DBNAME';/" /var/www/html/application/config/stikked.php -sed -i "s/\['db_username'\].*/['db_username'] = '$DBUSER';/" /var/www/html/application/config/stikked.php -sed -i "s/\['db_password'\].*/['db_password'] = '$DBPASS';/" /var/www/html/application/config/stikked.php -sed -i "s/\['enable_captcha'\].*/['enable_captcha'] = '$CAPTHCA';/" /var/www/html/application/config/stikked.php diff --git a/docker/stikked-envvars.txt b/docker/stikked-envvars.txt index 574d65e..2f9ab3a 100644 --- a/docker/stikked-envvars.txt +++ b/docker/stikked-envvars.txt @@ -1,7 +1,26 @@ -SITENAME=Stikked -BASEURL=http:\/\/stikked.local\/ -DBHOST=db -DBNAME=stikked -DBUSER=stikked -DBPASS=stikked -CAPTCHA=false +MYSQL_ROOT_PASSWORD=thisREALLYshouldBEchanged +MYSQL_DATABASE=stikked +MYSQL_USER=stikked +MYSQL_PASSWORD=stikked + +STIKKED_SITE_NAME=Stikked +STIKKED_BASE_URL=http:\/\/stikked.local\/ + +# This should match the database container name +STIKKIT_DB_HOSTNAME=db + +# These do NOT need to be set, as they will be inherited from +# the MYSQL_DATABASE settings above. However, you can set them +# if you are using a seperately managed database. +#STIKKIT_DB_DATABASE=stikked +#STIKKIT_DB_USERNAME=stikked +#STIKKIT_DB_PASS=stikked + +# Other random examples +STIKKIT_DISALLOW_SEARCH_ENGINES="true" +STIKKIT_JS_EDITOR="codemirror" + +# Example of enabling CAPTCHA +#STIKKIT_ENABLE_CAPTCHA="true" +#STIKKIT_RECAPTCHA_PUBLICKEY="_replace_this_with_your_public_key_" +#STIKKIT_RECAPTCHA_PRIVATEKEY="_replace_this_with_your_private_key_" From 0ef7cbf4c66dab1c43fd67425955d3705208525d Mon Sep 17 00:00:00 2001 From: Rob Thomas Date: Fri, 16 Mar 2018 04:44:31 +0000 Subject: [PATCH 2/4] Remove debugging left in there --- docker-compose.yml | 2 +- docker/stikked-envvars.txt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 2c48307..6c3b27d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ services: env_file: docker/stikked-envvars.txt ports: - - 8070:80 + - 80:80 volumes: db_data: diff --git a/docker/stikked-envvars.txt b/docker/stikked-envvars.txt index 2f9ab3a..d511f70 100644 --- a/docker/stikked-envvars.txt +++ b/docker/stikked-envvars.txt @@ -4,7 +4,8 @@ MYSQL_USER=stikked MYSQL_PASSWORD=stikked STIKKED_SITE_NAME=Stikked -STIKKED_BASE_URL=http:\/\/stikked.local\/ +# Note that there is no need to escape the URL +STIKKED_BASE_URL=http://stikked.local/ # This should match the database container name STIKKIT_DB_HOSTNAME=db From f3399c45df1c9a25f20f832aeec0e581b431c0c8 Mon Sep 17 00:00:00 2001 From: Rob Thomas Date: Fri, 16 Mar 2018 05:02:48 +0000 Subject: [PATCH 3/4] Better documentation in docker-compose Also put the version back to 3, which was a typo. --- docker-compose.yml | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6c3b27d..f06d1de 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,29 @@ -version: '2' +version: "3.2" services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql - env_file: - docker/stikked-envvars.txt + env_file: docker/stikked-envvars.txt stikked: depends_on: - db image: stikked - env_file: - docker/stikked-envvars.txt + env_file: docker/stikked-envvars.txt ports: - 80:80 +# You should use persistant storage for this, +# as if the volume is deleted, everything is gone! volumes: db_data: + +# Example of NFS backed persistant storage: +# db_data: +# driver_opts: +# type: "nfs" +# o: "addr=192.168.1.254,nolock,soft,rw" +# device: ":/nfs/export/pbdatabase" + From cf6b35796796f02b53d0983a7044fbf499340d54 Mon Sep 17 00:00:00 2001 From: Rob Thomas Date: Fri, 16 Mar 2018 21:17:03 +0000 Subject: [PATCH 4/4] Fix brain-typo. Sorry about that! Somehow I managed to type STIKKIT instead of STIKKED all those times. I honestly have no idea how I managed to get that into my head. --- docker/docker-php-entrypoint | 34 +++++++++++++++++----------------- docker/stikked-envvars.txt | 18 +++++++++--------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/docker/docker-php-entrypoint b/docker/docker-php-entrypoint index 24b6327..83447f5 100644 --- a/docker/docker-php-entrypoint +++ b/docker/docker-php-entrypoint @@ -1,16 +1,16 @@ #!/bin/bash # This is copied from the original docker-php-entrypoint and was updated -# by the Stikkit container +# by the Stikked container set -e -# Check to see where Stikkit might be - If you added Stikkit to this +# Check to see where Stikked might be - If you added Stikked to this # container with something like: # ADD https://github.com/claudehohl/Stikked/archive/0.12.0.tar.gz /usr/local # then it will be in /usr/local/stikked/Stikked-0.12.0/htdocs/application/config/stikked.php.dist -# If you're using the standard Dockerfile from Stikkit, it will be in +# If you're using the standard Dockerfile from Stikked, it will be in # /var/www/html/htdocs/applcation/config/stikked.php.dist if [ -e /var/www/html/application/config/stikked.php.dist ]; then @@ -26,31 +26,31 @@ else fi # Set some default variables -STIKKIT_SITE_NAME="${STIKKIT_SITE_NAME:-Dockerised Stikkit Container}" -STIKKIT_BASE_URL="${STIKKIT_BASE_URL:-https://bogus.example.com/}" -STIKKIT_DB_HOSTNAME="${STIKKIT_DB_HOSTNAME:-db}" +STIKKED_SITE_NAME="${STIKKED_SITE_NAME:-Dockerised Stikked Container}" +STIKKED_BASE_URL="${STIKKED_BASE_URL:-https://bogus.example.com/}" +STIKKED_DB_HOSTNAME="${STIKKED_DB_HOSTNAME:-db}" # If these aren't set, use MYSQL_ values. If they're not set, then # just guess. -STIKKIT_DB_DATABASE="${STIKKIT_DB_DATABASE:-${MYSQL_DATABASE:-stikked}}" -STIKKIT_DB_USERNAME="${STIKKIT_DB_USERNAME:-${MYSQL_USER:-stikked}}" -STIKKIT_DB_PASS="${STIKKIT_DB_PASSWORD:-${MYSQL_PASSWORD:-stikked}}" +STIKKED_DB_DATABASE="${STIKKED_DB_DATABASE:-${MYSQL_DATABASE:-stikked}}" +STIKKED_DB_USERNAME="${STIKKED_DB_USERNAME:-${MYSQL_USER:-stikked}}" +STIKKED_DB_PASS="${STIKKED_DB_PASSWORD:-${MYSQL_PASSWORD:-stikked}}" # If there's not a cron key, set a random one. -if [ ! "$STIKKIT_CRON_KEY" ]; then +if [ ! "$STIKKED_CRON_KEY" ]; then # Note - this is not very random. But it'll do in a pinch. - STIKKIT_CRON_KEY=$RANDOM.$RANDOM.$RANDOM.$RANDOM + STIKKED_CRON_KEY=$RANDOM.$RANDOM.$RANDOM.$RANDOM fi # Put the cron file in place -echo "*/5 * * * * root curl --silent http://localhost/cron/$STIKKIT_CRON_KEY" > /etc/cron.d/stikkit +echo "*/5 * * * * root curl --silent http://localhost/cron/$STIKKED_CRON_KEY" > /etc/cron.d/stikked -# This gets all environment variables that start with STIKKIT_ -svars=$(set | grep \^STIKKIT_ | cut -d= -f1) +# This gets all environment variables that start with STIKKED_ +svars=$(set | grep \^STIKKED_ | cut -d= -f1) for svar in $svars; do - # Remove STIKKIT_ from the front, and convert it to lower - # case (STIKKIT_CRON_KEY is now cron_key) - val=$(echo $svar | sed 's/STIKKIT_\(.*\)/\L\1/') + # Remove STIKKED_ from the front, and convert it to lower + # case (STIKKED_CRON_KEY is now cron_key) + val=$(echo $svar | sed 's/STIKKED_\(.*\)/\L\1/') # if it has a /, escape it - for example, in a path or URL. FIXED=$(echo ${!svar} | sed 's_/_\\/_g') # Tell the user what's going on diff --git a/docker/stikked-envvars.txt b/docker/stikked-envvars.txt index d511f70..5a74990 100644 --- a/docker/stikked-envvars.txt +++ b/docker/stikked-envvars.txt @@ -8,20 +8,20 @@ STIKKED_SITE_NAME=Stikked STIKKED_BASE_URL=http://stikked.local/ # This should match the database container name -STIKKIT_DB_HOSTNAME=db +STIKKED_DB_HOSTNAME=db # These do NOT need to be set, as they will be inherited from # the MYSQL_DATABASE settings above. However, you can set them # if you are using a seperately managed database. -#STIKKIT_DB_DATABASE=stikked -#STIKKIT_DB_USERNAME=stikked -#STIKKIT_DB_PASS=stikked +#STIKKED_DB_DATABASE=stikked +#STIKKED_DB_USERNAME=stikked +#STIKKED_DB_PASS=stikked # Other random examples -STIKKIT_DISALLOW_SEARCH_ENGINES="true" -STIKKIT_JS_EDITOR="codemirror" +STIKKED_DISALLOW_SEARCH_ENGINES="true" +STIKKED_JS_EDITOR="codemirror" # Example of enabling CAPTCHA -#STIKKIT_ENABLE_CAPTCHA="true" -#STIKKIT_RECAPTCHA_PUBLICKEY="_replace_this_with_your_public_key_" -#STIKKIT_RECAPTCHA_PRIVATEKEY="_replace_this_with_your_private_key_" +#STIKKED_ENABLE_CAPTCHA="true" +#STIKKED_RECAPTCHA_PUBLICKEY="_replace_this_with_your_public_key_" +#STIKKED_RECAPTCHA_PRIVATEKEY="_replace_this_with_your_private_key_"