Using the redis Docker container locally

To develop applications with the redis database we can use a Docker image.

The documentation is at https://hub.docker.com/_/redis/

Set up the redis container to be accessed via localhost on the local computer

Start the redis container

Start the redis container “detached” ( -d ) in the background and expose it through localhost:6379

  • In the terminal execute
    docker run -dit --name redis -p 127.0.0.1:6379:6379 redis

Check if the container running

  • In the terminal execute
    docker ps -a

Test the redis database

Start a shell session in the redis container to access the command line utility

  • In the terminal execute
    docker exec -it redis sh
  • At the # prompt start the redis cli
    redis-cli
  • At the 127.0.0.1:6379> prompt test the connection
    ping
    The response should be “PONG”
  • Store a value
    set cat 10
    The response should be “OK”
  • Retrieve the value
    get cat
    The response should be “10”
  • Get the list of all keys
    keys *

Set up redis to be used by other Docker containers

Create a network

Create a Docker network, so multiple containers can communicate with each other using it.

  • In the terminal execute
    docker network create redis

Start the redis container

Start the redis container “detached” ( -d ) in the background in the “redis” network to make it available for redis-cli.

  • In the terminal execute
    docker run --name redis --network redis -d redis

Start the redis command line utility

  • In the terminal execute
    docker run --name redis-cli -it --network redis --rm redis redis-cli -h redis

Using the redis command line utiliy

Store a value in the redis database
  • In the terminal execute
    set cat 10
Read a value from the redis database
  • In the terminal execute
    get cat

Leave a comment

Your email address will not be published. Required fields are marked *