How to Run Redis with Docker Compose Tutorial (Step by Step)

How to Run Redis with Docker Compose. Relational databases are used in most IT projects. Some specific project requires to use NoSQL databases.  One of the most popular in memory data storage systems on the market is Redis that stands for Remote Dictionary Server.

What is Redis Server

Redis is a free and open source (BSD license) server that is both a memory and key value store.

 

  • The memory database stores all data directly in the memory allowing very fast access times, even for large amounts of unstructured data. 
  • Key value store are databases that offer high performance and are easy to scale due to their simple structure.

Redis server stores data in the memory and to limit the risk of losing all the data Redis copies all the data regularly to a backup hard drive or save all the commands needed for reconstruction in a log file.

These data structures include strings, hashes, lists, sets and sorted sets with range queries as well as Hyperloglogs, bitmaps and geographic indexes.

Streaming is also supported. For high availability, Redis enables Redis Sentinel and automated partitioning with Redis Cluster and provides replication that is built in, Lua scripting, LRU eviction, transactions and different levels of persistence on disk.

What is Docker Compose?

Docker Compose is a tool that allows you to centrally manage the deployments of many different Docker containers.

 

Docker container is a unit of software that packages up code and all its dependencies to allow the application to run quickly and reliably from one computing environment to another. For launching multiple Docker containers, a YAML file is used by Compose to initialize services for the app. Creating and launching all of the functions is as simple as issuing a single command when configuration is complete.

How to Run Redis with Docker Compose , how to construct a Docker container and how to deal with different issues that may arise, are all covered in this article.

How to Run Redis with Docker Compose

Prerequisites

 In order for the Redis Container to work, Docker has to be set up on the host system or server. The  “docker -v” command may be used to verify the current Docker engine version installed.

Step 1 — Use Docker pull to download the Redis image

We will pursue the recommended way which is to use the Docker pull command to obtain the prebuilt Redis Docker image from the Docker Hub Registry.

We will download the image by using the command below:

				
					$ docker pull bitnami/redis:latest

				
			

Step 2 — Persisting your database

Upon removal of the container all of your stored data can be lost, leading to recreation of the database every time the image is run. Mount a volume that will survive even after the container is removed to prevent losing data.

To achieve persistence a directory will be mounted at the /bitnami path.

Do this by proceeding to make changes to the docker-compose.yml file present in the linked repository, in the following way:

				
					services:
  redis:
  ...
    volumes:
      - /path/to/redis-persistence:/bitnami/redis/data
  ...

				
			

Step 3 — Use Docker Compose

By default, if a network name is not provided, a new network is created by Docker compose which connects all deployed services to it. However, a new bridge network called app-network” will be expressly defined. If you wish to connect to the Redis(TM) server from your own custom application image, use the service name “myapplication” in the following code snippet.

				
					version: '2'

networks:
  app-network:
    driver: bridge

services:
  redis:
    image: 'bitnami/redis:latest'
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    networks:
      - app-network
  myapplication:
    image: 'YOUR_APPLICATION_IMAGE'
    networks:
      - app-tier

				
			

Next, we will launch the containers using:

				
					$ docker-compose up -d

				
			

Step 4 — Disable Redis(TM) commands

Some commands may need to be disabled for security concerns. The following environment variable may be used to initialise them.

Proceed to make changes to the docker-compose.yml file present in the linked repository, as show below:

				
					services:
  redis:
  ...
    environment:
      - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL,CONFIG
  ...

				
			

Step 5 — Pass extra command-line flags

The run.sh script may be used to provide additional command-line parameters to the redis service command.

Proceed to make changes to the  docker-compose.yml  file present in the linked repository as show below:

				
					services:
  redis:
  ...
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    command: /opt/bitnami/scripts/redis/run.sh --maxmemory 100mb
  ...

				
			

Step 6 -​​Setup server password on first run

The Redis server password will be set to the value of REDIS_PASSWORD (or the content of the file provided in REDIS_PASSWORD_FILE) if the REDIS_PASSWORD environment variable is passed when executing the image for the first time

Proceed to make changes to the docker-compose.yml file present in the linked repository as shown below:

				
					services:
  redis:
  ...
    environment:
      - REDIS_PASSWORD=password123
  ...

				
			

Step 7 — Allow empty passwords

How to Run Redis with Docker ComposeIf you want to use the Redis(TM) image, you must set all of the passwords. By setting the environment variable, ALLOW_EMPTY_PASSWORD=yes, empty passwords will be enabled and this feature will then work..

Proceed to make changes to the docker-compose.yml file present in the linked repository, as shown below:

				
					services:
  redis:
  ...
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
  ...

				
			

Step 8 — Disable AOF persistence

Redis(TM) has a variety of persistence options. The AOF (Append Only File) method is used by default in this picture. Should you need to change this behaviour, setting By setting  REDIS_AOF_ENABLED=no  this feature of this environment variable can be disabled in order to modify the behavior mentioned earlier.

Proceed to make changes to the docker-compose.yml file present in the linked repository, as shown below:

				
					services:
  redis:
  ...
    environment:
      - REDIS_AOF_ENABLED=no
  ...

				
			

Step 9 — Set up a standalone instance

On port 6379, Redis(TM) is launched by default in this image in standalone mode. Setting the  REDIS_PORT_NUMBER environment variable will update the port number if need be.

Proceed to make changes to the docker-compose.yml file present in the linked repository, as shown below:

				
					services:
  redis:
  ...
    environment:
      - REDIS_PORT_NUMBER=7000
    ...
    ports:
      - '7000:7000'
  ....

				
			

Step 10 — Create the replication master

First we need to initialise the Redis master by using following commands:

				
					$ docker run --name redis-master \
  -e REDIS_REPLICATION_MODE=master \
  -e REDIS_PASSWORD=masterpassword123 \
  bitnami/redis:latest

				
			

Step 11 — Create the masternode

With Docker Compose the master node can be setup using:

				
					version: '2'

services:
  redis-master:
    image: 'bitnami/redis:latest'
    ports:
      - '6379'
    environment:
      - REDIS_REPLICATION_MODE=master
      - REDIS_PASSWORD=my_master_password
    volumes:
      - '/path/to/redis-persistence:/bitnami'

  redis-replica:
    image: 'bitnami/redis:latest'
    ports:
      - '6379'
    depends_on:
      - redis-master
    environment:
      - REDIS_REPLICATION_MODE=slave
      - REDIS_MASTER_HOST=redis-master
      - REDIS_MASTER_PORT_NUMBER=6379
      - REDIS_MASTER_PASSWORD=my_master_password
      - REDIS_PASSWORD=my_replica_password

				
			

Scale the number of replicas up by 3 using the following command:

				
					$ docker-compose up --detach --scale redis-master=1 --scale redis-secondary=3

				
			

You can use the command above to scale down as well. Although make note that master node shouldnt be scaled up or down as at a time only one master node should be running.

Step 12 — Secure Redis(TM) traffic

As soon as you switch on TLS, all non-TLS communication is disabled. Non-TLS traffic may be enabled by changing the value of REDIS_TLS PORT to a port other than 0.

Proceed to make changes to  the docker-compose.yml file present in the linked repository, as shown below:

				
					 services:
   redis:
   ...
     environment:
       ...
       - REDIS_TLS_ENABLED=yes
       - REDIS_TLS_CERT_FILE=/opt/bitnami/redis/certs/redis.crt
       - REDIS_TLS_KEY_FILE=/opt/bitnami/redis/certs/redis.key
       - REDIS_TLS_CA_FILE=/opt/bitnami/redis/certs/redisCA.crt
     ...
     volumes:
       - /path/to/certs:/opt/bitnami/redis/certs
       - /path/to/redis-persistence:/bitnami/redis/data

				
			

Step 13 — Configuration file

Proceed to make changes to the docker-compose.yml file present in the linked repository, as shown below:

				
					services:
  redis:
  ...
    volumes:
      - /path/to/your_redis.conf:/opt/bitnami/redis/mounted-etc/redis.conf
      - /path/to/redis-persistence:/bitnami/redis/data
  ..

				
			

Step 14 — Logging

To view the logs on stdout that Redis Docker image sends our container run the following command:

				
					$ docker-compose logs redis

				
			

Step 15 — Maintenance

The last step of how to Run Redis with Docker Compose is maintenance. Bitnami provides up to date versions of Redis(TM), including security patches, soon after they are made upstream. We recommend that you follow these steps to upgrade your container.

1) To obtain the updated image, run the following command:

				
					$ docker pull bitnami/redis:latest

				
			

2) Next we need to stop the container:

We will do the stopping by running the command below:

				
					$ docker-compose stop redis

				
			

3) Now we will proceed to remove the container that is currently running by using the following command:

				
					$ docker-compose rm -v redis

				
			

4) Now it’s time to run the new image by deploying the following command:

				
					$ docker-compose up redis

				
			

How to Run Redis with Docker Compose Conclusion

This article demonstrated how to use Docker Compose to run Redis. Error handling, stopping the container, running Redis using Compose and creating several additional files were also showed in this tutorial.

We hope this article was informative and provided a step by step guide which was easy to follow and which you were able to successfully implement. Thank you for reading through!

Avatar for Emad Bin Abid
Emad Bin Abid

I'm a software engineer who has a bright vision and a strong interest in designing and engineering software solutions. I readily understand that in today's agile world the development process has to be rapid, reusable, and scalable; hence it is extremely important to develop solutions that are well-designed and embody a well-thought-of architecture as the baseline. Apart from designing and developing business solutions, I'm a content writer who loves to document technical learnings and experiences so that peers in the same industry can also benefit from them.

3.3 3 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x