Redis in Docker Compose
Running Redis using Docker makes it effortless, and it's even easier to run and manage Redis when using Docker Compose.
Follow these steps to run Redis with Docker Compose:
- Add the service below to a Compose file:
docker-compose.yml
services:
redis:
image: redis:7-alpine
volumes:
- ./mounted_data/redis:/data
ports:
- "127.0.0.1:6379:6379"
restart: always
- Create the directory on the host where Redis data will be retained
mkdir -p mounted_data/redis
- Start the service
docker compose up -d redis
- Use the following connection string to connect to the database:
redis://localhost:6379
(If you're connecting from another container in the same Compose file, use redis as the hostname instead of localhost)
tip
The service is exposed only to the host machine, so you'll need to SSH into the host to connect from outside the host machine. For example, when using RedisInsight, etc.