Redis Docker Container

Introduction

Redis is an in-memory data structure store, used as a database, cache, and message broker. Redis officially releases Docker container routinely. With the official containers, we probably don’t have to go through the Redis installation ever.

In this blog post, I would like to quickly discuss the Redis Docker container setups for both server and client.

Redis Docker Container

Redis Server

We first run a Redis server container by running the following command on our local computer.

1
2
3
4
5
$ docker run --name redis-local-server -p 127.0.0.1:6379:6379/tcp -d redis:6.2.6 redis-server --save 60 1 --loglevel warning
4c0bd280cc7ac0d2566f11beb5c81d43f50c0afbd1cb942f5157055cc8eca987
leimao@leimao-evolvx:~/Workspace/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c0bd280cc7a redis:6.2.6 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 127.0.0.1:6379->6379/tcp redis-local-server

Redis Client

The Redis server container will run in detach mode in the background. Then, we create a Redis client container that connects to the Redis server container that we just created.

We are able to communicate with the Redis sever from the other container.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ docker run -it --rm --name redis-client --network host redis:6.2.6 redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> HSET capital China Beijing
(integer) 1
127.0.0.1:6379> HSET capital Japan Tokyo
(integer) 1
127.0.0.1:6379> HGET capital China
"Beijing"
127.0.0.1:6379> HGET capital Japan
"Tokyo"
127.0.0.1:6379> HGET capital USA
(nil)
127.0.0.1:6379> EXIT

Custom Redis Server

With the redis.conf configuration file for Redis server customization, we will never have to install Redis server on our computer.

We prepared the following redis.conf configuration file and placed it into the redis_config directory.

redis.conf
1
2
3
4
5
6
7
8
9
10
port              6380
daemonize no
save 60 1
bind 0.0.0.0
tcp-keepalive 300
dbfilename dump.rdb
dir ./
rdbcompression yes
# Need to turn off if not using default connection interfaces.
protected-mode no

Notice the port has been changed from the default 6379 to 6380.

To start the Redis server container with our custom configuration file.

1
2
$ docker run --name redis-local-custom-server -v $(pwd)/redis_conf:/usr/local/etc/redis -p 127.0.0.1:6380:6380/tcp -d redis:6.2.6 redis-server /usr/local/etc/redis/redis.conf
f0df12876d40d5fe9d9d11e5622b20ef8a88ba9b347a8d8573c182d94756d3b4

The connection was successful from our Redis client container.

1
2
3
$ docker run -it --rm --name redis-client --network host redis:6.2.6 redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> PING
PONG

References

Author

Lei Mao

Posted on

02-03-2022

Updated on

02-03-2022

Licensed under


Comments