Redis Docker container

Easy Docker set-up using pre-built image:

(Ubuntu host in this example, but others should be similar)

# see

Docker run command:

docker run -d -v $(pwd)/redis.conf:/usr/local/etc/redis/redis.conf \
 -v /data/redis:/data -p 6379:6379 \
 --restart="always" \
 --name myredis redis:3.0.3 redis-server /usr/local/etc/redis/redis.conf
  • On docker host, create data volume so we aren’t reading/writing on the container:
    make sure this path is in the redis.conf file
  • sudo mkdir /data/redis && sudo chown ubuntu:docker $_ && sudo chmod g+w $_

  • if you get this warning:
    WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    > update Docker host:
    echo never > /sys/kernel/mm/transparent_hugepage/enabled

  • redis force save data file to disk command to test external volume:

    security issues:

  • add authentication if needed and check it on remote clients
  • set firewall: (ubuntu example where eth1 is LAN)
    ufw allow in on eth1 to any port 6379 proto tcp

    If you want to bake-in your config file or set other customizations, create Dockerfile:

    FROM redis:3.0.3
    COPY ./redis.conf /usr/local/etc/redis/redis.conf
  • PHP Redis with socket connection

    if you are using Redis on a single server and/or want the web server at the same location to use sockets instead of TCP, update your redis config (redis.conf) to include:

    unixsocket /var/run/redis/redis.sock

    (you may be able to just uncomment that line to use the default)

    You will need to make sure the socket connection has the correct permissions for the apache/php service. You can set the “unixsocketperm” attribute in redis.conf and make sure the socket location is appropriate for your apache/php set-up, or you can add a line in your redis start-up script to update the permissions, something like this:

    chown :apache $SOCK && chmod g+w $SOCK

    I’m not sure what the best practice is here, but I needed to do one the above to get it to work.

    Redis page created

    internal link > Redis Page

    Redis start/stop init.d script for Linux

    The init script that came with Redis (open source, advanced key-value store server) didn’t work and I couldn’t find one for RedHat/CentOS with a quick search, so I combined the parts that did and came up with one that seems to work both for start/stop via /sbin/service as well as /sbin/chkconfig. Note: at a minimum, you must set the “daemonize” value in the redis.conf file to “yes”.

    Use the redis.conf file comes with the distro — edit as needed and copy it to: /etc/redis.conf.
    You can then use the script below and copy that to: /etc/init.d/redis (change the port and any other attributes as needed). (To run multiple servers, change the port (and pid) values as appropriate.)

    Click here to view init.d script for Redis

    # redis        Startup script for Redis Server
    # chkconfig: - 90 10
    # description: Redis is an open source, advanced key-value store. 
    # processname: redis-server
    # config: /etc/redis.conf
    # pidfile: /var/run/
    REDIS_CLI=`which redis-cli`
    case "$1" in
            if [ -f $PIDFILE ]
                    echo -n "$PIDFILE exists, process is already running or crashed\n"
                    echo -n "Starting Redis server...\n"
                    $EXEC $CONF
            if [ ! -f $PIDFILE ]
                    echo -n "$PIDFILE does not exist, process is not running\n"
    		PID=$(cat $PIDFILE)
                    echo -n "Stopping ...\n"
                    while [ -x ${PIDFILE} ]
                        echo "Waiting for Redis to shutdown ..."
                        sleep 1
                    echo "Redis stopped"

    Redis vs Memcached + PHP Libs for Redis

    I like the looks of Redis as a fast utility that falls somewhere between a Sql DB and memory cache.

    Here is one comparison with Memcached, Redis vs Memcached
    the intro: take memcached and add a disk backing, replication, virtual memory, and some cool additional data structures. Hashes, lists, sets, sorting, joining, transactions, yay! I instantly got a geek boner scanning the feature list… (His enthusiasm goes, um, down from there.)

    And if you do want to use it with PHP, see Ori Pekelman’s blog:
    Which PHP Library to use with Redis? The Benchmark
    Basically comes down to using a pure-php library or one that uses a PHP-compiled module and the trade-offs of compatibility vs speed.

    To use it with Yii, see: rediscache

    init.d script for redis