romeoz/docker-redis

By romeoz

Updated almost 7 years ago

Redis container image which can be linked to other containers.

Image

3.9K

Table of Contents

Installation

docker pull romeoz/docker-redis

Alternately you can build the image yourself.

git clone https://github.com/romeoz/docker-redis.git
cd docker-redis
docker build -t="$USER/redis" .

Quick Start

Run the redis container:

docker run --name redis -d \
  -p 6379:6379 \
  romeoz/docker-redis

Command-line arguments

You can customize the launch command of Redis server by specifying arguments to redis on the docker run command. For example the following command for persistent storage:

docker run --name redis -d \
  -p 6379:6379 \
  romeoz/docker-redis --appendonly yes

Setting a specific password

To secure your Redis server with a password, specify the password in the REDIS_PASSWORD variable while starting the container.

docker run --name redis -d \
  -p 6379:6379 \
  -e 'REDIS_PASSWORD=pass' \
  romeoz/docker-redis

Persistence

For Redis to preserve its state across container shutdown and startup you should mount a volume at /var/lib/redis.

docker run --name redis -d \
  -p 6379:6379 \
  -v /host/to/path/data:/var/lib/redis
  romeoz/docker-redis --appendonly yes

Creating cluster (requires Redis 3.0+)

Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes.

Create nodes:

docker network create redis_net

docker run --name node1 -d \
  --net redis_net
  -e 'REDIS_CLUSTER_ENABLED=true' \
  romeoz/docker-redis --appendonly yes

Next, similarly to nodes 2..5.

Note that the minimal cluster that works as expected requires to contain at least three master nodes. For your first tests it is strongly suggested to start a six nodes cluster with three masters and three slaves.

Use the 6-node as a provider:

docker run --name node6 -d \
  --net redis_net
  -e 'REDIS_CLUSTER_ENABLED=true' \
  romeoz/docker-redis --appendonly yes

For Redis 3.2 param protected-mode must be as no. Example: docker exec -it node1 bash -c "echo 'CONFIG SET protected-mode no' | redis-cli -c" or protected-mode no to redis.conf.

Now that we have a number of instances running, we need to create our cluster by writing some meaningful configuration to the nodes. For this we use the utility redis-trib:

docker exec -it node6 bash -c '
IP=$(ifconfig | grep "inet addr:17" | cut -f2 -d ":" | cut -f1 -d " ") \

echo "yes" | \
ruby /redis-trib.rb create --replicas 1 ${IP}:6379 node1:6379 node2:6379 node3:6379 node3:6379 node4:6379 node5:6379'  

List of added nodes can be viewed with query cluster nodes:

docker exec -it node6 redis-cli cluster node

For more information you can refer to the official documentation.

Environment variables

REDIS_PASSWORD: Set a specific password for the admin account.

REDIS_CLUSTER_ENABLED: Run Redis server as cluster (default "false").

REDIS_CLUSTER_NODE_TIMEOUT: The maximum amount of time a Redis Cluster node can be unavailable, without it being considered as failing. If a master node is not reachable for more than the specified amount of time, it will be failed over by its slaves (default "5000").

Logging

All the logs are forwarded to stdout and sterr. You have use the command docker logs.

docker logs redis

####Split the logs

You can then simply split the stdout & stderr of the container by piping the separate streams and send them to files:

docker logs redis > stdout.log 2>stderr.log
cat stdout.log
cat stderr.log

or split stdout and error to host stdout:

docker logs redis > -
docker logs redis 2> -

####Rotate logs

Create the file /etc/logrotate.d/docker-containers with the following text inside:

/var/lib/docker/containers/*/*.log {
    rotate 31
    daily
    nocompress
    missingok
    notifempty
    copytruncate
}

Optionally, you can replace nocompress to compress and change the number of days.

Out of the box

  • Ubuntu 14.04 LTS
  • Redis 2.8, 3.0, 3.2 or 4.0

License

Redis docker image is open-sourced software licensed under the MIT license

Docker Pull Command

docker pull romeoz/docker-redis