opened image

    In this article, we will look at how to deploy Redis in Docker and in the Docker Compose file.

    Redis is a high-performance database management system used for storing and processing data in memory. Redis allows for efficient processing of large volumes of data and provides high availability and fault tolerance.

    Installing Docker and Docker Compose

    Before we begin, you need to install Docker and Docker Compose. You can find installation instructions for Docker for your operating system on our blog for Centos 7 or for Ubuntu 20.04.

    Installing Docker Compose


    curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

    Setting execute permissions. 



    chmod +x /usr/local/bin/docker-compose


    Checking the installation of Docker Compose:




    docker-compose --version



    Creating Docker Compose file

    To deploy Redis using Docker Compose, we need to create a file docker-compose.yaml in the root directory of our project. It will contain the settings for the Redis container.

    To keep track of what we have installed in the future, let's create a separate folder for this project in the directory /home and navigate to it.




    mkdir /home/redis && cd /home/redis


    You can also use another directory  to place this and other projects. 

    Let's use the repository to create the file docker-compose.yaml at the link

    Create a *.yaml file for Docker Compose.





    version: '3.3'
        image: redis:latest
        restart: always
          - "6379:6379"
          - /path/to/local/dаta:/root/redis
          - /path/to/local/redis.conf:/usr/local/etc/redis/redis.conf
          - REDIS_PASSWORD=my-password
          - REDIS_PORT=6379
          - REDIS_DATABASES=16


    Let's consider each parameter:

    version - this is the version of the Docker Compose file format. We are using version 3.3.
    services - this is the section where we define the list of containers and their settings. In our case, we define one service - Redis.
    redis - this is the name of our service.
    image - this is the name of the Redis image that we will use to create our container.
    restart - this is a setting that tells Docker to restart the Redis container in case of its stoppage or failure.
    ports - this is a setting that defines which ports of the container should be accessible on the host system. In this case, we are forwarding port 6379 of the Redis container to port 6379 of the host system.
    volumes - this is a setting that defines which directories on the host system should be accessible inside the container. In this case, we are mounting directories with Redis data and configuration files.
    environment - this is a setting that allows us to define environment variables for the Redis container. In this case, we define the password, port, and number of Redis databases.
    REDIS_PASSWORD: my-password defines the REDIS_PASSWORD variable with the value my-password.
    REDIS_PORT: 6379 defines the Redis port that will be used in the container. By default, Redis uses port 6379, so this variable can be used to change the port to another if necessary.
    REDIS_DATABASES: 16 defines the number of Redis databases that will be available in the container. By default, Redis supports 16 databases, but this parameter can be changed using this variable.




    Running Redis

    Now that we have the docker-compose.yaml file, we can start the Redis container using the command:




    docker-compose up -d


    This command starts the Redis container in the background (option -d). Docker Compose will automatically download and create the Redis container based on the settings specified in the docker-compose.yaml file.

    You can check if the Redis container is running by running the command:


    docker ps | grep redis

    We can start the Redis container without using the docker-compose.yaml file using the following Docker command:






    docker run -d --name my-redis-container -p 6379:6379 -v /path/to/local/dаta:/root/redis -v /path/to/local/redis.conf:/usr/local/etc/redis/redis.conf -e REDIS_PASSWORD=my-password -e REDIS_PORT=6379 -e REDIS_DATABASES=16 redis:latest




    Connecting to Redis


    You can connect to the Redis container using any Redis client. For this, you will need the IP address of your host system and the port that you defined in the docker-compose.yaml file.

    For example, if you are using the standard Redis client, you can connect to the Redis container by running the command:




    redis-cli -h -p 6379


    Where is the IP address of the Docker container on which Redis is running. To find it, use the command docker ps | grep redis to find the ID of our container or use its name.




    docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis_redis_1


    Where redis_redis_1 is the name of the container whose IP address needs to be filtered.

    This command uses the --format flag to format the output, in which the range function is used to iterate over all the container's networks and only the IP address of each network is output. If the container has multiple networks, all their IP addresses will be output. If the container is only connected to one network, only its IP address will be output.

    This command  redis-cli -h -p 6379 - connects to Redis running on the local machine and listening on port 6379.

    To connect from CMS systems, use built-in plugins. 

    So, in this article, we have looked at how to deploy Redis in Docker and Docker Compose. We have created the docker-compose.yaml file with settings for the Redis container.