Learn how to easily configure a Docker image of Redis to access it from the host machine for local development.

How to setup a Docker image of Redis to access it from the host machine for local development

It happens a lot, that you deploy a project locally and most of the features don't work as they should because some things are missing in your local environment. If your project uses horizontal scaling, it's quite probable that the project uses Redis to handle sessions or related stuff. Redis, which stands for Remote Dictionary Server, is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue.

In this short article, I will show you how to easily deploy an instance of Redis through docker in a few seconds.

Creating a container for development

If what you are looking for is simply a Redis instance that can be used instantly, you may simply run the following command that will create a new container named my-redis and it will be reachable at port 6379 (127.0.0.1:6379):

docker run -d --name my-redis -p 6379:6379 redis

After launching it, you can access it via SSH with the following command:

docker exec -it my-redis bash

And that's it! You can access the Redis CLI running the following command in the SSH instance:

redis-cli

Creating a container with custom redis.conf

As the article mentions, we are using a container for local development, which uses the Redis built-in default configuration, which is in most cases enough for testing and development purposes. In case that you need to customize the configuration file of Redis, you need to follow some extra configuration steps.

If you want to use a custom redis.conf file, you need to have it in a directory of your host machine, in my case, the configuration file will be stored in C:\Users\sdkca\Desktop\redis-conf\redis.conf. The configuration is obviously up to you as you are the one who needs the custom configuration file, however, it's important to know that if you want to access the instance in your local environment, be sure to bind Redis to listen to all the available interfaces in your redis.conf file like this:

# Set the bind parameter in your configuration file
# redis.conf
bind 0.0.0.0

We can build a container that uses the custom configuration file with the following command:

docker run -d -p 6379:6379 --name myredis -v "C:\Users\sdkca\Desktop\redis-conf":/redis-conf redis redis-server /redis-conf

Where the parameters are identified like this:

  • -d: detaches the process and runs it in the background. Otherwise, the process will run on the current terminal.
  • -p 6379:6379: Map port 6379 in the container to port 6379 on the Docker host.
  • --name <my custom redis instance name>: specify the name of the docker container.
  • -v /host/directory:/docker/directory:  sets up a bind-mount volume that links the directory from inside the container to the directory on the host machine.
  • As a final positional argument, we'll provide the path of the linked directory inside the container (/docker/directory).

After launching the container, you will be able to access it through SSH normally:

docker exec -it myredis bash

And access Redis with:

redis-cli

Happy coding ❤️!


Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Become a more social person

Sponsors