From 92bfd747c5dee5ab578e552c3fe251cf116bc082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 26 Apr 2014 17:24:57 +0200 Subject: [PATCH] Initial commit --- Dockerfile | 33 ++++++++++++++ README.md | 94 ++++++++++++++++++++++++++++++++++++++ configure-db.php | 114 +++++++++++++++++++++++++++++++++++++++++++++++ supervisord.conf | 15 +++++++ ttrss.nginx.conf | 18 ++++++++ 5 files changed, 274 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 configure-db.php create mode 100644 supervisord.conf create mode 100644 ttrss.nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bee7a3f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM ubuntu +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 + +# add ttrss as the only nginx site +ADD ttrss.nginx.conf /etc/nginx/sites-available/ttrss +RUN ln -s /etc/nginx/sites-available/ttrss /etc/nginx/sites-enabled/ttrss +RUN rm /etc/nginx/sites-enabled/default + +# install ttrss and patch configuration +RUN git clone https://github.com/gothfox/Tiny-Tiny-RSS.git /var/www +WORKDIR /var/www +RUN cp config.php-dist config.php +RUN sed -i -e "/'SELF_URL_PATH'/s/ '.*'/ 'http:\/\/localhost\/'/" config.php +RUN chown www-data:www-data -R /var/www + +# expose only nginx HTTP port +EXPOSE 80 + +# expose default database credentials via ENV in order to ease overwriting +ENV DB_NAME ttrss +ENV DB_USER ttrss +ENV DB_PASS ttrss + +# always re-configure database with current ENV when RUNning container, then monitor all services +ADD configure-db.php /configure-db.php +ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf +CMD php /configure-db.php && supervisord -c /etc/supervisor/conf.d/supervisord.conf + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c48ba24 --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# docker-ttrss + +This docker image allows you to run the [Tiny Tiny RSS](http://tt-rss.org) feed reader. +Keep your feed history to yourself and access your RSS and atom feeds from everywhere. +You can access it through an easy to use webinterface on your desktop, your mobile browser +or using one of available apps. + +## Instructions + +```bash +$ git clone https://github.com/clue/docker-ttrss.git +$ cd docker-ttrss +$ sudo docker build -t ttrss . +``` + +### Running + +Following docker's best practices, this container does not contain its own database, +but instead expects you to supply a running instance. +While slightly more complicated at first, this gives your more freedom as to which +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 + +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. + +Example: + +```bash +$ sudo docker pull nornagon/postgres +$ sudo docker run -d --name=tinystore nornagon/postgres +``` + +#### Testing ttrss in foreground + +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 -it --link tinystore:db -p 80:80 ttrss +``` + +##### 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: + +``` +-e DB_NAME=ttrss +-e DB_USER=ttrss +-e DB_PASS=ttrss +``` + +##### 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. + +For this to work, it will need a superuser account that is permitted to create a new database +and user. It assumes the following default configuration, which can be changed by passing the +following additional arguments: + +``` +-e DB_ENV_USER=docker +-e DB_ENV_PASS=docker +``` + +#### 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). +Remaining arguments can be passed just like before, the following is the recommended +minimum: + +```bash +$ sudo docker run -d --link tinystore:db -p 80:80 ttrss +``` + +### Accessing your webinterface + +The above examples expose the Tiny Tiny RSS webinterface on port 80, so that you can browse to: + +http://localhost/ + +The default login credentials are: + +Username: admin +Password: password + +Obviously, you're recommended to change those ASAP. diff --git a/configure-db.php b/configure-db.php new file mode 100644 index 0000000..2cc0807 --- /dev/null +++ b/configure-db.php @@ -0,0 +1,114 @@ +#!/usr/bin/env php +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)) { + echo 'Database login created and confirmed' . PHP_EOL; + } else { + error('Database login failed, trying to create login failed as well'); + } +} + +$pdo = dbconnect($config); +try { + $pdo->query('SELECT 1 FROM ttrss_feeds'); + // reached this point => table found, assume db is complete +} +catch (PDOException $e) { + echo 'Database table not found, applying schema... ' . PHP_EOL; + $schema = file_get_contents('schema/ttrss_schema_' . $config['DB_TYPE'] . '.sql'); + $schema = preg_replace('/--(.*?);/', '', $schema); + $schema = preg_replace('/[\r\n]/', ' ', $schema); + $schema = trim($schema, ' ;'); + foreach (explode(';', $schema) as $stm) { + $pdo->exec($stm); + } + unset($pdo); +} + +$contents = file_get_contents($confpath); +foreach ($config as $name => $value) { + $contents = preg_replace('/(define\s*\(\'' . $name . '\',\s*)(.*)(\);)/', '$1"' . $value . '"$3', $contents); +} +file_put_contents($confpath, $contents); + +function env($name, $default = null) +{ + $v = getenv($name) ?: $default; + + if ($v === null) { + error('The env ' . $name . ' does not exist'); + } + + return $v; +} + +function error($text) +{ + echo 'Error: ' . $text . PHP_EOL; + exit(1); +} + +function dbconnect($config) +{ + $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME', 'user' => 'USER', 'password' => 'PASS'); + $dsn = $config['DB_TYPE'] . ':'; + foreach ($map as $d => $h) { + if (isset($config['DB_' . $h])) { + $dsn .= $d . '=' . $config['DB_' . $h] . ';'; + } + } + $pdo = new \PDO($dsn); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + return $pdo; +} + +function dbcheck($config) +{ + try { + dbconnect($config); + return true; + } + catch (PDOException $e) { + return false; + } +} + diff --git a/supervisord.conf b/supervisord.conf new file mode 100644 index 0000000..f117df0 --- /dev/null +++ b/supervisord.conf @@ -0,0 +1,15 @@ +[supervisord] +nodaemon=true + +[program:php5-fpm] +command=/usr/sbin/php5-fpm --nodaemonize + +[program:nginx] +command=/usr/sbin/nginx -g "daemon off;" + +[program:ttrss-update-daemon] +command=/usr/bin/php /var/www/update_daemon2.php +user=www-data +stdout_logfile=/tmp/%(program_name)s.stdout +stderr_logfile=/tmp/%(program_name)s.stderr + diff --git a/ttrss.nginx.conf b/ttrss.nginx.conf new file mode 100644 index 0000000..fa46f23 --- /dev/null +++ b/ttrss.nginx.conf @@ -0,0 +1,18 @@ +server { + listen 80; + root /var/www; + + index index.php index.html; + + location / { + try_files $uri $uri/ =404; + } + + location ~ \.php$ { + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass unix:/var/run/php5-fpm.sock; + fastcgi_index index.php; + include fastcgi_params; + } +} +