From 857fe09da855addeba4ccf0b8f2792f8f063c220 Mon Sep 17 00:00:00 2001 From: Sven Fischer Date: Tue, 11 Oct 2016 15:39:08 +0200 Subject: [PATCH] Added autoconfiguration to nextcloud image --- nextcloud/10.0/Dockerfile | 1 + nextcloud/10.0/run.sh | 25 ++++++++--- nextcloud/10.0/setup.sh | 95 +++++++++++++++++++++++++++++++++++++++ nextcloud/README.md | 79 +++++++++++++++++++++----------- 4 files changed, 168 insertions(+), 32 deletions(-) create mode 100755 nextcloud/10.0/setup.sh diff --git a/nextcloud/10.0/Dockerfile b/nextcloud/10.0/Dockerfile index ee9cb9d..8b41ab1 100644 --- a/nextcloud/10.0/Dockerfile +++ b/nextcloud/10.0/Dockerfile @@ -93,6 +93,7 @@ COPY php-fpm.conf /etc/php7/php-fpm.conf COPY opcache.ini /etc/php7/conf.d/00_opcache.ini COPY apcu.ini /etc/php7/conf.d/apcu.ini COPY run.sh /usr/local/bin/run.sh +COPY setup.sh /usr/local/bin/setup.sh COPY occ /usr/local/bin/occ COPY s6.d /etc/s6.d diff --git a/nextcloud/10.0/run.sh b/nextcloud/10.0/run.sh index 3fdff29..16b4785 100644 --- a/nextcloud/10.0/run.sh +++ b/nextcloud/10.0/run.sh @@ -1,17 +1,30 @@ #!/bin/sh -if [ ! -f /config/config.php ]; then - echo -e " '/data',\n);" > /config/config.php -fi - sed -i -e "s//$UPLOAD_MAX_SIZE/g" /etc/nginx/nginx.conf /etc/php7/php-fpm.conf \ -e "s//$APC_SHM_SIZE/g" /etc/php7/conf.d/apcu.ini \ -e "s//$OPCACHE_MEM_SIZE/g" /etc/php7/conf.d/00_opcache.ini \ -e "s//$CRON_PERIOD/g" /etc/s6.d/cron/run +# Put the configuration and apps into volumes +ln -sf /config/config.php /nextcloud/config/config.php &>/dev/null +ln -sf /apps2 /nextcloud &>/dev/null + chown -R $UID:$GID /nextcloud /data /config /apps2 /etc/nginx /etc/php7 /var/log /var/lib/nginx /tmp /etc/s6.d -ln -s /config/config.php /nextcloud/config/config.php &>/dev/null -ln -s /apps2 /nextcloud &>/dev/null +if [ ! -f /config/config.php ]; then + # New installation, run the setup + /usr/local/bin/setup.sh +else + occ upgrade + if [ \( $? -ne 0 \) -a \( $? -ne 3 \) ]; then + echo "Trying ownCloud upgrade again to work around ownCloud upgrade bug..." + occ upgrade + if [ \( $? -ne 0 \) -a \( $? -ne 3 \) ]; then exit 1; fi + occ maintenance:mode --off + echo "...which seemed to work." + fi +fi + +chown -R $UID:$GID /nextcloud /data /config /apps2 /etc/nginx /etc/php7 /var/log /var/lib/nginx /tmp /etc/s6.d exec su-exec $UID:$GID /bin/s6-svscan /etc/s6.d diff --git a/nextcloud/10.0/setup.sh b/nextcloud/10.0/setup.sh new file mode 100755 index 0000000..9b07f86 --- /dev/null +++ b/nextcloud/10.0/setup.sh @@ -0,0 +1,95 @@ +#!/bin/sh +# Nextcloud +########################## + +#source setup/functions.sh # load our functions +#source /etc/mailinabox.conf # load global vars +CONFIGFILE=/config/config.php + + +# Create an initial configuration file. +instanceid=oc$(echo $PRIMARY_HOSTNAME | sha1sum | fold -w 10 | head -n 1) +cat > $CONFIGFILE < '/data', + + 'instanceid' => '$instanceid', +); +?> +EOF + +# Create an auto-configuration file to fill in database settings +# when the install script is run. Make an administrator account +# here or else the install can't finish. +adminpassword=$(dd if=/dev/urandom bs=1 count=40 2>/dev/null | sha1sum | fold -w 30 | head -n 1) +cat > /nextcloud/config/autoconfig.php < '/data', + 'dbtype' => '${DB_TYPE:-sqlite3}', + 'dbname' => '${DB_NAME:-nextcloud}', + 'dbuser' => '${DB_USER:-nextcloud}', + 'dbpass' => '${DB_PASSWORD:-password}', + 'dbhost' => '${DB_HOST:-nextcloud-db}', + 'dbtableprefix' => 'oc_', +EOF +if [[ ! -z "$ADMIN_USER" ]]; then + cat >> /nextcloud/config/autoconfig.php < '${ADMIN_USER}', + 'adminpass' => '${ADMIN_PASSWORD}', +EOF +fi +cat >> /nextcloud/config/autoconfig.php < +EOF + +echo "Starting automatic configuration..." +# Execute ownCloud's setup step, which creates the ownCloud database. +# It also wipes it if it exists. And it updates config.php with database +# settings and deletes the autoconfig.php file. +(cd /nextcloud; php7 index.php) +echo "Automatic configuration finished." + +# Update config.php. +# * trusted_domains is reset to localhost by autoconfig starting with ownCloud 8.1.1, +# so set it here. It also can change if the box's PRIMARY_HOSTNAME changes, so +# this will make sure it has the right value. +# * Some settings weren't included in previous versions of Mail-in-a-Box. +# * We need to set the timezone to the system timezone to allow fail2ban to ban +# users within the proper timeframe +# * We need to set the logdateformat to something that will work correctly with fail2ban +# Use PHP to read the settings file, modify it, and write out the new settings array. +echo ${TZ:-UTC} >/etc/timezone + +TIMEZONE=$(cat /etc/timezone) +CONFIG_TEMP=$(/bin/mktemp) +php7 < $CONFIG_TEMP && mv $CONFIG_TEMP $CONFIGFILE + +EOF + +chown -R $UID:$GID /config +# Enable/disable apps. Note that this must be done after the ownCloud setup. +# The firstrunwizard gave Josh all sorts of problems, so disabling that. +# user_external is what allows ownCloud to use IMAP for login. The contacts +# and calendar apps are the extensions we really care about here. +occ app:disable firstrunwizard diff --git a/nextcloud/README.md b/nextcloud/README.md index c1fa665..a4db5e1 100644 --- a/nextcloud/README.md +++ b/nextcloud/README.md @@ -41,6 +41,14 @@ Other tags than `daily` are built weekly. For security reasons, you should occas - **APC_SHM_SIZE** : apc memory size *(default : 128M)* - **OPCACHE_MEM_SIZE** : opcache memory size in megabytes *(default : 128)* - **CRON_PERIOD** : time interval between two cron tasks *(default : 15m)* +- **TZ** : The log timezone *(default : Europe/Berlin)* +- **ADMIN_USER** : Username of the administrator user *(default : admin)* +- **ADMIN_PASSWORD** : Password of the administrator user *(default : admin)* +- **DB_TYPE** : Database type (sqlite3, mysql or pgsql) *(default : sqlite3)* +- **DB_NAME** : Name of database *(default : none)* +- **DB_USER** : Username for database *(default : none)* +- **DB_PASSWORD** : Password for database user *(default : none)* +- **DB_HOST** : Database host *(default : none)* #### Port - **8888** @@ -59,7 +67,7 @@ Pull the image and create a container. `/mnt` can be anywhere on your host, this ```` docker pull wonderfall/nextcloud && docker pull mariadb:10 docker run -d --name db_nextcloud -v /mnt/nextcloud/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=supersecretpassword -e MYSQL_DATABASE=nextcloud -e MYSQL_USER=nextcloud -e MYSQL_PASSWORD=supersecretpassword mariadb:10 -docker run -d --name nextcloud --link db_nextcloud:db_nextcloud -e UID=1000 -e GID=1000 -v /mnt/nextcloud/data:/data -v /mnt/nextcloud/config:/config -v /mnt/nextcloud/apps:/apps2 wonderfall/nextcloud +docker run -d --name nextcloud --link db_nextcloud:db_nextcloud -e UID=1000 -e GID=1000 -e DB_NAME=nextcloud -e DB_USER=nextcloud -e DB_PASSWORD=supersecretpassword -e DB_HOST=db_nextcloud -v /mnt/nextcloud/data:/data -v /mnt/nextcloud/config:/config -v /mnt/nextcloud/apps:/apps2 wonderfall/nextcloud ``` **Below you can find a docker-compose file, which is very useful!** @@ -113,34 +121,53 @@ If Nextcloud performed a full upgrade, your apps could be disabled. Enable them #### Docker-compose -I advise you to use [docker-compose](https://docs.docker.com/compose/), which is a great tool for managing containers. You can create a `docker-compose.yml` with the following content (which must be adapted to your needs) and then run everything with `docker-compose up -d`, that's it! +I advise you to use [docker-compose](https://docs.docker.com/compose/), which is a great tool for managing containers. You can create a `docker-compose.yml` with the following content (which must be adapted to your needs) and then run `docker-compose up -d nextcloud-db`, wait some 15 seconds for the database to come up, then run everything with `docker-compose up -d`, that's it! On subsequent runs, a single `docker-compose up -d` is sufficient! ``` -nextcloud: - image: wonderfall/nextcloud:10.0 - links: - - db_nextcloud:db_nextcloud - environment: - - UID=1000 - - GID=1000 - - UPLOAD_MAX_SIZE=10G - - APC_SHM_SIZE=128M - - OPCACHE_MEM_SIZE=128 - - CRON_PERIOD=15m - volumes: - - /mnt/nextcloud/data:/data - - /mnt/nextcloud/config:/config - - /mnt/nextcloud/apps:/apps2 +version: '2' -db_nextcloud: - image: mariadb:10 - volumes: - - /mnt/nextcloud/db:/var/lib/mysql - environment: - - MYSQL_ROOT_PASSWORD=supersecretpassword - - MYSQL_DATABASE=nextcloud - - MYSQL_USER=nextcloud - - MYSQL_PASSWORD=supersecretpassword +volumes: + nextcloud-db-data: + nextcloud-data: + nextcloud-config: + nextcloud-apps: + +services: + nextcloud-db: + image: mariadb + volumes: + - nextcloud-db-data:/var/lib/mysql + environment: + - MYSQL_ROOT_PASSWORD=1234 + - MYSQL_DATABASE=nextcloud + - MYSQL_USER=nextcloud + - MYSQL_PASSWORD=foo5678 + + nextcloud: + image: wonderfall/nextcloud + environment: + - UID=1000 + - GID=1000 + - UPLOAD_MAX_SIZE=10G + - APC_SHM_SIZE=128M + - OPCACHE_MEM_SIZE=128 + - CRON_PERIOD=15m + - TZ=Europe/Berlin + - ADMIN_USER=admin + - ADMIN_PASSWORD=admin + - DB_TYPE=mysql + - DB_NAME=nextcloud + - DB_USER=nextcloud + - DB_PASSWORD=foo5678 + - DB_HOST=nextcloud-db + depends_on: + - nextcloud-db + volumes: + - nextcloud-data:/data + - nextcloud-config:/config + - nextcloud-apps:/apps2 + ports: + - 8888:8888 ``` You can update everything with `docker-compose pull` followed by `docker-compose up -d`.