DVL-009 Adding Redis docker container

This commit is contained in:
cytopia 2017-04-23 13:20:32 +02:00
parent e344ff2243
commit 2c8e56dd7a
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
6 changed files with 721 additions and 9 deletions

View File

@ -81,8 +81,17 @@ function loadClass($class) {
$_LOADED_LIBS[$class] = \devilbox\Postgres::getInstance($Docker->getEnv('POSTGRES_USER'), $Docker->getEnv('POSTGRES_PASSWORD'), $POSTGRES_HOST_ADDR);
break;
// Get optional docker classes
default:
exit('Class does not exist: '.$class);
// Redis
if ($class == 'Redis' && loadClass('Docker')->getEnv('COMPOSE_OPTIONAL') == 1) {
require $LIB_DIR . DIRECTORY_SEPARATOR . $class . '.php';
$_LOADED_LIBS[$class] = \devilbox\Redis::getInstance('redis');
break;
} else {
exit('Class does not exist: '.$class);
}
}
return $_LOADED_LIBS[$class];
}
@ -94,3 +103,4 @@ function loadClass($class) {
// Temporarily disable due to:
// https://github.com/cytopia/devilbox/issues/8
$ENABLE_VHOST_DNS_CHECK = false;

View File

@ -316,6 +316,74 @@
<!-- ################################################################################ -->
<!-- ### -->
<!-- ### OPTIONAL DOCKER CONTAINER MOUNTS-->
<!-- ### -->
<!-- ################################################################################ -->
<?php if ($Docker->getEnv('COMPOSE_OPTIONAL') == 1): ?>
<br/>
<br/>
<div class="row">
<div class="col-md-12">
<h2 class="text-xs-center">Additional Docker container</h2>
</div>
</div>
<br/>
<br/>
<div class="row">
<!-- ############################################################ -->
<!-- Redis Docker Circle -->
<!-- ############################################################ -->
<div class="col-md-3">
<div class="circles">
<div>
<div class="bg-danger">
<div>
<div>
<h3><?php echo loadClass('Redis')->getVersion();?></h3>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br/><br/>
<div class="row">
<!-- ############################################################ -->
<!-- Redis Docker -->
<!-- ############################################################ -->
<div class="col-md-3">
<table class="table table-striped table-sm font-small">
<thead class="thead-inverse">
<tr>
<th colspan="2">Redis docker</th>
</tr>
</thead>
<tbody>
<tr>
<th>IP</th>
<td><?php echo gethostbyname('redis');?></td>
</tr>
<tr>
<th>Hostname</th>
<td><?php echo 'redis';?></td>
</tr>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
<br/>
<br/>
@ -435,6 +503,38 @@
</div>
<!-- ################################################################################ -->
<!-- ### -->
<!-- ### OPTIONAL DOCKER CONTAINER MOUNTS-->
<!-- ### -->
<!-- ################################################################################ -->
<?php if ($Docker->getEnv('COMPOSE_OPTIONAL') == 1): ?>
<div class="row">
<!-- ############################################################ -->
<!-- Redis Docker Mounts -->
<!-- ############################################################ -->
<div class="col-md-3">
<table class="table table-striped table-sm font-small">
<thead class="thead-inverse">
<tr>
<th>Redis docker</th>
<th>host</th>
</tr>
</thead>
<tbody>
<tr>
<th>Log directory</th>
<td>./log</td>
</tr>
</tbody>
</table>
</div>
<?php endif; ?>
</div><!-- /.container -->
<?php require '../include/footer.php'; ?>

View File

@ -0,0 +1,207 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Redis
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
/**
* Redis instance
* @var Redis|null
*/
protected static $instance = null;
/**
* Singleton Instance getter.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
* @return object|null
*/
public static function getInstance($host = null)
{
if (!isset(static::$instance)) {
static::$instance = new static($host);
}
// If current Redis instance was unable to connect
if (!static::$instance) {
loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
//return null;
}
return static::$instance;
}
/**
* Connect to Redis
*
* @param string $err Reference to error message
* @param string $host Redis hostname
* @return boolean
*/
public static function testConnection(&$err, $host)
{
$err = false;
// Silence errors and try to connect
error_reporting(0);
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
$err = 'Failed to connect to Redis host on '.$host.': ' .$redis->getLastError();
error_reporting(-1);
return false;
}
error_reporting(-1);
$redis->close();
return true;
}
/*********************************************************************************
*
* Private Variables
*
*********************************************************************************/
/**
* Redis instance
* @var object|null
*/
private $_redis = null;
/**
* Connection error string
* @var string
*/
private $_connect_error = '';
/**
* Connection error code
* @var integer
*/
private $_connect_errno = 0;
/**
* Error string
* @var string
*/
private $_error = '';
/**
* Error code
* @var integer
*/
private $_errno = 0;
/*********************************************************************************
*
* Construct/Destructor
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct($host)
{
// Silence errors and try to connect
error_reporting(0);
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
$this->_connect_error = 'Failed to connect to Redis host on '.$host.': ' .$redis->getLastError();
$this->_connect_errno = 1;
loadClass('Logger')->error($this->_connect_error);
} else {
$this->_redis = $redis;
}
error_reporting(-1);
}
/**
* Destructor
*/
public function __destruct()
{
if ($this->_redis) {
$this->_redis->close();
}
}
/*********************************************************************************
*
* Redis Select functions
*
*********************************************************************************/
public function getVersion()
{
$info = $this->_redis->info();
return 'Redis '.$info['redis_version'];
}
/*********************************************************************************
*
* MySQL Error functions
*
*********************************************************************************/
/**
* Return connection error message.
*
* @return string Error message
*/
public function getConnectError()
{
return $this->_connect_error;
}
/**
* Return connection errno code.
*
* @return integer Error code
*/
public function getConnectErrno()
{
return $this->_connect_errno;
}
/**
* Return error message.
*
* @return string Error message
*/
public function getError()
{
return $this->_error;
}
/**
* Return errno code.
*
* @return integer Error code
*/
public function getErrno()
{
return $this->_errno;
}
}

