From b7bc34a36ced89b8be1353c51510c4719f2c0d80 Mon Sep 17 00:00:00 2001 From: Andrei Poenaru Date: Tue, 13 Mar 2018 21:07:21 +0000 Subject: [PATCH] Add support for storing data in bind-mounted directories --- Dockerfile | 8 +++--- README.md | 20 +++++++++++++- docker-startup.sh | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100755 docker-startup.sh diff --git a/Dockerfile b/Dockerfile index 97ac265..990eb36 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,8 +33,7 @@ RUN DEBIAN_FRONTEND=noninteractive \ RUN wget -q -O /dokuwiki.tgz "http://download.dokuwiki.org/src/dokuwiki/dokuwiki-$DOKUWIKI_VERSION.tgz" && \ if [ "$DOKUWIKI_CSUM" != "$(md5sum /dokuwiki.tgz | awk '{print($1)}')" ];then echo "Wrong md5sum of downloaded file!"; exit 1; fi && \ mkdir /dokuwiki && \ - tar -zxf dokuwiki.tgz -C /dokuwiki --strip-components 1 && \ - rm dokuwiki.tgz + tar -zxf dokuwiki.tgz -C /dokuwiki --strip-components 1 # Set up ownership RUN chown -R www-data:www-data /dokuwiki @@ -44,8 +43,11 @@ ADD dokuwiki.conf /etc/lighttpd/conf-available/20-dokuwiki.conf RUN lighty-enable-mod dokuwiki fastcgi accesslog RUN mkdir /var/run/lighttpd && chown www-data.www-data /var/run/lighttpd +COPY docker-startup.sh /startup.sh + EXPOSE 80 VOLUME ["/dokuwiki/data/","/dokuwiki/lib/plugins/","/dokuwiki/conf/","/dokuwiki/lib/tpl/","/var/log/"] -ENTRYPOINT ["/usr/sbin/lighttpd", "-D", "-f", "/etc/lighttpd/lighttpd.conf"] +ENTRYPOINT ["/startup.sh"] +CMD ["run"] diff --git a/README.md b/README.md index 3725cf8..e7261d9 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,17 @@ You can now visit the install page to configure your new DokuWiki wiki. For example, if you are running container locally, you can acces the page in browser by going to http://127.0.0.1/install.php +### Run with bind mounts ### + +The run command above will store your data in internal Docker volumes. If you prefer to bind mount directories on your file system, then you can use the `-v` parameter, for example: + + docker run -d -p 80:80 --name my_wiki \ + -v /data/docker/dokuwiki/data:/dokuwiki/data \ + -v /data/docker/dokuwiki/conf:/dokuwiki/conf \ + -v /data/docker/dokuwiki/lib/plugins:/dokuwiki/lib/plugins \ + -v /data/docker/dokuwiki/lib/tpl:/dokuwiki/lib/tpl \ + -v /data/docker/dokuwiki/logs:/var/log + ### Run a specific version ### When running the container you can specify version to download from docker registry by using couple provided tags like *stable* which contains current stable release (this is generaly the same as *latest*) or *oldstable*. You can also use specific version like *2016-06-26a*. @@ -20,6 +31,8 @@ When running the container you can specify version to download from docker regis To upate the image: ------------------- +### With internal volumes ### + First stop your container docker stop my_wiki @@ -42,6 +55,10 @@ afterwards you can remove data container if you want (or keep it for next update, takes no space anyway..) +### With bind mounts ### + +Just stop the old container and run the new one with the same `-v` mapping. Since the data is on your file system, you do not need to do anything special to preserve it between containers. + Optimizing your wiki -------------------- @@ -54,4 +71,5 @@ Set to proprietary lighttpd header (for lighttpd < 1.5) Build your own -------------- - docker build -t my_dokuwiki . \ No newline at end of file + docker build -t my_dokuwiki . + diff --git a/docker-startup.sh b/docker-startup.sh new file mode 100755 index 0000000..bf30f30 --- /dev/null +++ b/docker-startup.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -eu +set -o pipefail + +# Updating DokuWiki may need to change files in directories which we hold in +# volumes, e.g. `data` or `conf`. Therefore, we need to make sure these files +# are there when the container is started, not only when it is created. We do +# this by keeping track which version we last installed and update as necessary, +# or fully populate these folders if this is the first run. + +dokudir=/dokuwiki +verfile=.last-version + +if [ ! -d "$dokudir" ]; then + echo "DokuWiki does not appear to be installed correctly at: $dokudir." >&2 + exit 1 +fi + +# Unpack a temporary copy +tmpdir=/tmp/dokuwiki +mkdir "$tmpdir" +tar -zxf /dokuwiki.tgz -C "$tmpdir" --strip-components 1 +containerver="$(date -f <(awk '{print $1}' "$tmpdir/VERSION" | tr -d '[:alpha:]') +%s)" + +# Check for downgrade/overwrite parameters +if [ "$1" = 'downgrade' ]; then downgrade=1; else downgrade=0; fi +if [ "$1" = 'overwrite' ]; then overwrite=1; else overwrite=0; fi + +if [ "$1" = 'run' ] || [ "$1" = 'downgrade' ] || [ "$1" = 'overwrite' ]; then + # Check each volume directory in turn + for d in conf data lib/plugins lib/tpl; do + if [ -f "$dokudir/$d/$verfile" ]; then + volumever="$(date -f <(awk '{print $1}' "$dokudir/$d/$verfile" | tr -d '[:alpha:]') +%s)" + else + volumever=0 + fi + + if [ "$volumever" -eq "$containerver" ] && [ ! "$overwrite" -eq 1 ]; then + # Do nothing for equal versions + continue + elif [ "$volumever" -lt "$containerver" ] || [ "$downgrade" -eq 1 ] || [ "$overwrite" -eq 1 ]; then + # Update if the container version is newer than the volume version + # Or if overridden using `downgrade` + echo "Migrating $d..." + cp -r "$tmpdir/$d/"* "$dokudir/$d/" + cp "$tmpdir/VERSION" "$dokudir/$d/$verfile" + elif [ "$volumever" -gt "$containerver" ]; then + # Otherwise print an error message and stop + cat >&2 <