Using Redis in Docker Containers

Leave your reply

Introduction

Learn how to deploy Redis in a Docker container. Redis is a key-value store which allows data to be stored and accessed at lightning fast speeds. These qualities make it a natural choice for use with Docker containers. Redis is one of the top three most popular Docker images.

Requirements

  • A Cloud Server running Linux (Ubuntu 14.04 or CentOS 7)
  • Docker installed and running
  • Familiarity with basic Docker functionality and commands

For more information on installing and using Redis, see our article on Installing Redis on a Linux Server. For more information on installing Docker, see our article on Installing and Running Docker on a Linux Server.

Running Redis in a Docker Container

The official Redis image is one of the top three most popular Docker images. It contains the command EXPOSE 6379 (the default Redis port) which makes it automatically available to any linked containers.

To run a Redis instance in a Docker container named my-redis-container, use the command:

sudo docker run --name my-redis-container -d redis 

Connecting to Redis Running in a Docker Container

To connect to a Redis instance from another Docker container, add --link [Redis container name or ID]:redis to that container's docker run command.

For example, to launch a container named my-redis-application from the official CentOS 7 image and link it to the my-redis-container container, use the command:

sudo docker run --name my-redis-application --link my-redis-container:redis -d centos

To connect to a Redis instance from another Docker container with a command-line interface, link the container and specify the host and port with -h redis -p 6379.

For example:

sudo docker run -it --name my-redis-cli --link my-redis-container:redis --rm redis redis-cli -h redis -p 6379

This will connect you to the new container my-redis-cli with a redis-cli connection to the my-redis-container container. Use ctrl-P + ctrl-Q to exit this container and return to the command line.

Connect to a Redis Container From a Remote Server

If you wish to connect to a Docker container running Redis from a remote server, you can use Docker's port forwarding to access the container with the host server's IP address or domain name.

To use Docker's port forwarding for Redis, add the flag -p [host port]:6379 to the docker run command.

For example, to set up port forwarding so that you can connect to the container using port 7001, the docker run command is:

sudo docker run --name my-redis-container -p 7001:6379 -d redis

You can then switch to another server and access the my-redis-container container with the command:

sudo redis-cli -h [host IP or domain name] -p 7001

For example, if the host server running the Redis container is IP address 192.168.0.1, you can access the Redis container from any server with the command:

sudo redis-cli -h 192.168.0.1 -p 7001

Note: if you are using a firewall, you will need to allow external access to the relevant port(s).

Loading a Custom redis.conf File

If you have a custom redis.conf file, you can load it at container launch by adding the -v flag to the docker run command:

 -v [path to custom redis.conf file]:/usr/local/etc/redis/redis.conf

For example, to load the customized file /data/myredis/redis.conf file, the command is:

sudo docker run --name my-redis-container -v /data/myredis/redis.conf:/usr/local/etc/redis/redis.conf -d redis