Skip to content

Commit

Permalink
Initial image build
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Jul 27, 2015
0 parents commit b6c2f33
Show file tree
Hide file tree
Showing 13 changed files with 852 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apache/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM php:5.6-apache
MAINTAINER Michael Babker <[email protected]> (@mbabker)

# Enable Apache Rewrite Module
RUN a2enmod rewrite

# Install PHP extensions
RUN apt-get update && apt-get install -y libicu-dev libmcrypt-dev zip unzip
RUN rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install intl mbstring mcrypt mysqli pdo pdo_mysql

VOLUME /var/www/html

# Define Mautic version and expected SHA1 signature
ENV MAUTIC_VERSION 1.1.3
ENV MAUTIC_UPSTREAM_VERSION 1.1.3
ENV MAUTIC_SHA1 77e4fe2438b3efac8a26f4f8c6c216a89fac1337

# Download package and extract to web volume
RUN curl -o mautic.zip -SL https://s3.amazonaws.com/mautic/releases/${MAUTIC_VERSION}.zip \
&& echo "$MAUTIC_SHA1 *mautic.zip" | sha1sum -c - \
&& mkdir /usr/src/mautic \
&& unzip mautic.zip -d /usr/src/mautic \
&& rm mautic.zip \
&& chown -R www-data:www-data /usr/src/mautic

# Copy init scripts and custom .htaccess
COPY docker-entrypoint.sh /entrypoint.sh
COPY makeconfig.php /makeconfig.php
COPY makedb.php /makedb.php

ENTRYPOINT ["/entrypoint.sh"]
CMD ["apache2-foreground"]
72 changes: 72 additions & 0 deletions apache/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

set -e

if [ -n "$MYSQL_PORT_3306_TCP" ]; then
if [ -z "$MAUTIC_DB_HOST" ]; then
MAUTIC_DB_HOST='mysql'
else
echo >&2 "warning: both MAUTIC_DB_HOST and MYSQL_PORT_3306_TCP found"
echo >&2 " Connecting to MAUTIC_DB_HOST ($MAUTIC_DB_HOST)"
echo >&2 " instead of the linked mysql container"
fi
fi

if [ -z "$MAUTIC_DB_HOST" ]; then
echo >&2 "error: missing MAUTIC_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 MAUTIC_DB_HOST=hostname:port?"
exit 1
fi

