Skip to content

Commit 7aed3fb

Browse files
committed
Populate user dir once (getgrav#44)
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
1 parent ebdf201 commit 7aed3fb

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Dockerfile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ USER www-data
5454
# Define Grav specific version of Grav or use latest stable
5555
ARG GRAV_VERSION=latest
5656

57-
# Install grav
57+
# Install grav (skip user dir)
5858
WORKDIR /var/www
5959
RUN curl -o grav-admin.zip -SL https://getgrav.org/download/core/grav-admin/${GRAV_VERSION} && \
6060
unzip grav-admin.zip && \
61-
mv -T /var/www/grav-admin /var/www/html && \
61+
for file in $(ls -A "grav-admin/" | egrep -v user); do mv "grav-admin/$file" "html/"; done && \
6262
rm grav-admin.zip
6363

6464
# Create cron job for Grav maintenance scripts
@@ -70,9 +70,21 @@ USER root
7070
# Copy init scripts
7171
# COPY docker-entrypoint.sh /entrypoint.sh
7272

73+
# Create script that populates user dir
74+
RUN { \
75+
echo '#!/bin/bash'; \
76+
echo '# Ensure user dir exists'; \
77+
echo 'sudo -iu www-data mkdir -p /var/www/html/user/'; \
78+
echo '# Populate user dir if it is empty'; \
79+
echo 'if test -n "$(find /var/www/html/user -maxdepth 0 -empty)" ; then'; \
80+
echo 'cp -rp /var/www/grav-admin/user/* /var/www/html/user/'; \
81+
echo 'fi'; \
82+
} > /usr/local/bin/populate-userdir && \
83+
chmod +x /usr/local/bin/populate-userdir
84+
7385
# provide container inside image for data persistence
74-
VOLUME ["/var/www/html"]
86+
VOLUME ["/var/www/html/backup", "/var/www/html/logs", "/var/www/html/user"]
7587

7688
# ENTRYPOINT ["/entrypoint.sh"]
7789
# CMD ["apache2-foreground"]
78-
CMD ["sh", "-c", "cron && apache2-foreground"]
90+
CMD ["sh", "-c", "populate-userdir && cron && apache2-foreground"]

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ This currently is pretty minimal and uses:
1414

1515
## Persisting data
1616

17-
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.
17+
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.
1818

19-
> 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.
19+
> 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.
20+
21+
This also applies for '/var/www/backup' and '/var/www/logs' except population at first startup.
2022

2123
## Building the image from Dockerfile
2224

@@ -35,7 +37,7 @@ Point browser to `http://localhost:8000` and create user account...
3537
## Running Grav Image with Latest Grav + Admin with a named volume (can be used in production)
3638

3739
```
38-
docker run -d -p 8000:80 --restart always -v grav_data:/var/www/html grav:latest
40+
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
3941
```
4042

4143
## Running Grav Image with docker-compose and a volume mapped to a local directory

0 commit comments

Comments
 (0)