[Docker Redis] Run Redis with Bitnami, Docker and Docker Compose
Bitnami Redis with Docker and Docker Compose
Bitnami makes it easy to get your favorite open source software up and running on any platform, including your laptop, Kubernetes and all the major clouds. In addition to popular community offerings, Bitnami, now part of VMware, provides IT organizations with an enterprise offering that is secure, compliant, continuously maintained and customizable to your organizational policies.
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Prerequisites
-
Docker
Docker is a tool to build safer, share wider, run faster: New updates to our product subscriptions.
See Empowering App Development for Developers | Docker - https://www.docker.com/
to learn more. -
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
See Overview of Docker Compose | Docker Documentation - https://docs.docker.com/compose/ to learn more.
Examples
Docker
Run Redis with Docker.
1 | docker run \ |
Docker Compose
Create or edit docker-compose.yml
file:
1 | version: '2' |
Run Redis with Docker Compose.
1 | docker-compose up |
See bitnami-docker-redis/docker-compose.yml at master · bitnami/bitnami-docker-redis - https://github.com/bitnami/bitnami-docker-redis/blob/master/docker-compose.yml to learn more.
Configuration
Persisting your database
Redis™ provides a different range of persistence options. This contanier uses AOF persistence by default but it is easy to overwrite that configuration in a docker-compose.yaml
file with this entry command: /opt/bitnami/scripts/redis/run.sh --appendonly no
. Alternatively, you may use the REDIS_AOF_ENABLED
env variable as explained in Disabling AOF persistence.
If you remove the container all your data will be lost, and the next time you run the image the database will be reinitialized. To avoid this loss of data, you should mount a volume that will persist even after the container is removed.
For persistence you should mount a directory at the /bitnami
path. If the mounted directory is empty, it will be initialized on the first run.
1 | docker run \ |
You can also do this by modifying the docker-compose.yml
file present in this repository:
1 | services: |
NOTE: As this is a non-root container, the mounted files and directories must have the proper permissions for the UID 1001
.
Disabling Redis™ commands
For security reasons, you may want to disable some commands. You can specify them by using the following environment variable on the first run:
REDIS_DISABLE_COMMANDS
: Comma-separated list of Redis™ commands to disable. Defaults to empty.
1 | docker run --name redis -e REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL,CONFIG bitnami/redis:latest |
Alternatively, modify the docker-compose.yml
file present in this repository:
1 | services: |
As specified in the docker-compose, FLUSHDB
and FLUSHALL
commands are disabled. Comment out or remove the environment variable if you don’t want to disable any commands:
1 | services: |
Passing extra command-line flags to redis-server startup
Passing extra command-line flags to the redis service command is possible by adding them as arguments to run.sh script:
1 | docker run --name redis -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis:latest /opt/bitnami/scripts/redis/run.sh --maxmemory 100mb |
Alternatively, modify the docker-compose.yml
file present in this repository:
1 | services: |
Refer to the Redis™ documentation - https://redis.io/topics/config#passing-arguments-via-the-command-line for the complete list of arguments.
Setting the server password on first run
Passing the REDIS_PASSWORD
environment variable when running the image for the first time will set the Redis™ server password to the value of REDIS_PASSWORD
(or the content of the file specified in REDIS_PASSWORD_FILE
).
1 | docker run --name redis -e REDIS_PASSWORD=password123 bitnami/redis:latest |
Alternatively, modify the docker-compose.yml
file present in this repository:
1 | services: |
NOTE: The at sign (@) is not supported for REDIS_PASSWORD.
Warning The Redis™ database is always configured with remote access enabled. It’s suggested that the REDIS_PASSWORD
env variable is always specified to set a password. In case you want to access the database without a password set the environment variable ALLOW_EMPTY_PASSWORD=yes
. This is recommended only for development.
Allowing empty passwords
By default the Redis™ image expects all the available passwords to be set. In order to allow empty passwords, it is necessary to set the ALLOW_EMPTY_PASSWORD=yes
env variable. This env variable is only recommended for testing or development purposes. We strongly recommend specifying the REDIS_PASSWORD
for any other scenario.
1 | docker run --name redis -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis:latest |
Alternatively, modify the docker-compose.yml file present in this repository:
1 | services: |
Disabling AOF persistence
Redis™ offers different options when it comes to persistence. By default, this image is set up to use the AOF (Append Only File) approach. Should you need to change this behaviour, setting the REDIS_AOF_ENABLED=no
env variable will disable this feature.
1 | docker run --name redis -e REDIS_AOF_ENABLED=no bitnami/redis:latest |
Alternatively, modify the docker-compose.yml file present in this repository:
1 | services: |
Setting up replication
A replication cluster can easily be setup with the Bitnami Redis™ Docker Image using the following environment variables:
-
REDIS_REPLICATION_MODE
: The replication mode. Possible values master/slave. No defaults. -
REDIS_REPLICA_IP
: The replication announce ip. Defaults to $(get_machine_ip) which return the ip of the container. -
REDIS_REPLICA_PORT
: The replication announce port. Defaults toREDIS_MASTER_PORT_NUMBER
. -
REDIS_MASTER_HOST
: Hostname/IP of replication master (replica node parameter). No defaults. -
REDIS_MASTER_PORT_NUMBER
: Server port of the replication master (replica node parameter). Defaults to6379
. -
REDIS_MASTER_PASSWORD
: Password to authenticate with the master (replica node parameter). No defaults. As an alternative, you can mount a file with the password and set theREDIS_MASTER_PASSWORD_FILE
variable.
In a replication cluster you can have one master and zero or more replicas. When replication is enabled the master node is in read-write mode, while the replicas are in read-only mode. For best performance its advisable to limit the reads to the replicas.
Step 1: Create the replication master
The first step is to start the Redis™ master.
1 | docker run --name redis-master \ |
In the above command the container is configured as the master using the REDIS_REPLICATION_MODE
parameter. The REDIS_PASSWORD
parameter enables authentication on the Redis™ master.
Step 2: Create the replica node
Next we start a Redis™ replica container.
1 | docker run --name redis-replica \ |
In the above command the container is configured as a slave using the REDIS_REPLICATION_MODE
parameter. The REDIS_MASTER_HOST
, REDIS_MASTER_PORT_NUMBER
and REDIS_MASTER_PASSWORD
parameters are used connect and authenticate with the Redis™ master. The REDIS_PASSWORD
parameter enables authentication on the Redis™ replica.
You now have a two node Redis™ master/replica replication cluster up and running which can be scaled by adding/removing replicas.
If the Redis™ master goes down you can reconfigure a replica to become a master using:
1 | docker exec redis-replica redis-cli -a password123 SLAVEOF NO ONE |
Note: The configuration of the other replicas in the cluster needs to be updated so that they are aware of the new master. In our example, this would involve restarting the other replicas with --link redis-replica:master
.
With Docker Compose the master/replica mode can be setup using:
1 | version: '2' |
The above command scales up the number of replicas to 3. You can scale down in the same way.
Note: You should not scale up/down the number of master nodes. Always have only one master node running.
Securing Redis™ traffic
Starting with version 6, Redis™ adds the support for SSL/TLS connections. Should you desire to enable this optional feature, you may use the following environment variables to configure the application:
-
REDIS_TLS_ENABLED
: Whether to enable TLS for traffic or not. Defaults to no. -
REDIS_TLS_PORT
: Port used for TLS secure traffic. Defaults to6379
. -
REDIS_TLS_CERT_FILE
: File containing the certificate file for the TSL traffic. No defaults. -
REDIS_TLS_KEY_FILE
: File containing the key for certificate. No defaults. -
REDIS_TLS_CA_FILE
: File containing the CA of the certificate. No defaults. -
REDIS_TLS_DH_PARAMS_FILE
: File containing DH params (in order to support DH based ciphers). No defaults. -
REDIS_TLS_AUTH_CLIENTS
: Whether to require clients to authenticate or not. Defaults to yes.
When enabling TLS, conventional standard traffic is disabled by default. However this new feature is not mutually exclusive, which means it is possible to listen to both TLS and non-TLS connection simultaneously. To enable non-TLS traffic, set REDIS_TLS_PORT
to another port different than 0.
Using docker run
1 | docker run --name redis \ |
Modifying the docker-compose.yml
file present in this repository:
1 | services: |
Alternatively, you may also provide with this configuration in your custom configuration file.
Configuration file
The image looks for configurations in /opt/bitnami/redis/mounted-etc/redis.conf
. You can overwrite the redis.conf
file using your own custom configuration file.
1 | docker run --name redis \ |
Alternatively, modify the docker-compose.yml
file present in this repository:
1 | services: |
Refer to the Redis™ configuration - http://redis.io/topics/config manual for the complete list of configuration options.
FAQs
Cant open the append only file - Permission Denied
Non-root container images add an extra layer of security and are generally recommended for production environments. However, because they run as a non-root user, privileged tasks are typically off-limits.
As this is a non-root container, the mounted files and directories must have the proper permissions for the UID 1001
.
1 | chown -R 1001:1001 ./runtime/bitnami/redis/data |
References
[2] bitnami/bitnami-docker-redis-cluster - https://github.com/bitnami/bitnami-docker-redis-cluster
[6] Redis - Official Image | Docker Hub - https://hub.docker.com/_/redis
[7] bitnami/redis - Docker Image | Docker Hub - https://hub.docker.com/r/bitnami/redis/
[8] Empowering App Development for Developers | Docker - https://www.docker.com/
[9] Overview of Docker Compose | Docker Documentation - https://docs.docker.com/compose/