# If the DB user is 'root' then use the MySQL root password env var
: ${MAUTIC_DB_USER:=root}
if [ "$MAUTIC_DB_USER" = 'root' ]; then
: ${MAUTIC_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
fi
: ${MAUTIC_DB_NAME:=mautic}

if [ -z "$MAUTIC_DB_PASSWORD" ]; then
echo >&2 "error: missing required MAUTIC_DB_PASSWORD environment variable"
echo >&2 " Did you forget to -e MAUTIC_DB_PASSWORD=... ?"
echo >&2
echo >&2 " (Also of interest might be MAUTIC_DB_USER and MAUTIC_DB_NAME.)"
exit 1
fi

if ! [ -e index.php -a -e app/AppKernel.php ]; then
echo >&2 "Mautic 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/mautic . | tar xf -

echo >&2 "Complete! Mautic has been successfully copied to $(pwd)"
fi

# Ensure the MySQL Database is created
php /makedb.php "$MAUTIC_DB_HOST" "$MAUTIC_DB_USER" "$MAUTIC_DB_PASSWORD" "$MAUTIC_DB_NAME"

echo >&2 "========================================================================"
echo >&2
echo >&2 "This server is now configured to run Mautic!"
echo >&2 "You will need the following database information to install Mautic:"
echo >&2 "Host Name: $MAUTIC_DB_HOST"
echo >&2 "Database Name: $MAUTIC_DB_NAME"
echo >&2 "Database Username: $MAUTIC_DB_USER"
echo >&2 "Database Password: $MAUTIC_DB_PASSWORD"
echo >&2
echo >&2 "========================================================================"

# Write the database connection to the config so the installer prefills it
php /makeconfig.php "$MAUTIC_DB_HOST" "$MAUTIC_DB_USER" "$MAUTIC_DB_PASSWORD" "$MAUTIC_DB_NAME"

# Make sure our web user owns the config file if it exists
if [ -e app/config/local.php ]; then
chown www-data:www-data app/config/local.php
fi

exec "$@"
123 changes: 123 additions & 0 deletions apache/makeconfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
// Args: 0 => makedb.php, 1 => "$MAUTIC_DB_HOST", 2 => "$MAUTIC_DB_USER", 3 => "$MAUTIC_DB_PASSWORD", 4 => "$MAUTIC_DB_NAME"
$stderr = fopen('php://stderr', 'w');
fwrite($stderr, "\nWriting initial Mautic config\n");

// Figure out if we have a port in the database host string
if (strpos($argv[1], ':') !== false)
{
list($host, $port) = explode(':', $argv[1], 2);
}
else
{
$host = $argv[1];
$port = 3306;
}

$parameters = array(
'db_driver' => 'pdo_mysql',
'db_host' => $host,
'db_port' => $port,
'db_name' => $argv[4],
'db_user' => $argv[2],
'db_password' => $argv[3],
'install_source' => 'Docker'
);

$path = '/var/www/html/app/config/local.php';
$rendered = render($parameters);

$status = file_put_contents($path, $rendered);

if ($status === false) {
fwrite($stderr, "\nCould not write configuration file to $path, you can create this file with the following contents:\n\n$rendered\n");
}

/**
* Renders parameters as a string.
*
* @param array $parameters
*
* @return string
*/
function render(array $parameters)
{
$string = "<?php\n";
$string .= "\$parameters = array(\n";

foreach ($parameters as $key => $value)
{
if ($value !== '')
{
if (is_string($value))
{
$value = "'" . addslashes($value) . "'";
}
elseif (is_bool($value))
{
$value = ($value) ? 'true' : 'false';
}
elseif (is_null($value))
{
$value = 'null';
}
elseif (is_array($value))
{
$value = renderArray($value);
}

$string .= "\t'$key' => $value,\n";
}
}

$string .= ");\n";

return $string;
}

/**
* Renders an array of parameters as a string.
*
* @param array $array
* @param bool $addClosingComma
*
* @return string
*/
function renderArray(array $array, $addClosingComma = false)
{
$string = "array(";
$first = true;

foreach ($array as $key => $value)
{
if (!$first)
{
$string .= ',';
}

if (is_string($key))
{
$string .= '"' . $key . '" => ';
}

if (is_array($value))
{
$string .= $this->renderArray($value, true);
}
else
{
$string .= '"' . addslashes($value) . '"';
}

$first = false;
}

$string .= ")";

if ($addClosingComma)
{
$string .= ',';
}

return $string;
}
46 changes: 46 additions & 0 deletions apache/makedb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// Args: 0 => makedb.php, 1 => "$MAUTIC_DB_HOST", 2 => "$MAUTIC_DB_USER", 3 => "$MAUTIC_DB_PASSWORD", 4 => "$MAUTIC_DB_NAME"
$stderr = fopen('php://stderr', 'w');
fwrite($stderr, "\nEnsuring Mautic database is present\n");

if (strpos($argv[1], ':') !== false)
{
list($host, $port) = explode(':', $argv[1], 2);
}
else
{
$host = $argv[1];
$port = 3306;
}

$maxTries = 10;

do
{
$mysql = new mysqli($host, $argv[2], $argv[3], '', (int) $port);

if ($mysql->connect_error)
{
fwrite($stderr, "\nMySQL Connection Error: ({$mysql->connect_errno}) {$mysql->connect_error}\n");
--$maxTries;

if ($maxTries <= 0)
{
exit(1);
}

sleep(3);
}
}
while ($mysql->connect_error);

if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`'))
{
fwrite($stderr, "\nMySQL 'CREATE DATABASE' Error: " . $mysql->error . "\n");
$mysql->close();
exit(1);
}

fwrite($stderr, "\nMySQL Database Created\n");

$mysql->close();
72 changes: 72 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

set -e

if [ -n "$MYSQL_PORT_3306_TCP" ]; then
if [ -z "$MAUTIC_DB_HOST" ]; then
MAUTIC_DB_HOST='mysql'
else
echo >&2 "warning: both MAUTIC_DB_HOST and MYSQL_PORT_3306_TCP found"
echo >&2 " Connecting to MAUTIC_DB_HOST ($MAUTIC_DB_HOST)"
echo >&2 " instead of the linked mysql container"
fi
fi

if [ -z "$MAUTIC_DB_HOST" ]; then
echo >&2 "error: missing MAUTIC_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 MAUTIC_DB_HOST=hostname:port?"
exit 1
fi

# If the DB user is 'root' then use the MySQL root password env var
: ${MAUTIC_DB_USER:=root}
if [ "$MAUTIC_DB_USER" = 'root' ]; then
: ${MAUTIC_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
fi
: ${MAUTIC_DB_NAME:=mautic}

if [ -z "$MAUTIC_DB_PASSWORD" ]; then
echo >&2 "error: missing required MAUTIC_DB_PASSWORD environment variable"
echo >&2 " Did you forget to -e MAUTIC_DB_PASSWORD=... ?"
echo >&2
echo >&2 " (Also of interest might be MAUTIC_DB_USER and MAUTIC_DB_NAME.)"
exit 1
fi

if ! [ -e index.php -a -e app/AppKernel.php ]; then
echo >&2 "Mautic 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/mautic . | tar xf -

echo >&2 "Complete! Mautic has been successfully copied to $(pwd)"
fi

# Ensure the MySQL Database is created
php /makedb.php "$MAUTIC_DB_HOST" "$MAUTIC_DB_USER" "$MAUTIC_DB_PASSWORD" "$MAUTIC_DB_NAME"

echo >&2 "========================================================================"
echo >&2
echo >&2 "This server is now configured to run Mautic!"
echo >&2 "You will need the following database information to install Mautic:"
echo >&2 "Host Name: $MAUTIC_DB_HOST"
echo >&2 "Database Name: $MAUTIC_DB_NAME"
echo >&2 "Database Username: $MAUTIC_DB_USER"
echo >&2 "Database Password: $MAUTIC_DB_PASSWORD"
echo >&2
echo >&2 "========================================================================"

# Write the database connection to the config so the installer prefills it
php /makeconfig.php "$MAUTIC_DB_HOST" "$MAUTIC_DB_USER" "$MAUTIC_DB_PASSWORD" "$MAUTIC_DB_NAME"

# Make sure our web user owns the config file if it exists
if [ -e app/config/local.php ]; then
chown www-data:www-data app/config/local.php
fi

exec "$@"
30 changes: 30 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM php:5.6-fpm
MAINTAINER Michael Babker <[email protected]> (@mbabker)

# Install PHP extensions
RUN apt-get update && apt-get install -y libicu-dev libmcrypt-dev zip unzip
RUN rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install intl mbstring mcrypt mysqli pdo pdo_mysql

VOLUME /var/www/html

# Define Mautic version and expected SHA1 signature
ENV MAUTIC_VERSION 1.1.3
ENV MAUTIC_UPSTREAM_VERSION 1.1.3
ENV MAUTIC_SHA1 77e4fe2438b3efac8a26f4f8c6c216a89fac1337

# Download package and extract to web volume
RUN curl -o mautic.zip -SL https://s3.amazonaws.com/mautic/releases/${MAUTIC_VERSION}.zip \
&& echo "$MAUTIC_SHA1 *mautic.zip" | sha1sum -c - \
&& mkdir /usr/src/mautic \
&& unzip mautic.zip -d /usr/src/mautic \
&& rm mautic.zip \
&& chown -R www-data:www-data /usr/src/mautic

# Copy init scripts and custom .htaccess
COPY docker-entrypoint.sh /entrypoint.sh
COPY makeconfig.php /makeconfig.php
COPY makedb.php /makedb.php

ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]
Loading

0 comments on commit b6c2f33

Please sign in to comment.