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
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.
|
|
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 SuperSecretPass1 --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:
|
|
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.
|
|
If you need to clean up the container when not in use, you can stop and remove the container using this command:
|
|
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
:
|
|
Now bring up the containers:
|
|
The commands:
up
brings up the container-d
in a detached mode
Verify the container processes using the ps command:
|
|
To login to the container and login to redis, use this:
|
|
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