We stand with Ukraine. To make an impact, please consider donating . Click here for more details.
    SIGN IN / UP

    How to Install Redis in Docker and Docker Compose

    In this article, we'll look at how to deploy Redis in Docker and in a Docker Compose file.

    Redis is a high performance database management system that is used to store and process data in memory. Redis allows you to efficiently process large amounts of data and provides high availability and fault tolerance.

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

     

    Install Docker Compose
    curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin /docker-compose
    


    Set permissions to launch.

     

     

     

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

     


    Check how Docker-Compose was installed:

     

     

     

     

    docker-compose --version
    

     

     

    Creating a Docker Compose File


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

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

     

     

     

     

     

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

     



    You can also use a different directory to host this and other projects.

    Let's use the repository to create the docker-compose.yaml file at https://hub.docker.com/_/redis

    Let's create a *.yaml file for Docker-Compose.

     

     

     

     

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

     




    Let's go over each of the settings in this file:

    image - this is the Redis Docker image that we want to use. In our case, we are using the official Redis image from Docker Hub.
    ports - this setting defines which ports of the container should be available on the host system. In our case, we are forwarding port 6379 from the Redis container to port 6379 on the host system.
    volumes - this setting defines which directories on the host system should be available inside the container. In our case, we are mounting directories for Redis data and configuration files.
    environment - this setting allows us to define environment variables for the Redis container. In our case, we are defining the Redis password, port, and number of databases.
    REDIS_PASSWORD: my-password defines a REDIS_PASSWORD variable with the value my-password.
    REDIS_PORT: 6379 defines the Redis port to be used in the container. By default, Redis uses port 6379, so this variable can be used to change the port to something else if necessary.
    REDIS_DATABASES: 16 specifies the number of Redis databases that will be available in the container. Redis supports 16 databases by default, but this setting can be changed with this variable.

     

     

     

    Running Redis

     

     

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

     

    docker-compose up -d
    



    This command starts a Redis container in the background (option -d). Docker Compose will automatically download and create a 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 a Redis container without using the docker-compose.yaml file with 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 a Redis container using any Redis client. To do this, you will need your host system's IP address and port, which 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 172.27.0.2 -p 6379
    

     




    Where 172.27.0.2 is the IP of the docker container running Redis. To find out, docker ps | grep redis to find out the ID of our container or use its name.

     

     

     

     

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

     



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

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

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