From e018a8a41aaed33a1368beb18a86ba6f95b65f43 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 3 Oct 2014 01:23:20 +0200 Subject: [PATCH 1/5] Support mysql storage backend --- Dockerfile | 3 +-- README.md | 31 ++++++++++++++++++++++++ configure-db.php | 61 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index bee7a3f..bf93968 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,7 @@ MAINTAINER Christian Lück RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \ nginx git supervisor php5-fpm php5-cli php5-curl php5-gd php5-json \ - php5-pgsql -# php5-mysql + php5-pgsql php5-mysql && apt-get clean # add ttrss as the only nginx site ADD ttrss.nginx.conf /etc/nginx/sites-available/ttrss diff --git a/README.md b/README.md index fdbabda..b152ac6 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,37 @@ following additional arguments: -e DB_ENV_PASS=docker ``` +#### Running with external database server + +If you already have a PostgreSQL or MySQL server around off docker you also can go with +that. Instead of linking docker containers you need to provide database hostname, port, +database name and user credentials manually like so: + +``` +-e DB_HOST=172.17.42.1 +-e DB_PORT=3306 +-e DB_NAME=ttrss +-e DB_USER=ttrssuser +-e DB_PASS=ttrsspass +``` + +If your database is exposed on a non-standard port you also need to provide DB_TYPE set +to either "pgsql" or "mysql". + +#### Running with mysql database server + +If you'd like to use ttrss with a mysql database backend, simply use the additional +database configuration arguments to docker mentioned above. + +You also might want to link ttrss container to a mysql container. If the mysql server +is exposed on port 3306 it will be detected automatically, otherwise you need to specify +DB_TYPE env flag. + +```bash +$ sudo docker run -name mysql -d sameersbn/mysql:latest +$ sudo docker run -it --link mysql:db -p 80:80 clue/ttrss +``` + #### Running ttrss daemonized Once you've confirmed everything works in the foreground, you can start your container diff --git a/configure-db.php b/configure-db.php index 2cc0807..a291045 100644 --- a/configure-db.php +++ b/configure-db.php @@ -2,18 +2,49 @@ exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS'])); - $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER'])); + + if($super['DB_TYPE'] == 'mysql') { + $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME'])); + $pdo->exec('GRANT ALL PRIVILEGES ON ' . ($config['DB_NAME']) . '.* TO ' . $pdo->quote($config['DB_USER']) . '@"%" IDENTIFIED BY ' . $pdo->quote($config['DB_PASS'])); + } + else { + $pdo->exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS'])); + $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER'])); + } + unset($pdo); if (dbcheck($config)) { @@ -89,14 +128,14 @@ function error($text) function dbconnect($config) { - $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME', 'user' => 'USER', 'password' => 'PASS'); + $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME'); $dsn = $config['DB_TYPE'] . ':'; foreach ($map as $d => $h) { if (isset($config['DB_' . $h])) { $dsn .= $d . '=' . $config['DB_' . $h] . ';'; } } - $pdo = new \PDO($dsn); + $pdo = new \PDO($dsn, $config['DB_USER'], $config['DB_PASS']); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $pdo; } From eb88b7c2611753f7a858a0552cfd1bca72ed2d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 9 Nov 2014 00:23:17 +0100 Subject: [PATCH 2/5] PSR-2 code style --- configure-db.php | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/configure-db.php b/configure-db.php index a291045..a135b0e 100644 --- a/configure-db.php +++ b/configure-db.php @@ -6,42 +6,38 @@ $confpath = '/var/www/config.php'; $config = array(); -if(getenv($ename . '_TYPE') !== false) { +if (getenv($ename . '_TYPE') !== false) { $config['DB_TYPE'] = getenv($ename . '_TYPE'); -} -elseif(getenv($ename . '_PORT_5432_TCP_ADDR') !== false) { +} elseif (getenv($ename . '_PORT_5432_TCP_ADDR') !== false) { // postgres container linked $config['DB_TYPE'] = 'pgsql'; $eport = 5432; -} -elseif(getenv($ename . '_PORT_3306_TCP_ADDR') !== false) { +} elseif (getenv($ename . '_PORT_3306_TCP_ADDR') !== false) { // mysql container linked $config['DB_TYPE'] = 'mysql'; $eport = 3306; } -if(!empty($eport)) { +if (!empty($eport)) { $config['DB_HOST'] = env($ename . '_PORT_' . $eport . '_TCP_ADDR'); $config['DB_PORT'] = env($ename . '_PORT_' . $eport . '_TCP_PORT'); -} -elseif(getenv($ename . '_PORT') === false) { - error('The env ' . $ename .'_PORT does not exist. Make sure to run with "--link mypostgresinstance:' . $ename . '"'); -} -elseif(is_numeric(getenv($ename . '_PORT')) && getenv($ename . '_HOST') !== false) { +} elseif (getenv($ename . '_PORT') === false) { + error('The env ' . $ename . '_PORT does not exist. Make sure to run with "--link mypostgresinstance:' . $ename . '"'); +} elseif (is_numeric(getenv($ename . '_PORT')) && getenv($ename . '_HOST') !== false) { // numeric DB_PORT provided; assume port number passed directly $config['DB_HOST'] = env($ename . '_HOST'); $config['DB_PORT'] = env($ename . '_PORT'); - if(empty($config['DB_TYPE'])) { - switch($config['DB_PORT']) { - case 3306: - $config['DB_TYPE'] = 'mysql'; - break; - case 5432: - $config['DB_TYPE'] = 'pgsql'; - break; - default: - error('Database on non-standard port '.$config['DB_PORT'].' and env ' . $ename .'_TYPE not present'); + if (empty($config['DB_TYPE'])) { + switch ($config['DB_PORT']) { + case 3306: + $config['DB_TYPE'] = 'mysql'; + break; + case 5432: + $config['DB_TYPE'] = 'pgsql'; + break; + default: + error('Database on non-standard port ' . $config['DB_PORT'] . ' and env ' . $ename . '_TYPE not present'); } } } @@ -68,11 +64,10 @@ if (!dbcheck($config)) { $pdo = dbconnect($super); - if($super['DB_TYPE'] == 'mysql') { + if ($super['DB_TYPE'] === 'mysql') { $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME'])); $pdo->exec('GRANT ALL PRIVILEGES ON ' . ($config['DB_NAME']) . '.* TO ' . $pdo->quote($config['DB_USER']) . '@"%" IDENTIFIED BY ' . $pdo->quote($config['DB_PASS'])); - } - else { + } else { $pdo->exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS'])); $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER'])); } From fe6070a6649d9ae8215eaa914f2e443acdc3cd11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 9 Nov 2014 02:22:11 +0100 Subject: [PATCH 3/5] Simplify accessing ENV by always assuming DB prefix --- configure-db.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/configure-db.php b/configure-db.php index a135b0e..6025e30 100644 --- a/configure-db.php +++ b/configure-db.php @@ -1,32 +1,31 @@ #!/usr/bin/env php Date: Sun, 9 Nov 2014 12:56:47 +0100 Subject: [PATCH 4/5] Reorganize documentation of database setup --- README.md | 96 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index b152ac6..97de8c3 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,9 @@ Having trouble getting the above to run? This is the detailed installation walkthrough. If you've already followed the [quickstart](#quickstart) guide and everything works, you can skip this part. -### Running +### Select database + +This container requires a PostgreSQL or MySQL database instance. Following docker's best practices, this container does not contain its own database, but instead expects you to supply a running instance. @@ -64,28 +66,55 @@ database instance and configuration you're relying on. Also, this makes this container quite disposable, as it doesn't store any sensitive information at all. -#### Starting a database instance +#### PostgreSQL container -This container requires a PostgreSQL database instance. You're free to pick (or build) -any, as long as is exposes its database port (5432) to the outside. +The recommended way to run this container is by linking it to a PostgreSQL database instance. +You're free to pick (or build) any PostgreSQL container, as long as it exposes +its database port (5432) to the outside. Example: ```bash -$ sudo docker run -d --name=ttrssdb nornagon/postgres +$ docker run -d --name=tinydatabase nornagon/postgres:latest ``` -#### Testing ttrss in foreground +Use the following database options when running the container: -For testing purposes it's recommended to initially start this container in foreground. -This is particular useful for your initial database setup, as errors get reported to -the console and further execution will halt. +``` +--link tinydatabase:db +``` + +#### MySQL container + +If you'd like to use ttrss with a mysql database backend, simply link it to a +mysql container instead. +You're free to pick (or build) any MySQL container, as long as it exposes +its database port (3306) to the outside. + +Example: ```bash -$ sudo docker run -it --link ttrssdb:db -p 80:80 clue/ttrss +$ docker run -d --name=tinydatabase sameersbn/mysql:latest +``` + +Use the following database options when running the container: + +``` +--link tinydatabase:db +``` + +#### External database server + +If you already have a PostgreSQL or MySQL server around off docker you also can go with +that. +Instead of linking docker containers you need to provide database hostname and port like so: + +``` +-e DB_HOST=172.17.42.1 +-e DB_PORT=3306 ``` -##### Database configuration +### Database configuration Whenever your run ttrss, it will check your database setup. It assumes the following default configuration, which can be changed by passing the following additional arguments: @@ -96,7 +125,15 @@ default configuration, which can be changed by passing the following additional -e DB_PASS=ttrss ``` -##### Database superuser +If your database is exposed on a non-standard port you also need to provide DB_TYPE set +to either "pgsql" or "mysql". + +``` +-e DB_TYPE=pgsql +-e DB_TYPE=mysql +``` + +### Database superuser When you run ttrss, it will check your database setup. If it can not connect using the above configuration, it will automatically try to create a new database and user. @@ -110,38 +147,17 @@ following additional arguments: -e DB_ENV_PASS=docker ``` -#### Running with external database server - -If you already have a PostgreSQL or MySQL server around off docker you also can go with -that. Instead of linking docker containers you need to provide database hostname, port, -database name and user credentials manually like so: - -``` --e DB_HOST=172.17.42.1 --e DB_PORT=3306 --e DB_NAME=ttrss --e DB_USER=ttrssuser --e DB_PASS=ttrsspass -``` - -If your database is exposed on a non-standard port you also need to provide DB_TYPE set -to either "pgsql" or "mysql". +### Testing ttrss in foreground -#### Running with mysql database server - -If you'd like to use ttrss with a mysql database backend, simply use the additional -database configuration arguments to docker mentioned above. - -You also might want to link ttrss container to a mysql container. If the mysql server -is exposed on port 3306 it will be detected automatically, otherwise you need to specify -DB_TYPE env flag. +For testing purposes it's recommended to initially start this container in foreground. +This is particular useful for your initial database setup, as errors get reported to +the console and further execution will halt. ```bash -$ sudo docker run -name mysql -d sameersbn/mysql:latest -$ sudo docker run -it --link mysql:db -p 80:80 clue/ttrss +$ docker run -it --link tinydatabase:db -p 80:80 clue/ttrss ``` -#### Running ttrss daemonized +### Running ttrss daemonized Once you've confirmed everything works in the foreground, you can start your container in the background by replacing the `-it` argument with `-d` (daemonize). @@ -149,5 +165,5 @@ Remaining arguments can be passed just like before, the following is the recomme minimum: ```bash -$ sudo docker run -d --link tinystore:db -p 80:80 clue/ttrss +$ docker run -d --link tinydatabase:db -p 80:80 clue/ttrss ``` From 6c309d460bcd0b54815dca5f8c6dba7dfd4b51b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 9 Nov 2014 18:21:11 +0100 Subject: [PATCH 5/5] Explain database superuser --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 97de8c3..2bfaf00 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,16 @@ The recommended way to run this container is by linking it to a PostgreSQL datab You're free to pick (or build) any PostgreSQL container, as long as it exposes its database port (5432) to the outside. -Example: +Example with nornagon/postgres: ```bash $ docker run -d --name=tinydatabase nornagon/postgres:latest ``` +> The image nornagon/postgres exposes a database superuser that this image uses +to automatically create its user and database, +so you don't have to setup your database credentials here. + Use the following database options when running the container: ``` @@ -91,12 +95,15 @@ mysql container instead. You're free to pick (or build) any MySQL container, as long as it exposes its database port (3306) to the outside. -Example: +Example with sameersbn/mysql: ```bash -$ docker run -d --name=tinydatabase sameersbn/mysql:latest +$ docker run -d --name=tinydatabase -e DB_USER=ttrss -e DB_PASS=ttrss -e DB_NAME=ttrss sameersbn/mysql:latest ``` +> The image sameersbn/mysql does not expose a database superuser, +so you have to explicitly pass the database credentials here. + Use the following database options when running the container: ``` @@ -105,8 +112,7 @@ Use the following database options when running the container: #### External database server -If you already have a PostgreSQL or MySQL server around off docker you also can go with -that. +If you already have a PostgreSQL or MySQL server around off docker you also can go with that. Instead of linking docker containers you need to provide database hostname and port like so: ```