View File

@ -52,10 +52,20 @@ $ cp env-example .env
# Edit your configuration
$ vim .env
# Start the dockers
# Start the containers (base-stack)
$ docker-compose up
# Or instead of the above base-stack, you can also additionally load the
# optional stack.
# Use this command instead:
$ docker-compose -f docker-compose.optional.yml up
```
## Updates
In case you update this repository locally on the master branch (e.g.: `git pull origin master`), make sure to repull all docker containers as they very likely have also been up updated.
Otherwise you might run into problems.
[What is the `.env` file?](https://docs.docker.com/compose/env-file/)
## Documentation
@ -75,8 +85,15 @@ Select your prefered setup.
No need to install and configure different versions locally. Simply choose your required LAMP/LEMP stack combination during startup and it is up and running instantly.
**Note:** Some docker container combinations might not work well. See the overall build-matrix for possible problems: \[ [![Build Status](https://travis-ci.org/cytopia/devilbox.svg?branch=master)](https://travis-ci.org/cytopia/devilbox) \]
**Base stack**
If you only want to use the base stack, use `docker-compose.yml` (default):
```shell
$ docker-compose up
```
| Webserver | MySQL | PostgreSQL | PHP |
|-----------|-------|------------|-----|
| [![Build Status](https://travis-ci.org/cytopia/docker-apache-2.2.svg?branch=master)](https://travis-ci.org/cytopia/docker-apache-2.2) [Apache 2.2](https://github.com/cytopia/docker-apache-2.2) | [![Build Status](https://travis-ci.org/cytopia/docker-mysql-5.5.svg?branch=master)](https://travis-ci.org/cytopia/docker-mysql-5.5) [MySQL 5.5](https://github.com/cytopia/docker-mysql-5.5) | [![Build Status](https://travis-ci.org/docker-library/postgres.svg?branch=master)](https://travis-ci.org/docker-library/postgres/branches) [PgSQL 9.2](https://hub.docker.com/_/postgres/) | [![Build Status](https://travis-ci.org/cytopia/docker-php-fpm-5.4.svg?branch=master)](https://travis-ci.org/cytopia/docker-php-fpm-5.4) [PHP 5.4](https://github.com/cytopia/docker-php-fpm-5.4) |
@ -88,17 +105,20 @@ No need to install and configure different versions locally. Simply choose your
| | [![Build Status](https://travis-ci.org/cytopia/docker-mariadb-10.1.svg?branch=master)](https://travis-ci.org/cytopia/docker-mariadb-10.1) [MariaDB 10.1](https://github.com/cytopia/docker-mariadb-10.1) | |
| | [![Build Status](https://travis-ci.org/cytopia/docker-mariadb-10.2.svg?branch=master)](https://travis-ci.org/cytopia/docker-mariadb-10.2) [MariaDB 10.2](https://github.com/cytopia/docker-mariadb-10.2) | |
<sub>**Incompatible choices:**<sub><br/>
<sub>- Apache 2.2 and HHVM do not work together</sub>
**Optional NoSQL stack**
In order to also use the docker containers below, use the `docker-compose.optional.yml` instead:
```shell
$ docker-compose -f docker-compose.optional.yml up
```
| Cassandra | CouchDB | Memcached | MongoDB | Redis |
|-----------|---------|-----------|---------|-------|
| Cassandra 2.1 | CouchDB 1.6 | Memcached latest | MongoDB 2.6 | Redis 2.8 |
| Cassandra 2.2 | CouchDB 2.0 | | MongoDB 3.0 | Redis 3.0 |
| Cassandra 3.0 | | | MongoDB 3.2 | Redis 3.2 |
| | | | MongoDB 3.4 | Redis unstable |
| Cassandra 2.1 | CouchDB 1.6 | Memcached latest | MongoDB 2.6 | [![Travis CI](https://img.shields.io/travis/docker-library/redis/master.svg)](https://travis-ci.org/docker-library/redis/branches) [Redis 2.8](https://github.com/docker-library/redis) |
| Cassandra 2.2 | CouchDB 2.0 | | MongoDB 3.0 | [![Travis CI](https://img.shields.io/travis/docker-library/redis/master.svg)](https://travis-ci.org/docker-library/redis/branches) [Redis 3.0](https://github.com/docker-library/redis) |
| Cassandra 3.0 | | | MongoDB 3.2 | [![Travis CI](https://img.shields.io/travis/docker-library/redis/master.svg)](https://travis-ci.org/docker-library/redis/branches) [Redis 3.2](https://github.com/docker-library/redis) |
| | | | MongoDB 3.4 | |
<sub>**Note:** Entries without links or without build-status are not yet available, but are coming soon. See [ROADMAP](https://github.com/cytopia/devilbox/issues/23) for tasks and upcoming features.</sub>

352
docker-compose.optional.yml Normal file
View File

@ -0,0 +1,352 @@
##
## -------------------------
## | D E V I L S T A C K |
## -------------------------
##
## Local LAMP/LEMP stack
##
##
##
##
##
## -- DO NOT EDIT THIS FILE --
##
## Edit '.env' for configuration.
##
## If '.env' does not exist, copy 'env-example' to '.env'
## $ cp env-example .env
##
version: '2'
################################################################################
# SERVICES
################################################################################
services:
# ----------------------------------------
# HTTP
# ----------------------------------------
httpd:
# TODO: remove latest, once it is ready for the next release
image: cytopia/${HTTPD_SERVER}:latest
# Manually build via `docker-compose build`
#build:
#context: https://github.com/cytopia/docker-${HTTPD_SERVER}.git#1
# context: https://github.com/cytopia/docker-${HTTPD_SERVER}.git
environment:
# Show all executed commands during docker entrypoint?
- DEBUG_COMPOSE_ENTRYPOINT=${DEBUG_COMPOSE_ENTRYPOINT}
# Adjust timezone
- TIMEZONE=${TIMEZONE}
# Enable PHP-FPM support
- PHP_FPM_ENABLE=1
- PHP_FPM_SERVER_ADDR=172.16.238.11
- PHP_FPM_SERVER_PORT=9000
# Tell the webserver to look into this directory
# for additional configuration files.
#
# @see volumes:: - ./etc/${HTTPD_SERVER}:/etc/${HTTPD_SERVER}
- CUSTOM_HTTPD_CONF_DIR=/etc/${HTTPD_SERVER}
ports:
# ---- Format: ----
# [HOST-ADDR : ] HOST-PORT : DOCKER-PORT
- "${LOCAL_LISTEN_ADDR}${HOST_PORT_HTTPD}:80"
networks:
app_net:
ipv4_address: 172.16.238.10
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Custom scripts/binaries required for httpd server vhost
# configuration to work.
# (configured in /etc/${HTTPD_SERVER}/02-vhost-mass.conf)
- ${DEVILBOX_PATH}/.devilbox/bin/${HTTPD_SERVER}:/opt/bin:ro
# Mount user-defined httpd configuration files
# @see environment::CUSTOM_HTTPD_CONF_DIR for how this
# is added in httpd server
- ${DEVILBOX_PATH}/.devilbox/etc/${HTTPD_SERVER}:/etc/${HTTPD_SERVER}:ro
# Mount custom intranet
# (configured in /etc/${HTTPD_SERVER}/01-vhost-default.conf)
- ${DEVILBOX_PATH}/.devilbox/www:/var/www/default:ro
# Mount user-defined httpd log
# @see ./etc/${HTTPD_SERVER}/*.conf for log defines
- ${DEVILBOX_PATH}/log/${HTTPD_SERVER}:/var/log/${HTTPD_SERVER}
# Mount custom mass virtual hosting
# (configured in /etc/${HTTPD_SERVER}/02-vhost-mass.conf)
- ${HOST_PATH_TO_WWW_DOCROOTS}:/shared/httpd:ro
depends_on:
- php
# ----------------------------------------
# PHP-FPM
# ----------------------------------------
php:
# TODO: remove latest, once it is ready for the next release
image: cytopia/${PHP_SERVER}:latest
# Manually build via `docker-compose build`
#build:
#context: https://github.com/cytopia/docker-${PHP_SERVER}.git#1
# context: https://github.com/cytopia/docker-${PHP_SERVER}.git
environment:
# Show all executed commands during docker entrypoint?
- DEBUG_COMPOSE_ENTRYPOINT=${DEBUG_COMPOSE_ENTRYPOINT}
# Adjust timezone
- TIMEZONE=${TIMEZONE}
##
## PHP Xdebug
##
- PHP_XDEBUG_ENABLE=${PHP_XDEBUG_ENABLE}
- PHP_XDEBUG_REMOTE_PORT=${PHP_XDEBUG_REMOTE_PORT}
- PHP_XDEBUG_REMOTE_HOST=${PHP_XDEBUG_REMOTE_HOST}
##
## Postfix on
##
- ENABLE_MAIL=1
##
## Map remote MySQL Port to 127.0.0.1
##
- FORWARD_MYSQL_PORT_TO_LOCALHOST=1
- MYSQL_REMOTE_ADDR=172.16.238.12
- MYSQL_REMOTE_PORT=3306
- MYSQL_LOCAL_PORT=3306
##
## Mount remote MySQL socket file to local disk
##
- MOUNT_MYSQL_SOCKET_TO_LOCALDISK=1
- MYSQL_SOCKET_PATH=/tmp/mysql/mysqld.sock
##
## Map remote PostgreSQL Port to 127.0.0.1
##
# TODO
##
## Mount remote PostgreSQL socket file to local disk
##
# TODO
##
## Additional variables needed by custom intranet
##
- HOST_PATH_TO_WWW_DOCROOTS=${HOST_PATH_TO_WWW_DOCROOTS}
- HOST_PORT_HTTPD=${HOST_PORT_HTTPD}
- HOST_PATH_TO_MYSQL_DATADIR=${HOST_PATH_TO_MYSQL_DATADIR}
- HOST_PATH_TO_POSTGRES_DATADIR=${HOST_PATH_TO_POSTGRES_DATADIR}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
##
## Register optional enabled docker containers to PHP
##
- COMPOSE_OPTIONAL=1
networks:
app_net:
ipv4_address: 172.16.238.11
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Mount custom intranet
# (configured in /etc/${HTTPD_SERVER}/01-vhost-default.conf)
- ${DEVILBOX_PATH}/.devilbox/www:/var/www/default:ro
# Mount logs
- ${DEVILBOX_PATH}/log/${PHP_SERVER}:/var/log/php-fpm
# Mount MySQL Socket directory
- mysql_socket_volume:/tmp/mysql
# Mount Mail directory
#- ${DEVILBOX_PATH}/run/mail:/var/mail
# Mount devilbox user-defined *.ini files in order
# to overwrite the default PHP configuration
- ${DEVILBOX_PATH}/cfg/${PHP_SERVER}:/etc/php-custom.d:ro
# Mount custom mass virtual hosting
# (configured in /etc/${HTTPD_SERVER}/02-vhost-mass.conf)
- ${HOST_PATH_TO_WWW_DOCROOTS}:/shared/httpd
depends_on:
- mysql
- postgres
- redis
# ----------------------------------------
# DATABASE
# ----------------------------------------
mysql:
# TODO: remove latest, once it is ready for the next release
image: cytopia/${MYSQL_SERVER}:latest
# Manually build via `docker-compose build`
#build:
#context: https://github.com/cytopia/docker-${MYSQL_SERVER}.git#1
# context: https://github.com/cytopia/docker-${MYSQL_SERVER}.git
environment:
# Show all executed commands during docker entrypoint?
- DEBUG_COMPOSE_ENTRYPOINT=${DEBUG_COMPOSE_ENTRYPOINT}
# Adjust timezone
- TIMEZONE=${TIMEZONE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_SOCKET_DIR=/tmp/mysql
# Runtime settings
- MYSQL_GENERAL_LOG=${MYSQL_GENERAL_LOG}
ports:
# [local-machine:]local-port:docker-port
- "${LOCAL_LISTEN_ADDR}${HOST_PORT_MYSQL}:3306"
networks:
app_net:
ipv4_address: 172.16.238.12
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Mount logs
- ${DEVILBOX_PATH}/log/${MYSQL_SERVER}:/var/log/mysql
# Mount MySQL Socket directory
- mysql_socket_volume:/tmp/mysql
# Mount devilbox default overwrites
- ${DEVILBOX_PATH}/.devilbox/etc/${MYSQL_SERVER}:/etc/mysql/docker-default.d:ro
# Mount devilbox user-defined cnf files in order
# to overwrite the MySQL server configuration
- ${DEVILBOX_PATH}/cfg/${MYSQL_SERVER}:/etc/mysql/conf.d:ro
# Mount MySQL Data directory
- ${HOST_PATH_TO_MYSQL_DATADIR}:/var/lib/mysql
# ----------------------------------------
# POSTGRES
# ----------------------------------------
postgres:
image: postgres:${POSTGRES_SERVER}
# Manually build via `docker-compose build`
#build:
#context: https://github.com/cytopia/docker-${MYSQL_SERVER}.git#1
# context: https://github.com/cytopia/docker-${MYSQL_SERVER}.git
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
# [local-machine:]local-port:docker-port
- "${LOCAL_LISTEN_ADDR}${HOST_PORT_POSTGRES}:5432"
networks:
app_net:
ipv4_address: 172.16.238.13
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Mount logs
- ${DEVILBOX_PATH}/log/postgres-${POSTGRES_SERVER}:/var/log/postgresql
# Mount PostgreSQL Socket directory
- pgsql_socket_volume:/var/run/postgresql
# Mount PostgreSQL Data directory
- ${HOST_PATH_TO_POSTGRES_DATADIR}:/var/lib/postgresql/data/pgdata
# ----------------------------------------
# Redis
# ----------------------------------------
redis:
image: redis:${REDIS_SERVER}
ports:
# [local-machine:]local-port:docker-port
- "${LOCAL_LISTEN_ADDR}${HOST_PORT_REDIS}:6379"
networks:
app_net:
ipv4_address: 172.16.238.14
volumes:
# ---- Format: ----
# HOST-DIRECTORY : DOCKER-DIRECTORY
# Mount logs
- ${DEVILBOX_PATH}/log/redis-${REDIS_SERVER}:/var/log/redis
################################################################################
# VOLUMES
################################################################################
volumes:
# Create volume for mysql socket.
# This removes the need to mount the socket to the host
# but be able to mount it to different containers (from another container)
mysql_socket_volume:
# Create volume for postgresql socket.
# This removes the need to mount the socket to the host
# but be able to mount it to different containers (from another container)
pgsql_socket_volume:
################################################################################
# NETWORK
################################################################################
networks:
app_net:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
gateway: 172.16.238.1

View File

@ -101,7 +101,17 @@ PHP_SERVER=php-fpm-7.0
###
### 1.5 Timezone for all dockers and service config files
### 1.5 Choose Redis Server Image
### (only for docker-compose.optional.yml)
### $ docker-compose -f docker-compose.optional.yml up
###
REDIS_SERVER=2.8
#REDIS_SERVER=3.0
#REDIS_SERVER=3.2
###
### 1.6 Timezone for all dockers and service config files
###
TIMEZONE=Europe/Berlin
@ -228,3 +238,16 @@ PHP_XDEBUG_REMOTE_PORT=9000
# where your ide/editor is listening for xdebug connections.
PHP_XDEBUG_REMOTE_HOST=172.20.10.2
## TODO: Check if it works by automatically sending it to the broadcast address
################################################################################
###
### 7. Redis Docker Settings
###
################################################################################
###
### Expost Redis Port to Host
###
HOST_PORT_REDIS=6379