This tutorial requires the following software:
- Docker Engine: [https://docs.docker.com/install/]
- Docker Compose: [https://docs.docker.com/compose/install/]
Please ensure you have these components installed before proceeding. Installation instructions are available at the provided links.
Enjoy your Redis exploration!
1. Start Redis container with Docker Compose:
Create a file named compose.yaml
with the following content:
services:
redis:
image: redis:alpine
ports:
- "6379:6379"
2. Connect to the container and Redis:
Option 1: Use Docker exec
- Run the following command to open a shell inside the Redis container:
docker compose exec redis ash
- Inside the container, you can directly run Redis commands.
Option 2: Use Redis CLI client
- Install the Redis CLI client on your system.
- Connect to the running container using:
redis-cli -h localhost -p 6379
3. Run basic Redis commands:
INFO: Get information about the server:
INFO
MONITOR: Monitor all commands sent to the server:
MONITOR
SET/GET: Set and get a key-value pair:
SET mykey myvalue
GET mykey
Other commands:
- DEL: Delete a key:
DEL mykey
- KEYS: List all keys:
KEYS *
- LPUSH/RPUSH: Push elements to a list:
LPUSH mylist element1 element2
RPUSH mylist element3 element4
- LRANGE: Get elements from a list:
LRANGE mylist 0 2
(retrieves elements 0 to 2) - SADD/SMEMBERS: Add/Check members in a set:
SADD myset member1 member2
SMEMBERS myset
4. Exit and Stop the container:
Option 1:
- Exit the Redis CLI with
QUIT
. - Exit the container shell with
exit
.
Option 2:
- Exit the Redis CLI with
QUIT
.
Stop the container:
docker compose down
Tips:
- Explore the official Redis documentation for more advanced commands and features: https://redis.io/commands
- Remember that packages available in the Alpine image might differ slightly from the standard image. Refer to the specific documentation for details.
1. Start the Redis container:
Ensure your existing compose.yaml
file, with the Redis container definition using redis:alpine
, is present (see the first part above). Start the container using:
docker compose up -d
2. Modify existing compose.yaml
:
Add another service for the Ubuntu container in the same compose.yaml
file:
# Existing Redis service definition
services:
# ...
ubuntu:
image: ubuntu:latest
depends_on:
- redis # Ensures Redis starts first
# Other changes if needed for your network configuration
3. Connect to the Ubuntu container:
Run the following command to connect to the running Ubuntu container:
docker compose exec ubuntu bash
4. Install the Redis CLI client:
Inside the Ubuntu container, install the Redis CLI client:
apt update && apt install redis-tools
5. Connect to Redis:
Use redis-cli
to connect to the Redis container by referencing its hostname within the same network:
redis-cli -h redis -p 6379
6. Test Redis:
Execute basic Redis commands to verify the connection:
SET example_key example_value
GET example_key
7. Exit and Stop containers:
Exit the Redis CLI with QUIT
. Exit the Ubuntu container shell with exit
.
Stop both containers:
docker compose down