Skip to content

Commit

Permalink
Populate user dir once (getgrav#44)
Browse files Browse the repository at this point in the history
I found another way to install grav by populating user dir if empty during container start.
Therefore i skip user dir during build. A script will then populate /var/www/user as described in README.md
This commit also includes https://github.com/Freiheitswolke/docker-grav/tree/fix/volume and some changes to README.md to reflect the changed volumes.
The commit will not break the old way with a mount for the whole '/var/www/html' for those who want to upgrade grav via the admin console.
Building or pulling the container image with the new version of grav is a more proper way to perform upgrades in my opinion.

Closes getgrav#44
  • Loading branch information
fanks4 committed Mar 18, 2023
1 parent ebdf201 commit 7aed3fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 16 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,9 +70,21 @@ 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 'sudo -iu www-data mkdir -p /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"]
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 7aed3fb

Please sign in to comment.