This extends Docker Tutorial – client & server with docker-compose.yml. It assumes that you have a Docker desktop is running in your operating system.
1. Redis Docker compose file without password auth
Here is the docker-compose.yml file created under ~/projects folder. The server starts without a password for demonstration purpose only.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
version: '3.8' services: cache: image: redis:6.2-alpine restart: always ports: - '6379:6379' command: redis-server --appendonly yes volumes: - cache:/data volumes: cache: driver: local |
2. Run the redis server in Docker container
Docker will start your container in detached mode.
|
1 2 3 |
~/projects]$ docker-compose up --detach |
or a similar command is:
|
1 2 3 |
~/projects]$ docker compose -f docker-compose.yml up --detach |
Outputs:
|
1 2 3 4 5 |
[+] Running 2/2 ✔ Network projects_default Created 0.1s ✔ Container projects-cache-1 Started |
3. Get container id & sh into it
Use docker ps to get the container id. You run docker compose ps to see all docker compose services that are running.
|
1 2 3 4 5 |
~/projects]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a34d5557b8d redis:6.2-alpine "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp projects-cache-1 |
The container id is 5a34d5557b8d
Now sh into the container:
|
1 2 3 4 |
~/projects]$ docker exec -it 5a34d5557b8d sh /data # |
Use Redis cli by connecting to the Redis server
|
1 2 3 4 |
/data # redis-cli -h localhost -p 6379 localhost:6379> |
4. Using Redis in the Redis shell (redis-cli)
Set and get keys:
|
1 2 3 4 5 6 7 |
localhost:6379> set some_key some_value OK localhost:6379> get some_key "some_value" localhost:6379> |
Incrementing a key:
|
1 2 3 4 5 6 7 8 9 10 11 |
localhost:6379> set counter 10 OK localhost:6379> incr counter (integer) 11 localhost:6379> incr counter (integer) 12 localhost:6379> incrby counter 5 (integer) 17 localhost:6379> |
Set & get multiple keys:
|
1 2 3 4 5 6 7 8 |
localhost:6379> mset a 5 b 9 OK localhost:6379> mget a b 1) "5" 2) "9" localhost:6379> |
Deleting a key:
|
1 2 3 4 5 6 7 8 9 |
localhost:6379> set x 5 OK localhost:6379> del x (integer) 1 localhost:6379> get x (nil) localhost:6379> |
Checking if a key exists (returns 1 if true, 0 if false):
|
1 2 3 4 5 6 |
localhost:6379> exists x (integer) 0 localhost:6379> exists a (integer) 1 |
Expiring a key:
Using 5 seconds and checking expiration (i.e. ttl) returning seconds left before expiration
|
1 2 3 4 5 6 7 8 9 10 11 |
localhost:6379> expire a 5 (integer) 1 .... wait for 5 seconds localhost:6379> get a (nil) localhost:6379> |
5. Stop docker compose
Firstly, type “exit” to exit out of Redis client:
|
1 2 3 4 |
localhost:6379> exit /data # |
and type “exit” again to exit out of the container.
|
1 2 3 |
/data # exit |
Finally, bring down the docker container with docker-compose down:
|
1 2 3 4 5 6 |
~/projects]$ docker-compose down [+] Running 2/2 ✔ Container projects-cache-1 Removed 0.2s ✔ Network projects_default Removed |
The docker ps will show that no containers are running.
|
1 2 3 4 |
:~/projects]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |