How to Deploy and Run Redis in a Docker Container


Ensure that Docker is installed in your system. Follow Docker's installation guide if you haven’t installed it yet

Step 1: Run the Redis container#

Execute the following command to run Redis container in the background in a “detached” mode.

$ docker run -d --name my-redis-stack -p 6379:6379  redis/redis-stack-server:latest

where my-redis-stack is the name of Docker container -d representis running Redis in a background in a “detached” mode. redis/redis-stack-server is the name of Docker image that it fetches from Docker Hub.

Step 2: Verify that the Redis container is running#

$ docker ps
CONTAINER ID   IMAGE                                        COMMAND          STATUS           PORTS                     NAMES
885568fdf77e     redis/redis-stack-server:latest   "/"    Up 3 minutes>6379/tcp   my-redis-stack

Step 3: Connect to your database#

The following uses the first three alphanumeric characters of your Container ID and opens up sh shell of the Redis Docker container:

$ docker exec -it 885 sh
> redis-cli

Step 4: Testing Redis container​#

Execute the following command to test the Redis server:>ping

Step 5: Running Redis container with Persistent Storage#

In order to enable persistence, you should invoke the Docker container, passing appendonly yes option as shown below:

$ docker run -d --name my-redis-stack -p 6379:6379 -v /Users/my-redis/:/data -e REDIS_ARGS="--requirepass your_password_here --appendonly yes" redis/redis-stack-server:latest

Check ls -ld /Users/my-redis/ on your host machine to view the directory permissions.

So If persistence is enabled, data is stored in the volume /data, which can be used with --volumes-from some-volume-container or -v /docker/host/dir:/data