Loading...

How to use Redis with Docker

Jonas Scholz
3 min read 27. Jan. 2025

Using Redis with Docker is a great way to get a fast and efficient database up and running for your applications. Let's walk through how to set this up step by step.

First off, you'll need Docker installed on your computer. If you don't have it yet, go ahead and download Docker Desktop from the Docker website. It's pretty straightforward to install, and once it's set up, you'll be ready to start working with Docker containers.

Now, let's pull the Redis Docker image. This image is what we'll use to create our Redis container. Open up your terminal or command prompt and type in this command:

docker pull redis

This command fetches the latest version of the Redis image from Docker Hub. Once it's done, you can check that the image is there by opening Docker Desktop and looking under the "Images" tab. You should see the Redis image listed, and it should be around 140 MB in size.

Next, let's start a Redis instance. This is like starting a server that your application can connect to. In your terminal, run this command:

docker run --name some-redis -d redis

This command creates a new container named "some-redis" and starts it in detached mode, which means it runs in the background. The Redis server will start up automatically and be ready to use.

Now, let's talk about persistent storage. If you want your data to stick around even if you stop and start your Redis container, you'll need to set up persistence. First, stop the container we just started (you can do this by running docker stop some-redis and then docker rm some-redis). Then, start a new container with persistence enabled like this:

docker run --name some-redis -d redis redis-server --save 60 1 --loglevel warning

This command tells Redis to take a snapshot of the data every 60 seconds. The data will be saved in a volume at the /data location inside the container.

To interact with your Redis server, you'll want to use the Redis CLI. First, create a Docker network to connect your containers:

docker network create some-network

Then, run the Redis CLI in a new container that's connected to the same network as your Redis server:

docker run -it --network some-network --rm redis redis-cli -h some-redis

This will open up the Redis CLI, where you can run commands to manage your Redis database.

If you want to customize your Redis setup, you can use a configuration file. You can do this by creating a redis.conf file on your local machine and then mounting it into your Redis container. First, create a directory on your host machine, let's call it /myredis/config, and put your redis.conf file in there. Then, make sure Docker Desktop has access to this directory by going into the Docker Desktop settings and adding /myredis/config to the list of shared directories.

Once that's set up, you can start your Redis container with the custom configuration like this:

docker run -v /myredis/config:/usr/local/etc/redis --name myredis redis redis-server /usr/local/etc/redis/redis.conf

This command mounts the /myredis/config directory from your host machine into the container at /usr/local/etc/redis and starts the Redis server with your custom configuration.

Finally, if you want to extend Redis with additional modules, you can use the redislabs/redismod image from Docker Hub. This image includes several Redis modules that add extra functionality. To start a container with these modules, just run:

docker run -d -p 6379:6379 redislabs/redismod

And that's it! You now have a Redis database running in a Docker container, with options for persistence, custom configurations, and additional modules. Give it a try and see how it works with your applications.

If you want to make it easier, you can also use Docker Compose to set up Redis. You can read more about that here.

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!