diff --git a/Dockerfile b/Dockerfile index 43196a6..6ef983f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -54,11 +54,11 @@ USER www-data # Define Grav specific version of Grav or use latest stable ARG GRAV_VERSION=latest -# Install grav +# Install grav (skip user dir) WORKDIR /var/www RUN curl -o grav-admin.zip -SL https://getgrav.org/download/core/grav-admin/${GRAV_VERSION} && \ unzip grav-admin.zip && \ - mv -T /var/www/grav-admin /var/www/html && \ + for file in $(ls -A "grav-admin/" | egrep -v user); do mv "grav-admin/$file" "html/"; done && \ rm grav-admin.zip # Create cron job for Grav maintenance scripts @@ -70,9 +70,23 @@ USER root # Copy init scripts # COPY docker-entrypoint.sh /entrypoint.sh +# Create script that populates user dir +RUN { \ + echo '#!/bin/bash'; \ + echo '# Ensure user dir exists'; \ + echo 'mkdir -p /var/www/html/user/'; \ + echo '# Ensure user dir is owned by user www-data'; \ + echo 'chown www-data: /var/www/html/user/'; \ + echo '# Populate user dir if it is empty'; \ + echo 'if test -n "$(find /var/www/html/user -maxdepth 0 -empty)" ; then'; \ + echo 'cp -rp /var/www/grav-admin/user/* /var/www/html/user/'; \ + echo 'fi'; \ + } > /usr/local/bin/populate-userdir && \ + chmod +x /usr/local/bin/populate-userdir + # provide container inside image for data persistence -VOLUME ["/var/www/html"] +VOLUME ["/var/www/html/backup", "/var/www/html/logs", "/var/www/html/user"] # ENTRYPOINT ["/entrypoint.sh"] # CMD ["apache2-foreground"] -CMD ["sh", "-c", "cron && apache2-foreground"] +CMD ["sh", "-c", "populate-userdir && cron && apache2-foreground"] diff --git a/README.md b/README.md index 372b4f5..501780e 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,11 @@ This currently is pretty minimal and uses: ## Persisting data -To save the Grav site data to the host file system (so that it persists even after the container has been removed), simply map the container's `/var/www/html` directory to a named Docker volume or to a directory on the host. +To save the Grav site data to the host file system (so that it persists even after the container has been removed), simply map the container's `/var/www/html/user` directory to a named Docker volume or to a directory on the host. -> If the mapped directory or named volume is empty, it will be automatically populated with a fresh install of Grav the first time that the container starts. However, once the directory/volume has been populated, the data will persist and will not be overwritten the next time the container starts. +> If the mapped directory or named volume is empty, it will be automatically populated with a fresh user dir the first time that the container starts. However, once the directory/volume has been populated, the data will persist and will not be overwritten the next time the container starts. + +This also applies for '/var/www/backup' and '/var/www/logs' except population at first startup. ## Building the image from Dockerfile @@ -35,7 +37,7 @@ Point browser to `http://localhost:8000` and create user account... ## Running Grav Image with Latest Grav + Admin with a named volume (can be used in production) ``` -docker run -d -p 8000:80 --restart always -v grav_data:/var/www/html grav:latest +docker run -d -p 8000:80 --restart always -v grav_backup:/var/www/backup -v grav_logs:/var/www/logs -v gravi_user:/var/www/html/user grav:latest ``` ## Running Grav Image with docker-compose and a volume mapped to a local directory