01: Redis server & cli on Docker tutorial step by step

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.

2. Run the redis server in Docker container

Docker will start your container in detached mode.

or a similar command is:

Outputs:

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.

The container id is 5a34d5557b8d

Now sh into the container:

Use Redis cli by connecting to the Redis server

4. Using Redis in the Redis shell (redis-cli)

Set and get keys:

Incrementing a key:

Set & get multiple keys:

Deleting a key:

Checking if a key exists (returns 1 if true, 0 if false):

Expiring a key:

Using 5 seconds and checking expiration (i.e. ttl) returning seconds left before expiration

5. Stop docker compose

Firstly, type “exit” to exit out of Redis client:

and type “exit” again to exit out of the container.

Finally, bring down the docker container with docker-compose down:

The docker ps will show that no containers are running.


300+ Java Interview FAQs

Tutorials on Java & Big Data