Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize db on start up / support k8s example template #13

Merged
merged 1 commit into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 06_initialize_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
INIT_DB=${DB_PASSWORD:-FALSE}
IDB_HOST=${DB_HOST:-localhost}
IDB_PORT=${DB_PORT:-3306}
IDB_USER=${DB_USER:-root}
IDB_PASSWORD=${DB_PASSWORD:-password}
IDB_DB_NAME=${DB_NAME:-webtrees}
IDB_DB_PREFIX="wt_"
IDB_WT_ADMIN=${WT_ADMIN:-admin}
IDB_WT_ADMINPW=${WT_ADMINPW:-admin123}
IDB_WT_ADMINMAIL=${WT_ADMINMAIL:-noreply@webtrees.net}

#Check if initial database configuration should be set
if [ "$INIT_DB" != "FALSE" ]
then
#Check if not yet set, if file exists, do nothing
CONFIG_FILE=/var/www/html/data/config.ini.php
if [ -f "$CONFIG_FILE" ]; then
echo "Configuration file $CONFIG_FILE yet exist. No settings will be modified."
else
echo "Creating the initial database settings in configuration file $CONFIG_FILE and creating database."
cp /config.ini.php "$CONFIG_FILE"
sed -i 's/<DB_HOST>/'"$IDB_HOST"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PORT>/'"$IDB_PORT"'/g' "$CONFIG_FILE"
sed -i 's/<DB_USER>/'"$IDB_USER"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PASSWORD>/'"$IDB_PASSWORD"'/g' "$CONFIG_FILE"
sed -i 's/<DB_NAME>/'"$IDB_DB_NAME"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PREFIX>/'"$IDB_DB_PREFIX"'/g' "$CONFIG_FILE"
chown www-data:docker-data "$CONFIG_FILE"
chmod 660 "$CONFIG_FILE"
#Create database structure and add admin user
cp /webtrees.sql /mod_webtrees.sql
sed -i 's/<DB_NAME>/'"$IDB_DB_NAME"'/g' /mod_webtrees.sql
sed -i 's/<WT_ADMIN_NAME>/'"$IDB_WT_ADMIN"'/g' /mod_webtrees.sql
#Encode password and escape for sed
WTCRYPT=$(php -r "echo crypt('$IDB_WT_ADMINPW', '');")
sed -i 's/<WT_ADMIN_PW>/'"$(echo $WTCRYPT | sed -e 's/[]\/$*.^[]/\\&/g')"'/g' /mod_webtrees.sql
sed -i 's/<WT_ADMIN_MAIL>/'"$IDB_WT_ADMINMAIL"'/g' /mod_webtrees.sql
#Write to database
mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" < /mod_webtrees.sql
#Alternative to set Webtrees admin user:
#echo "UPDATE wt_user SET user_name='$IDB_WT_ADMIN', email='$IDB_WT_ADMINMAIL', real_name='Admin', password='$WTCRYPT' WHERE user_id=1" | mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" "$IDB_DB_NAME"
fi
fi
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ RUN apt-get upgrade -qy && apt-get install -qy \
php \
libapache2-mod-php \
php-mysql \
php-pgsql \
php-pdo-odbc \
php-curl \
php-gd \
php-intl \
Expand All @@ -43,6 +45,8 @@ RUN apt-get upgrade -qy && apt-get install -qy \
php-zip \
wget \
unzip \
sed \
mysql-client \
&& a2enmod ssl \
&& a2enmod rewrite \
&& a2enmod headers \
Expand All @@ -69,17 +73,21 @@ COPY 02_auto_update.sh ${start_scripts_path}
COPY 03_set_a2port.sh ${start_scripts_path}
COPY 04_enable_REMOTE_USER.sh ${start_scripts_path}
COPY 05_switch_http_https.sh ${start_scripts_path}
COPY 06_initialize_db.sh ${start_scripts_path}
COPY start.sh /start.sh
RUN chmod +x ${start_scripts_path}/01_user_config.sh \
&& chmod +x ${start_scripts_path}/02_auto_update.sh \
&& chmod +x ${start_scripts_path}/03_set_a2port.sh \
&& chmod +x ${start_scripts_path}/04_enable_REMOTE_USER.sh \
&& chmod +x ${start_scripts_path}/05_switch_http_https.sh \
&& chmod +x ${start_scripts_path}/06_initialize_db.sh \
&& chmod +x /start.sh

CMD ["./start.sh"]

ADD Auth.php /Auth.php
ADD config.ini.php /config.ini.php
ADD webtrees.sql /webtrees.sql

#Add Apache configuration
ADD php.ini /etc/php/7.2/apache2/
Expand Down
63 changes: 46 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
![Docker Build Status](https://img.shields.io/docker/build/dtjs48jkt/webtrees?style=plastic) ![Travis (.com) branch](https://img.shields.io/travis/com/H2CK/webtrees/master?label=multi-arch%20build&style=plastic) ![Docker Pulls](https://img.shields.io/docker/pulls/dtjs48jkt/webtrees?style=plastic) ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/H2CK/webtrees?style=plastic)
![Travis (.com) branch](https://img.shields.io/travis/com/H2CK/webtrees/master?label=multi-arch%20build&style=plastic) ![Docker Pulls](https://img.shields.io/docker/pulls/dtjs48jkt/webtrees?style=plastic) ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/H2CK/webtrees?style=plastic)

# docker-webtrees

[webtrees](http://www.webtrees.net) is a free open source web-based genealogy application intended for collaborative use.
It is compatible with standard 5.5.1-GEDCOM files. In this docker image based on phusion webtrees is provided. A database is not embedded in this image.

Expand All @@ -11,7 +13,7 @@ Supported architectures are: amd64, arm32v7, arm64v8, i386, ppc64le, s390x

## Usage

```
```sh
docker run -d -p 8088:8079 --name webtrees --link mysql:db -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e GROUP_ID=999 -e PORT=8079 --restart always dtjs48jkt/webtrees
```

Expand All @@ -20,56 +22,83 @@ After starting the docker container go to:
https://server or https://server:port if you have defined an alternative port

## Update Functionality

This docker image is based on Ubuntu. On each start of the container an update of the used Ubuntu packages could be performed. Due to the running update it might take a little longer until the application webtrees is available. This auto-update functionality has (now) to be activated explicitely (-e UPDATE_ON_START=TRUE).

## Persistent storage of data

Configuration and media files should be stored outside the container. Therefor you should create two directories that are mapped to the container internal directories /var/www/html/data and /var/www/html/media.
In the container apache is running under user www-data [33] (group www-data[33]). Both directories must therfore be read- and writable for this user. If this is not possible you can use the alternative and use the parameter GROUP_ID to inform the container about the group that has read and write access to those folders.

## Database

The image does not contain a MySQL database. Instead you have to use a separate MySQL instance. For example you could use the [MySQL Docker Image](https://store.docker.com/images/mysql). Using the --link parameter a direct connection to the database in an other container could be established.
If you use the --link parameter it is sufficient to set as database hostname db and port 3306. This can be set during the initial setup in the wizard or directly in the file config.ini.php (in data directory /var/www/html/data). The database user must have all access rights to create the necessary database and tables.

## Logging

Log data of the contained web-server is written in the files in the folder /var/log/apache2/. If access to those files is necessary this location could be mapped to an external volume.

## Port of Apache web server / Encryption

This image supports per default https based communication on port 443. Alternatively you can use this image also only using http, e.g. if you only use it within your home network.

### Details on encryption

If it is necessary to change the default port (e.g. in case of collisions) you can set the optional parameter PORT to a different value.
The https communication is based on a self signed certificate. It is possible to use an alternative certificate. Therfore you have to map the internal folder /crt to an external location. This folder should contain the two files webtrees.key (Key without password protection) und webtrees.crt (certificate). It is not possible to change further encryption settings from outside the container.
If you want a more sofisticated encryption you should use a reverse proxy in front of the webtrees container.

### Using http only

It is possible to use the image without https support. For that you have to start the container with the following environament variables set.

```
```sh
docker run -d -p 80:80 --name webtrees --link mysql:db -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
```

## Usage of additional 3rd party modules
It is possible to use additional 3rd party modules of webtrees with this container. To use such modules it is necessary to mount those folders containing the module into the following location -v /var/www/html/modulesv3/<modulexxx>

It is possible to use additional 3rd party modules of webtrees with this container. To use such modules it is necessary to mount those folders containing the module into the following location `-v /var/www/html/modulesv3/<modulexxx>`

## Automatic initialization of database

For MySQL or MariaDB it is possible to use an automatical initalization of the database within the application. User the parameters to DB_* and WT_* to set the corresponding values. To possible error of the initialization start the container in interactve mode using -it.

## Support for PostgreSQL or SQLServer

This image contains now the necessary libraries to optionally also select PostgreSQL or SQLServer as db storage in the setup wizard.

## Parameters

* `-v /var/www/html/data` - Where webtrees server stores its config files
* `-v /var/www/html/media` - Where webtrees server stores its media files
* `-v /etc/localtime` - Set to use the correct time of host
* `-v /etc/localtime` - Set to use the correct time of host
* `-e GROUP_ID` - allow access to mapped volumes
* `-e PORT` - change port web server listens on
* `-e UPDATE_ON_START` - if set to TRUE the auto-update functionality on restart is activated
* `-e ENABLE_REMOTE_USER` - if set to TRUE use REMOTE_USER for authentication
* `-e DISABLE_SSL` - if set to TRUE the image only provides an http entpoint. You should also set the port, because default port 443 is not modifed by this setting.
* `-e DISABLE_SSL` - if set to TRUE the image only provides an http entpoint. You should also set the port, because default port 443 is not modifed by this setting.
* `-e DB_USER` - can only be used at first instantiation to set the db username. Password (DB_PASSWORD) must also be set (see below), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be root.
* `-e DB_PASSWORD` - can only be used at first instantiation to set the db setting during instantiation and perform initial application setup.
* `-e DB_HOST` - can only be used at first instantiation to set the db hostname (or ip). Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be localhost.
* `-e DB_PORT` - can only be used at first instantiation to set the db port. Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be 3306.
* `-e DB_NAME` - can only be used at first instantiation to set the db name. Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be webtrees.
* `-e WT_ADMIN` - can only be used at first instantiation to set the initial webtrees admin user account name. Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be admin.
* `-e WT_ADMINPW` - can only be used at first instantiation to set the initial webtrees admin user account password. Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be admin123.
* `-e WT_ADMINMAIL` - can only be used at first instantiation to set the initial webtrees admin user account mail address. Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be noreply@webtrees.net.

## Versions
+ **2017/10/13:** Initial release. webtrees 1.7.9
+ **2018/06/27:** Webtrees 1.7.9 - Added switch to active/deactive auto update functionality
+ **2018/08/30:** Webtrees 1.7.10 - Now using baseimage Ubuntu 18.04 / Added possibility to use external authentication using REMOTE_USER
+ **2019/01/13:** Webtrees 1.7.12
+ **2019/05/08:** Webtrees 1.7.14
+ **2019/12/10:** Webtrees 1.7.16
+ **2019/12/10:** Webtrees 2.0.0
+ **2020/01/06:** Webtrees 2.0.1
+ **2020/01/13:** Webtrees 2.0.1 - Added possibility to use image without https (only http) for local networks
+ **2020/01/16:** Webtrees 2.0.1 - Added multi arch support
+ **2020/02/01:** Webtrees 2.0.2

* **2017/10/13:** Initial release. webtrees 1.7.9
* **2018/06/27:** Webtrees 1.7.9 - Added switch to active/deactive auto update functionality
* **2018/08/30:** Webtrees 1.7.10 - Now using baseimage Ubuntu 18.04 / Added possibility to use external authentication using REMOTE_USER
* **2019/01/13:** Webtrees 1.7.12
* **2019/05/08:** Webtrees 1.7.14
* **2019/12/10:** Webtrees 1.7.16
* **2019/12/10:** Webtrees 2.0.0
* **2020/01/06:** Webtrees 2.0.1
* **2020/01/13:** Webtrees 2.0.1 - Added possibility to use image without https (only http) for local networks
* **2020/01/16:** Webtrees 2.0.1 - Added multi arch support
* **2020/02/01:** Webtrees 2.0.2
* **2020/03/06docker run --rm -it -p 443:443/tcp -e DB_HOST=192.168.1.147 -e DB_PASSWORD=wt-test -e DB_USER=wt-test -e WT_ADMINPW=admin1234 webtrees:test-2:** Webtrees 2.0.2 - Added support to automatically initialize db and webtrees. And use K8S template for deployment
7 changes: 7 additions & 0 deletions config.ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; <?php exit; ?> DO NOT DELETE THIS LINE
dbhost="<DB_HOST>"
dbport="<DB_PORT>"
dbuser="<DB_USER>"
dbpass="<DB_PASSWORD>"
dbname="<DB_NAME>"
tblpfx="<DB_PREFIX>"
7 changes: 7 additions & 0 deletions kubernetes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Kubernetes template

This example template for kubernetes automatically creates a MySQL 5.7 database and a webtrees instance that uses this database. There for you have to set the database and webtrees admin credentials in the file `kustomization.yaml`.
Webtrees will be provided without SSL on port 80.
The template uses a persitent volume claim. Therefore your K8S instance has to provide a perstitant_storage.

You can apply the (modified) template using the command `kubectl apply -f kustomization.yaml`.
11 changes: 11 additions & 0 deletions kubernetes/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
secretGenerator:
- name: mysql-pass
literals:
- password=YOUR_PASSWORD
- name: webtrees-admin-pass
literals:
- password=YOUR_WEBTREES_ADMIN_PASSWORD
---
resources:
- mysql-deployment.yaml
- wwebtrees-deployment.yaml
65 changes: 65 additions & 0 deletions kubernetes/mysql-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: v1
kind: Service
metadata:
name: webtrees-mysql
labels:
app: webtrees
spec:
ports:
- port: 3306
selector:
app: webtrees
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: webtrees
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: webtrees-mysql
labels:
app: webtrees
spec:
selector:
matchLabels:
app: webtrees
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: webtrees
tier: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
80 changes: 80 additions & 0 deletions kubernetes/webtrees-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
apiVersion: v1
kind: Service
metadata:
name: webtrees
labels:
app: webtrees
spec:
ports:
- port: 80
selector:
app: webtrees
tier: frontend
type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wt-pv-claim
labels:
app: webtrees
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: webtrees
labels:
app: webtrees
spec:
selector:
matchLabels:
app: webtrees
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: webtrees
tier: frontend
spec:
containers:
- image: dtjs48jkt/webtrees
name: webtrees
env:
- name: DISABLE_SSL
value: true
- name: PORT
value: 80
- name: DB_HOST
value: webtrees-mysql
- name: WT_ADMINPW
valueFrom:
secretKeyRef:
name: webtrees-admin-pass
key: password
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: webtrees
volumeMounts:
- name: webtrees-persistent-storage
mountPath: /var/www/html/data
subPath: data
- name: webtrees-persistent-storage
mountPath: /var/www/html/media
subPath: media
volumes:
- name: webtrees-persistent-storage
persistentVolumeClaim:
claimName: wt-pv-claim
12 changes: 12 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ stderr_logfile_maxbytes=0
autostart=true
autorestart=true

[program:initialize_db_app]
command=/bin/06_initialize_db.sh
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
priority=7
exitcodes=0
startsecs=0
startretries=0
autorestart=false

[program:switch_http_https]
command=/bin/05_switch_http_https.sh
stdout_logfile=/dev/stdout
Expand Down
Loading