From 3707e26a7351318fe531059572899cc82d6bf6f9 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Fri, 8 Jan 2016 16:56:12 +0100
Subject: [PATCH] Update all VM docker images to use supervisord and give the
 (default) possibility of using a local mysql daemon instead of a linked
 container

---
 j2vm2_full/Dockerfile           |  28 +++-
 j2vm2_full/docker-entrypoint.sh | 197 ++++++++++++++++------------
 j2vm2_full/supervisord.conf     |  10 ++
 j2vm3_full/Dockerfile           |  20 +--
 j2vm3_full/docker-entrypoint.sh |  13 +-
 j3vm3/Dockerfile                |  27 +++-
 j3vm3/docker-entrypoint.sh      | 220 +++++++++++++++++++-------------
 j3vm3/supervisord.conf          |  10 ++
 8 files changed, 328 insertions(+), 197 deletions(-)
 create mode 100644 j2vm2_full/supervisord.conf
 create mode 100644 j3vm3/supervisord.conf

diff --git a/j2vm2_full/Dockerfile b/j2vm2_full/Dockerfile
index c5a9117..6f6ba9d 100644
--- a/j2vm2_full/Dockerfile
+++ b/j2vm2_full/Dockerfile
@@ -4,9 +4,10 @@ MAINTAINER Reinhold Kainhofer <reinhold@kainhofer.com>
 # Enable Apache Rewrite Module
 RUN a2enmod rewrite
 
+RUN apt-get update
 
 # Install PHP extensions
-RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev zip unzip && rm -rf /var/lib/apt/lists/* \
+RUN apt-get install -y libpng12-dev libjpeg-dev zip unzip sudo \
 	&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
 	&& docker-php-ext-install gd
 RUN docker-php-ext-install mysqli
@@ -14,24 +15,37 @@ RUN docker-php-ext-install mysqli
 VOLUME /var/www/html
 
 # Install MySQL (will only be started if needed)
-# RUN apt-get update && apt-get install -y mysql-server && rm -rf /var/lib/apt/lists/*
+# RUN export DEBIAN_FRONTEND=noninteractive
+RUN echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
+RUN echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
+RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
+
+RUN apt-get install -y supervisor
 
 
 # Define Joomla version and expected SHA1 signature
-ENV VMFULL_VERSION 2.6.14
-ENV VMFULL_MD5 0a589d9db3831ae4e3d8eb95db28aeee
+ENV VM_VERSION 2.6.14
+ENV VM_DLDIR 880
+ENV VM_MD5 0a589d9db3831ae4e3d8eb95db28aeee
+ENV VM_POSTFIX ""
 
 # Download package and extract to web volume
-RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/880/VirtueMart${VMFULL_VERSION}_Joomla_2.5.27-Stable-Full_Package.zip \
-	&& echo "$VMFULL_MD5 *virtuemart.zip" | md5sum -c - \
+RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/${VM_DLDIR}/VirtueMart${VM_VERSION}_Joomla_2.5.27-Stable-Full_Package${VM_POSTFIX}.zip \
+	&& echo "$VM_MD5 *virtuemart.zip" | md5sum -c - \
 	&& mkdir /usr/src/virtuemart \
 	&& unzip virtuemart.zip -d /usr/src/virtuemart \
 	&& rm virtuemart.zip \
 	&& chown -R www-data:www-data /usr/src/virtuemart
 
+
+# Clean up the apt cache etc.
+RUN rm -rf /var/lib/apt/lists/* 
+
 # Copy init scripts and custom .htaccess
+RUN echo "[program:mysql]\ncommand=/usr/bin/pidproxy /run/mysqld/mysqld.pid /usr/bin/mysqld_safe \nautorestart=true\n" > /etc/supervisor/conf.d/mysql.conf.save
+COPY supervisord.conf /etc/supervisor/supervisord.conf
 COPY docker-entrypoint.sh /entrypoint.sh
 COPY makedb.php /makedb.php
 
 ENTRYPOINT ["/entrypoint.sh"]
-CMD ["apache2-foreground"]
+CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
diff --git a/j2vm2_full/docker-entrypoint.sh b/j2vm2_full/docker-entrypoint.sh
index 52cb7b4..d47945f 100755
--- a/j2vm2_full/docker-entrypoint.sh
+++ b/j2vm2_full/docker-entrypoint.sh
@@ -2,102 +2,133 @@
 
 set -e
 
-if [[ "$1" == apache2* ]]; then
-        if [ -n "$MYSQL_PORT_3306_TCP" ]; then
-                if [ -z "$JOOMLA_DB_HOST" ]; then
-                        JOOMLA_DB_HOST='mysql'
-                else
-                        echo >&2 "warning: both JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP found"
-                        echo >&2 "  Connecting to JOOMLA_DB_HOST ($JOOMLA_DB_HOST)"
-                        echo >&2 "  instead of the linked mysql container"
-                fi
-        fi
+if [[ ! -e "/etc/opentools-docker-configured" ]]; then
+	# This docker container has not been configured yet, use the env variables 
+	# to set up the MYSQL server or linked container
 
-        # If the DB user is 'root' then use the MySQL root password env var
-        : ${JOOMLA_DB_USER:=root}
-        if [ "$JOOMLA_DB_USER" = 'root' ]; then
-                : ${JOOMLA_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
-        fi
-        if [ -z "$JOOMLA_DB_HOST" ]; then
-			echo >&2 "Neither linked database container nor mysql dabase server host given. "
-			echo >&2 "   Assuming local installation. An instance of the MySQL server will be installed locally."
-			MYSQL_LOCAL=1
-			JOOMLA_DB_HOST="127.0.0.1"
-			if [ -z "${JOOMLA_DB_PASSWORD}" ]; then
-				JOOMLA_DB_PASSWORD='root'
-				echo >&2 "No MySQL root password given. Assuming password 'root'."
+	# check if a MYSQL container is linked:
+	if [ -n "$MYSQL_PORT_3306_TCP" ]; then
+		if [ -z "$JOOMLA_DB_HOST" ]; then
+			JOOMLA_DB_HOST='mysql'
+		else
+			echo >&2 "warning: both JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP found"
+			echo >&2 "  Connecting to JOOMLA_DB_HOST ($JOOMLA_DB_HOST)"
+			echo >&2 "  instead of the linked mysql container"
+		fi
+	fi
+	
+	# If the DB user is 'root' and no DB password is given, then use the MySQL root password env var
+	: ${JOOMLA_DB_USER:=root}
+	if [ "$JOOMLA_DB_USER" = 'root' ]; then
+			: ${JOOMLA_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
+	fi
+	
+	# Check for local MySQL installation:
+	if [ -z "$JOOMLA_DB_HOST" ]; then
+		# No linked container and no explicit DB host => local MySQL installation
+		echo >&2 "Neither linked database container nor mysql dabase server host given. "
+		echo >&2 "   Assuming local installation. An instance of the MySQL server will be installed locally."
+		MYSQL_LOCAL=1
+		JOOMLA_DB_HOST="127.0.0.1"
+		if [ -z "${JOOMLA_DB_PASSWORD}" ]; then
+			JOOMLA_DB_PASSWORD='root'
+			echo >&2 "No MySQL root password given. Assuming password 'root'."
+		fi
+		echo >&2 "   Root password is ${JOOMLA_DB_PASSWORD}."
+		
+		# Temporarily start the mysql daemon to set up the database and shut it 
+		# down again (supervisord will start it at the very end)
+		echo "Starting temporary local MySQL server to set up the database..."
+		/usr/bin/mysqld_safe > /dev/null 2>&1 &
+		timeout=30
+		echo -n "Waiting for database server to accept connections"
+		while ! /usr/bin/mysqladmin --user=root --password=root status > /dev/null 2>&1; do
+			timeout=$(($timeout-1))
+			if [ $timeout -eq 0 ]; then
+				echo -e "\n Unable to connecto the database server. Aborting..."
+				exit 1
 			fi
-			echo >&2 "   Root password is ${JOOMLA_DB_PASSWORD}."
-				
-			export DEBIAN_FRONTEND=noninteractive
+			echo -n "."
+			sleep 1
+		done
+		echo
+		mysqladmin --password=root password "${JOOMLA_DB_PASSWORD}"
+		
+		# enable mysqld in the supervisor config
+		cp /etc/supervisor/conf.d/mysql.conf.save /etc/supervisor/conf.d/mysql.conf
+	fi
 
-			echo "mysql-server-5.5 mysql-server/root_password password ${JOOMLA_DB_PASSWORD}" | debconf-set-selections
-			echo "mysql-server-5.5 mysql-server/root_password_again password ${JOOMLA_DB_PASSWORD}" | debconf-set-selections
-# 			echo 'phpmyadmin phpmyadmin/dbconfig-install boolean true' | debconf-set-selections
-# 			echo 'phpmyadmin phpmyadmin/app-password-confirm password ${JOOMLA_DB_PASSWORD}' | debconf-set-selections
-# 			echo 'phpmyadmin phpmyadmin/mysql/admin-pass password ${JOOMLA_DB_PASSWORD}' | debconf-set-selections
-# 			echo 'phpmyadmin phpmyadmin/mysql/app-pass password ${JOOMLA_DB_PASSWORD}' | debconf-set-selections
-# 			echo 'phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2' | debconf-set-selections
+	
+	# Now set up the Database for Joomla/VirtueMart:
+	: ${JOOMLA_DB_NAME:=virtuemart}
 
-			dpkg -s mysql-server > /dev/null 2>&1 || ( apt-get -q update && apt-get -y -q install mysql-server && rm -rf /var/lib/apt/lists/* )
-#                echo >&2 "error: missing JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP environment variables"
-#                echo >&2 "  Did you forget to --link some_mysql_container:mysql or set an external db"
-#                echo >&2 "  with -e JOOMLA_DB_HOST=hostname:port?"
-#                exit 1
-        fi
-        if [ -n "$MYSQL_LOCAL" ]; then
-			echo >&2 "Starting MySQL daemon..."
-			service mysql restart
-		fi
+	if [ -z "$JOOMLA_DB_PASSWORD" ]; then
+		echo >&2 "error: missing required JOOMLA_DB_PASSWORD environment variable"
+		echo >&2 "  Did you forget to -e JOOMLA_DB_PASSWORD=... or link to a container?"
+		echo >&2
+		echo >&2 "  (Also of interest might be JOOMLA_DB_USER and JOOMLA_DB_NAME.)"
+		exit 1
+	fi
+	# Ensure the MySQL Database is created
+	php /makedb.php "$JOOMLA_DB_HOST" "$JOOMLA_DB_USER" "$JOOMLA_DB_PASSWORD" "$JOOMLA_DB_NAME"
 
-        : ${JOOMLA_DB_NAME:=j2vm2
 
-        if [ -z "$JOOMLA_DB_PASSWORD" ]; then
-                echo >&2 "error: missing required JOOMLA_DB_PASSWORD environment variable"
-                echo >&2 "  Did you forget to -e JOOMLA_DB_PASSWORD=... ?"
-                echo >&2
-                echo >&2 "  (Also of interest might be JOOMLA_DB_USER and JOOMLA_DB_NAME.)"
-                exit 1
-        fi
+	# Now set up the Joomla/VirtueMart installation files in apache's directory:
+	if ! [ -e index.php -a -e libraries/cms/version/version.php ]; then
+		echo >&2 "Virtuemart/Joomla not found in $(pwd) - copying now..."
 
-        if ! [ -e index.php -a -e libraries/cms/version/version.php ]; then
-                echo >&2 "Virtuemart/Joomla not found in $(pwd) - copying now..."
+		if [ "$(ls -A)" ]; then
+			echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
+			( set -x; ls -A; sleep 10 )
+		fi
 
-                if [ "$(ls -A)" ]; then
-                        echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
-                        ( set -x; ls -A; sleep 10 )
-                fi
+		tar cf - --one-file-system -C /usr/src/virtuemart . | tar xf -
+		
+		# Some versions of the full installer hae an additional subdir in the ZIP file.
+		# Search for joomla.xml and mv everything from that directory to the webroot
+		if [ ! -e joomla.xml ]; then
+			for jmanifest in */joomla.xml; do 
+				jdir=$(dirname $jmanifest)
+				echo
+				mv $jdir/** .
+				rm -rf $jdir
+			done
+		fi
 
-                tar cf - --one-file-system -C /usr/src/virtuemart . | tar xf -
+		if [ -e htaccess.txt -a ! -e .htaccess ]; then
+			# NOTE: The "Indexes" option is disabled in the php:apache base image so remove it as we enable .htaccess
+			sed -r 's/^(Options -Indexes.*)$/#\1/' htaccess.txt > .htaccess
+			chown www-data:www-data .htaccess
+		fi
 
-                if [ ! -e .htaccess ]; then
-                        # NOTE: The "Indexes" option is disabled in the php:apache base image so remove it as we enable .htaccess
-                        sed -r 's/^(Options -Indexes.*)$/#\1/' htaccess.txt > .htaccess
-                        chown www-data:www-data .htaccess
-                fi
-                
-                sed 's/default="localhost"/default="'$JOOMLA_DB_HOST'"/;
-                     s/\(db_user.*\)$/\1 default="'$JOOMLA_DB_USER'"/;
-                     s/\(db_pass.*\)$/\1 default="'$JOOMLA_DB_PASSWORD'"/;
-                     s/\(db_name.*\)$/\1 default="'$JOOMLA_DB_NAME'"/' installation/models/forms/database.xml > installation/models/forms/database.xml.new
-				mv installation/models/forms/database.xml.new installation/models/forms/database.xml
+		sed 's/default="localhost"/default="'$JOOMLA_DB_HOST'"/;
+			 s/\(db_user.*\)$/\1 default="'$JOOMLA_DB_USER'"/;
+			 s/\(db_pass.*\)$/\1 default="'$JOOMLA_DB_PASSWORD'"/;
+			 s/\(db_name.*\)$/\1 default="'$JOOMLA_DB_NAME'"/' installation/models/forms/database.xml > installation/models/forms/database.xml.new
+		mv installation/models/forms/database.xml.new installation/models/forms/database.xml
 
-                echo >&2 "Complete! Virtuemart has been successfully copied to $(pwd)"
-        fi
+		echo >&2 "Complete! Virtuemart has been successfully copied to $(pwd)"
+	fi
 
-        # Ensure the MySQL Database is created
-        php /makedb.php "$JOOMLA_DB_HOST" "$JOOMLA_DB_USER" "$JOOMLA_DB_PASSWORD" "$JOOMLA_DB_NAME"
+	if [ "$MYSQL_LOCAL" = "1" ]; then
+		# Local installation, so shut down MySQL again, will later be started through supervisord
+		echo "Shutting down temporary MySQL instance ..."
+		/usr/bin/mysqladmin --user=root --password="${JOOMLA_DB_PASSWORD}" shutdown
+	fi
 
-        echo >&2 "========================================================================"
-        echo >&2
-        echo >&2 "This server is now configured to run Joomla!"
-        echo >&2 "You will need the following database information to install Joomla:"
-        echo >&2 "Host Name: $JOOMLA_DB_HOST"
-        echo >&2 "Database Name: $JOOMLA_DB_NAME"
-        echo >&2 "Database Username: $JOOMLA_DB_USER"
-        echo >&2 "Database Password: $JOOMLA_DB_PASSWORD"
-        echo >&2
-        echo >&2 "========================================================================"
+	echo >&2 "========================================================================"
+	echo >&2
+	echo >&2 "This server is now configured to run Joomla!"
+	echo >&2 "You will need the following database information to install Joomla:"
+	echo >&2 "Host Name: $JOOMLA_DB_HOST"
+	echo >&2 "Database Name: $JOOMLA_DB_NAME"
+	echo >&2 "Database Username: $JOOMLA_DB_USER"
+	echo >&2 "Database Password: $JOOMLA_DB_PASSWORD"
+	echo >&2
+	echo >&2 "========================================================================"
+	
+	# create the file to indicate this container has been configured:
+	touch /etc/opentools-docker-configured
 fi
 
 exec "$@"
diff --git a/j2vm2_full/supervisord.conf b/j2vm2_full/supervisord.conf
new file mode 100644
index 0000000..d5d19a3
--- /dev/null
+++ b/j2vm2_full/supervisord.conf
@@ -0,0 +1,10 @@
+[supervisord]
+nodaemon=true
+
+[program:apache]
+command=/usr/local/bin/apache2-foreground
+autorestart=true
+
+[include]
+files = /etc/supervisor/conf.d/*.conf
+
diff --git a/j2vm3_full/Dockerfile b/j2vm3_full/Dockerfile
index d7d14bc..984fd8a 100644
--- a/j2vm3_full/Dockerfile
+++ b/j2vm3_full/Dockerfile
@@ -4,10 +4,10 @@ MAINTAINER Reinhold Kainhofer <reinhold@kainhofer.com>
 # Enable Apache Rewrite Module
 RUN a2enmod rewrite
 
-RUN apt-get update 
+RUN apt-get update
 
 # Install PHP extensions
-RUN apt-get install -y libpng12-dev libjpeg-dev zip unzip \
+RUN apt-get install -y libpng12-dev libjpeg-dev zip unzip sudo \
 	&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
 	&& docker-php-ext-install gd
 RUN docker-php-ext-install mysqli
@@ -24,28 +24,28 @@ RUN apt-get install -y supervisor
 
 
 # Define Joomla version and expected SHA1 signature
-ENV VMFULL_VERSION 3.0.12
-ENV VMFULL_MD5 68dfeda59610bd798c0f03a7fb9340df
-ENV VMFULL_POSTFIX "_with_security_patch"
+ENV VM_VERSION 3.0.12
+ENV VM_DLDIR 983
+ENV VM_MD5 68dfeda59610bd798c0f03a7fb9340df
+ENV VM_POSTFIX "_with_security_patch"
 
 # Download package and extract to web volume
-RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/983/VirtueMart${VMFULL_VERSION}_Joomla_2.5.29-Stable-Full_Package${VMFULL_POSTFIX}.zip \
-	&& echo "$VMFULL_MD5 *virtuemart.zip" | md5sum -c - \
+RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/${VM_DLDIR}/VirtueMart${VM_VERSION}_Joomla_2.5.29-Stable-Full_Package${VM_POSTFIX}.zip \
+	&& echo "$VM_MD5 *virtuemart.zip" | md5sum -c - \
 	&& mkdir /usr/src/virtuemart \
 	&& unzip virtuemart.zip -d /usr/src/virtuemart \
 	&& rm virtuemart.zip \
 	&& chown -R www-data:www-data /usr/src/virtuemart
-	
-RUN echo "[program:mysql]\ncommand=/usr/bin/pidproxy /run/mysqld/mysqld.pid /usr/bin/mysqld_safe \nautorestart=true\n" > /etc/supervisor/conf.d/mysql.conf.save
 
 
 # Clean up the apt cache etc.
 RUN rm -rf /var/lib/apt/lists/* 
 
 # Copy init scripts and custom .htaccess
+RUN echo "[program:mysql]\ncommand=/usr/bin/pidproxy /run/mysqld/mysqld.pid /usr/bin/mysqld_safe \nautorestart=true\n" > /etc/supervisor/conf.d/mysql.conf.save
+COPY supervisord.conf /etc/supervisor/supervisord.conf
 COPY docker-entrypoint.sh /entrypoint.sh
 COPY makedb.php /makedb.php
-ADD supervisord.conf /etc/supervisor/supervisord.conf
 
 ENTRYPOINT ["/entrypoint.sh"]
 CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
diff --git a/j2vm3_full/docker-entrypoint.sh b/j2vm3_full/docker-entrypoint.sh
index ba5ddae..d47945f 100755
--- a/j2vm3_full/docker-entrypoint.sh
+++ b/j2vm3_full/docker-entrypoint.sh
@@ -38,7 +38,7 @@ if [[ ! -e "/etc/opentools-docker-configured" ]]; then
 		
 		# Temporarily start the mysql daemon to set up the database and shut it 
 		# down again (supervisord will start it at the very end)
-		echo "Starting local mysql server temporarily to set up the database..."
+		echo "Starting temporary local MySQL server to set up the database..."
 		/usr/bin/mysqld_safe > /dev/null 2>&1 &
 		timeout=30
 		echo -n "Waiting for database server to accept connections"
@@ -71,11 +71,6 @@ if [[ ! -e "/etc/opentools-docker-configured" ]]; then
 	fi
 	# Ensure the MySQL Database is created
 	php /makedb.php "$JOOMLA_DB_HOST" "$JOOMLA_DB_USER" "$JOOMLA_DB_PASSWORD" "$JOOMLA_DB_NAME"
-	
-	if [ "$MYSQL_LOCAL" = "1" ]; then
-		# Local installation, so shut down MySQL again, will later be started through supervisord
-		/usr/bin/mysqladmin --user=root --password="${JOOMLA_DB_PASSWORD}" shutdown
-	fi
 
 
 	# Now set up the Joomla/VirtueMart installation files in apache's directory:
@@ -115,6 +110,12 @@ if [[ ! -e "/etc/opentools-docker-configured" ]]; then
 		echo >&2 "Complete! Virtuemart has been successfully copied to $(pwd)"
 	fi
 
+	if [ "$MYSQL_LOCAL" = "1" ]; then
+		# Local installation, so shut down MySQL again, will later be started through supervisord
+		echo "Shutting down temporary MySQL instance ..."
+		/usr/bin/mysqladmin --user=root --password="${JOOMLA_DB_PASSWORD}" shutdown
+	fi
+
 	echo >&2 "========================================================================"
 	echo >&2
 	echo >&2 "This server is now configured to run Joomla!"
diff --git a/j3vm3/Dockerfile b/j3vm3/Dockerfile
index 82bcd8d..beb51dd 100644
--- a/j3vm3/Dockerfile
+++ b/j3vm3/Dockerfile
@@ -4,20 +4,32 @@ MAINTAINER Reinhold Kainhofer <reinhold@kainhofer.com>
 # Enable Apache Rewrite Module
 RUN a2enmod rewrite
 
+RUN apt-get update
+
 # Install PHP extensions
-RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev zip unzip sudo && rm -rf /var/lib/apt/lists/* \
+RUN apt-get install -y libpng12-dev libjpeg-dev zip unzip sudo \
 	&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
 	&& docker-php-ext-install gd
 RUN docker-php-ext-install mysqli
 
 VOLUME /var/www/html
 
+# Install MySQL (will only be started if needed)
+# RUN export DEBIAN_FRONTEND=noninteractive
+RUN echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
+RUN echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
+RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
+
+RUN apt-get install -y supervisor
+
+
 # Define Joomla version and expected SHA1 signature
-ENV VM_VERSION 3.0.10
-ENV VM_MD5 a684928d8b1e2dde0c9ac8e047d65038
+ENV VM_VERSION 3.0.12
+ENV VM_DLDIR 979
+ENV VM_MD5 2b9855cd0de8e7073fb3c4c7d633c5a3
 
 # Download package and extract to web volume
-RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/972/com_virtuemart.${VM_VERSION}_extract_first.zip \
+RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/${VM_DLDIR}/com_virtuemart.${VM_VERSION}_extract_first.zip \
 	&& echo "$VM_MD5 *virtuemart.zip" | md5sum -c - \
 	&& mkdir /usr/src/virtuemart \
 	&& unzip virtuemart.zip -d /usr/src/virtuemart \
@@ -26,11 +38,16 @@ RUN curl -o virtuemart.zip -SL http://dev.virtuemart.net/attachments/download/97
 	&& unzip /usr/src/virtuemart/com_virtuemart*_ext_aio.zip -d /usr/src/virtuemart/com_virtumart_ext_aio/ \
 	&& rm /usr/src/virtuemart/com_virtuemart*_ext_aio.zip
 
+# Clean up the apt cache etc.
+RUN rm -rf /var/lib/apt/lists/* 
+
 # Copy init scripts and custom .htaccess
+RUN echo "[program:mysql]\ncommand=/usr/bin/pidproxy /run/mysqld/mysqld.pid /usr/bin/mysqld_safe \nautorestart=true\n" > /etc/supervisor/conf.d/mysql.conf.save
+COPY supervisord.conf /etc/supervisor/supervisord.conf
 COPY docker-entrypoint.sh /entrypoint.sh
 COPY makedb.php /makedb.php
 COPY install-joomla.php /usr/src/joomla/installation/install.php
 COPY install-joomla-extension.php /usr/src/joomla/cli/install-joomla-extension.php
 
 ENTRYPOINT ["/entrypoint.sh"]
-CMD ["apache2-foreground"]
+CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
diff --git a/j3vm3/docker-entrypoint.sh b/j3vm3/docker-entrypoint.sh
index 0dd61d0..15e7be2 100755
--- a/j3vm3/docker-entrypoint.sh
+++ b/j3vm3/docker-entrypoint.sh
@@ -2,92 +2,140 @@
 
 set -e
 
-if [[ "$1" == apache2* ]]; then
-        if [ -n "$MYSQL_PORT_3306_TCP" ]; then
-                if [ -z "$JOOMLA_DB_HOST" ]; then
-                        JOOMLA_DB_HOST='mysql'
-                else
-                        echo >&2 "warning: both JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP found"
-                        echo >&2 "  Connecting to JOOMLA_DB_HOST ($JOOMLA_DB_HOST)"
-                        echo >&2 "  instead of the linked mysql container"
-                fi
-        fi
-
-        if [ -z "$JOOMLA_DB_HOST" ]; then
-                echo >&2 "error: missing JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP environment variables"
-                echo >&2 "  Did you forget to --link some_mysql_container:mysql or set an external db"
-                echo >&2 "  with -e JOOMLA_DB_HOST=hostname:port?"
-                exit 1
-        fi
-
-        # If the DB user is 'root' then use the MySQL root password env var
-        : ${JOOMLA_DB_USER:=root}
-        if [ "$JOOMLA_DB_USER" = 'root' ]; then
-                : ${JOOMLA_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
-        fi
-        : ${JOOMLA_DB_NAME:=virtuemart}
-
-        if [ -z "$JOOMLA_DB_PASSWORD" ]; then
-                echo >&2 "error: missing required JOOMLA_DB_PASSWORD environment variable"
-                echo >&2 "  Did you forget to -e JOOMLA_DB_PASSWORD=... ?"
-                echo >&2
-                echo >&2 "  (Also of interest might be JOOMLA_DB_USER and JOOMLA_DB_NAME.)"
-                exit 1
-        fi
-
-        if ! [ -e index.php -a -e libraries/cms/version/version.php ]; then
-                echo >&2 "Joomla not found in $(pwd) - copying now..."
-
-                if [ "$(ls -A)" ]; then
-                        echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
-                        ( set -x; ls -A; sleep 10 )
-                fi
-
-                tar cf - --one-file-system -C /usr/src/joomla . | tar xf -
-
-                if [ ! -e .htaccess ]; then
-                        # NOTE: The "Indexes" option is disabled in the php:apache base image so remove it as we enable .htaccess
-                        sed -r 's/^(Options -Indexes.*)$/#\1/' htaccess.txt > .htaccess
-                        chown www-data:www-data .htaccess
-                fi
-
-                echo >&2 "Complete! Joomla has been successfully copied to $(pwd)"
-        fi
-
-        # Ensure the MySQL Database is created
-        php /makedb.php "$JOOMLA_DB_HOST" "$JOOMLA_DB_USER" "$JOOMLA_DB_PASSWORD" "$JOOMLA_DB_NAME"
-
-        echo >&2 "========================================================================"
-        echo >&2
-        echo >&2 "This server is now configured to run Joomla!"
-        echo >&2 "You will need the following database information to install Joomla:"
-        echo >&2 "Host Name: $JOOMLA_DB_HOST"
-        echo >&2 "Database Name: $JOOMLA_DB_NAME"
-        echo >&2 "Database Username: $JOOMLA_DB_USER"
-        echo >&2 "Database Password: $JOOMLA_DB_PASSWORD"
-        echo >&2
-        echo >&2 "========================================================================"
-        
-        
-        # Now run the joomla Installer:
-        
-        : ${JOOMLA_ADMIN_USER:=admin}
-        : ${JOOMLA_ADMIN_PASSWORD:=admin}
-        : ${JOOMLA_ADMIN_EMAIL:=admin@example.com}
-        : ${JOOMLA_SITE_NAME:=Joomla Installation}
-        if [ -z "$JOOMLA_DB_PREFIX" ]; then
-            DBPREFIX="--dbprefix=\"${JOOMLA_DB_PREFIX}_\""
-        fi
-        
-        php ./installation/install.php --name "$JOOMLA_SITE_NAME" \
-            --admin-user "$JOOMLA_ADMIN_USER" --admin-pass "$JOOMLA_ADMIN_PASSWORD" --admin-email "$JOOMLA_ADMIN_EMAIL" \
-            --db-host "$JOOMLA_DB_HOST" --db-user "$JOOMLA_DB_USER" --db-pass "$JOOMLA_DB_PASSWORD" --db-name "$JOOMLA_DB_NAME" $DBPREFIX  && \
-            rm -rf "./installation/"
-
-
-        for p in /usr/src/virtuemart/*.zip; do
-            sudo -u www-data php ./cli/install-joomla-extension.php --package=$p
-        done
+if [[ ! -e "/etc/opentools-docker-configured" ]]; then
+	# This docker container has not been configured yet, use the env variables 
+	# to set up the MYSQL server or linked container
+
+	# check if a MYSQL container is linked:
+	if [ -n "$MYSQL_PORT_3306_TCP" ]; then
+		if [ -z "$JOOMLA_DB_HOST" ]; then
+			JOOMLA_DB_HOST='mysql'
+		else
+			echo >&2 "warning: both JOOMLA_DB_HOST and MYSQL_PORT_3306_TCP found"
+			echo >&2 "  Connecting to JOOMLA_DB_HOST ($JOOMLA_DB_HOST)"
+			echo >&2 "  instead of the linked mysql container"
+		fi
+	fi
+	
+	# If the DB user is 'root' and no DB password is given, then use the MySQL root password env var
+	: ${JOOMLA_DB_USER:=root}
+	if [ "$JOOMLA_DB_USER" = 'root' ]; then
+			: ${JOOMLA_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
+	fi
+	
+	# Check for local MySQL installation:
+	if [ -z "$JOOMLA_DB_HOST" ]; then
+		# No linked container and no explicit DB host => local MySQL installation
+		echo >&2 "Neither linked database container nor mysql dabase server host given. "
+		echo >&2 "   Assuming local installation. An instance of the MySQL server will be installed locally."
+		MYSQL_LOCAL=1
+		JOOMLA_DB_HOST="127.0.0.1"
+		if [ -z "${JOOMLA_DB_PASSWORD}" ]; then
+			JOOMLA_DB_PASSWORD='root'
+			echo >&2 "No MySQL root password given. Assuming password 'root'."
+		fi
+		echo >&2 "   Root password is ${JOOMLA_DB_PASSWORD}."
+		
+		# Temporarily start the mysql daemon to set up the database and shut it 
+		# down again (supervisord will start it at the very end)
+		echo "Starting local mysql server temporarily to set up the database..."
+		/usr/bin/mysqld_safe > /dev/null 2>&1 &
+		timeout=30
+		echo -n "Waiting for database server to accept connections"
+		while ! /usr/bin/mysqladmin --user=root --password=root status > /dev/null 2>&1; do
+			timeout=$(($timeout-1))
+			if [ $timeout -eq 0 ]; then
+				echo -e "\n Unable to connecto the database server. Aborting..."
+				exit 1
+			fi
+			echo -n "."
+			sleep 1
+		done
+		echo
+		mysqladmin --password=root password "${JOOMLA_DB_PASSWORD}"
+		
+		# enable mysqld in the supervisor config
+		cp /etc/supervisor/conf.d/mysql.conf.save /etc/supervisor/conf.d/mysql.conf
+	fi
+
+	
+	# Now set up the Database for Joomla/VirtueMart:
+	: ${JOOMLA_DB_NAME:=virtuemart}
+
+	if [ -z "$JOOMLA_DB_PASSWORD" ]; then
+		echo >&2 "error: missing required JOOMLA_DB_PASSWORD environment variable"
+		echo >&2 "  Did you forget to -e JOOMLA_DB_PASSWORD=... or link to a container?"
+		echo >&2
+		echo >&2 "  (Also of interest might be JOOMLA_DB_USER and JOOMLA_DB_NAME.)"
+		exit 1
+	fi
+	# Ensure the MySQL Database is created
+	php /makedb.php "$JOOMLA_DB_HOST" "$JOOMLA_DB_USER" "$JOOMLA_DB_PASSWORD" "$JOOMLA_DB_NAME"
+
+
+	# Now set up the Joomla/VirtueMart installation files in apache's directory:
+	if ! [ -e index.php -a -e libraries/cms/version/version.php ]; then
+		echo >&2 "Joomla not found in $(pwd) - copying now..."
+
+		if [ "$(ls -A)" ]; then
+			echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
+			( set -x; ls -A; sleep 10 )
+		fi
+
+		# extract the joomla installer (VM will be installed from the package later
+		tar cf - --one-file-system -C /usr/src/joomla . | tar xf -
+
+		if [ -e htaccess.txt -a ! -e .htaccess ]; then
+			# NOTE: The "Indexes" option is disabled in the php:apache base image so remove it as we enable .htaccess
+			sed -r 's/^(Options -Indexes.*)$/#\1/' htaccess.txt > .htaccess
+			chown www-data:www-data .htaccess
+		fi
+
+		echo >&2 "Complete! Joomla has been successfully copied to $(pwd)"
+	fi
+
+	# Now run the joomla Installer:
+	: ${JOOMLA_ADMIN_USER:=admin}
+	: ${JOOMLA_ADMIN_PASSWORD:=admin}
+	: ${JOOMLA_ADMIN_EMAIL:=admin@example.com}
+	: ${JOOMLA_SITE_NAME:=Joomla Installation}
+	if [ -z "$JOOMLA_DB_PREFIX" ]; then
+		DBPREFIX="--dbprefix=\"${JOOMLA_DB_PREFIX}_\""
+	fi
+
+	php ./installation/install.php --name "$JOOMLA_SITE_NAME" \
+		--admin-user "$JOOMLA_ADMIN_USER" --admin-pass "$JOOMLA_ADMIN_PASSWORD" --admin-email "$JOOMLA_ADMIN_EMAIL" \
+		--db-host "$JOOMLA_DB_HOST" --db-user "$JOOMLA_DB_USER" --db-pass "$JOOMLA_DB_PASSWORD" --db-name "$JOOMLA_DB_NAME" $DBPREFIX  && \
+		rm -rf "./installation/"
+
+
+	for p in /usr/src/virtuemart/*.zip; do
+		sudo -u www-data php ./cli/install-joomla-extension.php --package=$p
+	done
+
+
+	if [ "$MYSQL_LOCAL" = "1" ]; then
+		# Local installation, so shut down MySQL again, will later be started through supervisord
+		echo "Shutting down temporary MySQL instance ..."
+		/usr/bin/mysqladmin --user=root --password="${JOOMLA_DB_PASSWORD}" shutdown
+	fi
+
+	echo >&2 "========================================================================"
+	echo >&2
+	echo >&2 "This server is now configured to run Joomla!"
+	echo >&2 "You will need the following database information to install Joomla:"
+	echo >&2 "Host Name: $JOOMLA_DB_HOST"
+	echo >&2 "Database Name: $JOOMLA_DB_NAME"
+	echo >&2 "Database Username: $JOOMLA_DB_USER"
+	echo >&2 "Database Password: $JOOMLA_DB_PASSWORD"
+	echo >&2 
+	echo >&2 "Joomla admin user: $JOOMLA_ADMIN_USER"
+	echo >&2 "Joomla admin password: $JOOMLA_ADMIN_PASSWORD"
+	echo >&2
+	echo >&2 "========================================================================"
+
+	# create the file to indicate this container has been configured:
+	touch /etc/opentools-docker-configured
 fi
 
 exec "$@"
diff --git a/j3vm3/supervisord.conf b/j3vm3/supervisord.conf
new file mode 100644
index 0000000..d5d19a3
--- /dev/null
+++ b/j3vm3/supervisord.conf
@@ -0,0 +1,10 @@
+[supervisord]
+nodaemon=true
+
+[program:apache]
+command=/usr/local/bin/apache2-foreground
+autorestart=true
+
+[include]
+files = /etc/supervisor/conf.d/*.conf
+
-- 
GitLab