This tutorial talks about setup and configuring Redis in Docker images.

  • Redis docker install
  • Redis Docker Configuration setup
  • docker volume
  • Access Redis docker remotely

Docker is a virtualization OS framework that allows you to ship any software as a container.

You can install redis locally with Docker Container using the below approaches.

  • Docker commands: Commands used to execute and operates on a single container
  • Docker-compose: It is used to manage multiple containers at once.

Redis on Docker Container

In this section, How to run the Redis server on the docker image container, and start the server.

First check, whether the Docker service is installed and running or not

docker --version

Next, install a docker container with pulling redis image.

sudo docker run --name redis-host -d redis  -p 6376:6379
  • It downloads the latest Redis image from the docker hub
  • Create a docker container assigned with name redis-host
  • Run docker container
  • map port on current system 6379 to docker Redis port 6379

Once the container is installed, You can check the container status using the below command.

docker ps

It gives container ID, Image, port, and status information.

Next, Connect to the Redis server from the docker container.

docker exec -it redis-host sh or
docker exec -it container_ID sh
#

This connects to a shell terminal inside a docker container and opens the terminal and is ready to accept redis-cli commands

#redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set id 12
OK
127.0.0.1:6379> get id
"12"

How to access the Redis docker server from outside?

Clients communicate to Redis docker container if is enabled remote access in the container.

So, specify the port number while creating the docker container

sudo docker run --name redis-host -p 6451:6379 -d redis

Once the port number is set, you can access the redis server via the command used below.

redis-cli -h hostname -p 6451 - a password

How to save a docker Redis container?

Redis is cached data, data lost when the container restarted. So you have to persist the data to the machine docker container is hosted.

Use -v option, while creating a docker container.

sudo docker run --name redis-host -d redis  -p 6376:6379 -v redis_data:/data redis

docker run –name redis -d
-v redis-data:/data -v ./redis-conf:/usr/local/etc/redis redis:6.0 /usr/local/etc/redis.conf

How to start, stop Redis docker container

Following are commands for start, stop, and restart the docker Redis container.

docker stop redis-host

docker stop redis-host
docker restart Redis-host

Another docker command, monitor to monitor docker container.

Docker compose Install redis

let’s create a docker compile yaml file

redis-docker-compose.yaml

This contains the following things.

  • Configure image cache services with Redis and version 6.2. It pulls from docker hub

  • restarts parameter allows you to restart the server if any failure or errors are thrown.

  • 6379:6379 port allows you to map the container port with hosted machine port

  • Once the container is created and started, the Command parameter allows you to run.

  • volumes configure a folder to persist RDB data of Redis server that mounted to cache volume

version: '6.2'
services:
  cache:
    image: redis:6.2
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --loglevel warning
    volumes:
      - cache:/data
volumes:
  cache:
    driver: local

Next, run the docker container with the above compose file.

docker-compose -f redis-docker-compose.yaml up

It starts the container and volume is created and mounted to save the cache data during restarts of the server.