To interact with specific Redis databases within a Docker environment, follow these steps to connect directly using containerized tools.
Begin by launching a Redis instance in a detached container:
docker run -d --name redis-instance redis
Once the container is runing, access its shell using the exec comand:
docker exec -it redis-instance sh
Within the container shell, use the redis-cli utility with the -n flag to select a database. Redis supports 16 databases (indexed from 0 to 15). To switch to database number 3, for example:
redis-cli -n 3
This command connects to the specified database, enabling operations such as setting or retrieving keys in that context.
For verification, you can check the current database index using:
redis-cli INFO | grep "db0"
This workflow allows seamless navigation between Redis databases without modifying configuration files or restarting services.