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:
- Running Postgresql 14 with Docker and Docker-Compose
- How to run MsSQL Server 2019 with Docker and Docker-Compose
- How to run Mysql 8 with Docker and Docker-Compose
- How to run Cassandra 4 with Docker and Docker-Compose
Prerequisites
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- Basic knowledge of the command line
Table of content
- Using the
docker run
command - 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
57ba51f4a23615ebde5f3063b04296021c3d43a78a45658b4a020af161d93c96
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 port6379
is mapped to host port6379
. That means the service can be accessed throughlocalhost: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 0.0.0.0:6379->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
127.0.0.1:6379> auth MDNcVb924a
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
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'
services:
redis:
image: redis:6.2-alpine
ports:
- 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 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->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
127.0.0.1:6379> auth MDNcVb924a
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
Conclusion
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