How to run Redis 6 with Docker and Docker-Compose

Pinterest LinkedIn Tumblr

In this guide we are going to explore how to run Redis 6 locally with docker and docker compose. This can be helpful if you want to run Redis locally without installing it in your server or if you want to run multiple versions of Redis seamlessly.

Also check:


To follow along, ensure you have the following:

  1. Docker installed locally and permissions to use it to launch containers
  2. Docker compose is installed locally
  3. Basic knowledge of the command line

Table of content

  1. Using the docker run command
  2. Using the docker-compose tool

1. Using the docker run command

We are going to use the docker run command to achieve our goal. The version of Redis that we want is redis:6.2-alpine – a minimal version of Redis container.

❯ docker run -d \
    --name my-redis \
    -p 6379:6379 \
    redis:6.2-alpine \
    redis-server --save 60 1 --requirepass MDNcVb924a --loglevel warning
Unable to find image 'redis:6.2-alpine' locally
6.2-alpine: Pulling from library/redis
a0d0a0d46f8b: Already exists
a04b0375051e: Pull complete
cdc2bb0f9590: Pull complete
0aa2a8e7bd65: Pull complete
f64034a16b58: Pull complete
7b9178a22893: Pull complete
Digest: sha256:58132ff3162cf9ecc8e2042c77b2ec46f6024c35e83bda3cabde76437406f8ac
Status: Downloaded newer image for redis:6.2-alpine

In the above command:

  • The -d instructs docker container to run as a detached process. It run container in background and print container ID
  • -p is for port mapping. We are instructing the container to expose the container port externally. Container port 6379 is mapped to host port 6379. That means the service can be accessed through localhost:6379
  • The redis:6.2-alpine defines the image that we are running. We are running redis image version 6.2 – alpine image (a minimal version).
  • The remaining commands (redis-server --save 60 1 --requirepass MDNcVb924a --loglevel warning) defines the command to execute when the container starts. In our case, we are defining the default command with the redis-server command passing arguments to save data every 60 seconds and specifying a password for our redis.

To check that our container is running as expected, use the docker ps command:

➜ docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
57ba51f4a236   redis:6.2-alpine   "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes>6379/tcp, :::6379->6379/tcp              my-redis

In my case the container is running as my-redis the name we gave it. We can login to the container using the docker exec command while executing /bin/bash interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.

❯ docker exec -it my-redis /bin/sh
/data # redis-cli> auth MDNcVb924a
OK> keys *
(empty array)>

If you need to clean up the container when not in use, you can stop and remove the container using this command:

docker stop my-redis

# Removing
docker rm my-redis

2. Using the docker-compose tool

We can achieve the same functionality with docker-compose. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

Docker Compose allows you to define the service (Redis in our case) with properties like the image to use, command to overide the default Dockerfile command and andports to expose.

Here is how we would use docker-compose to achieve the functionality above. Save this as docker-compose.yaml:

version: '3.9'

    image: redis:6.2-alpine
      - 6379:6379
    command: redis-server --save 60 1 --requirepass MDNcVb924a --loglevel warning

Now bring up the containers:

➜ docker-compose up -d
Creating network "red_default" with the default driver
Creating red_redis_1 ... done

The commands:

  • up brings up the container
  • -d in a detached mode

Verify the container processes using the ps command:

➜ docker-compose ps
   Name                  Command               State                    Ports
red_redis_1 redis ...   Up>6379/tcp,:::6379->6379/tcp

To login to the container and login to redis, use this:

❯ docker-compose exec redis /bin/sh
/data # redis-cli> auth MDNcVb924a
OK> keys *
(empty array)>


In this guide we managed to run Redis 6 as a container in our system, we explored using the docker run command while passing the required arguments an alternative approach of simplifying the process with docker-compose

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment