1.3 安装和配置基于Docker的Redis环境

1.3.1 用docker pull下载最新Redis镜像

拉取镜像:

docker pull redis:latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete 
c7a4e4382001: Pull complete 
4044b9ba67c9: Pull complete 
c8388a79482f: Pull complete 
413c8bb60be2: Pull complete 
1abfd3011519: Pull complete 
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339

查看结果:

 docker images|grep redis
redis                                                            latest    7614ae9453d1   19 months ago   113MB

1.3.2 用docker run启动Redis容器

运行Redis容器:

docker run -itd --name myFirstRedis -p 6379:6379 redis:latest
db18e24f57c664d85897241a248fa0ebace73fe7df321fd2d34307bb2b0291e1

查看结果:

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                    NAMES
db18e24f57c6   redis:latest   "docker-entrypoint.s…"   18 seconds ago   Up 17 seconds   0.0.0.0:6379->6379/tcp   myFirstRedis

1.3.3 用docker logs观察Redis启动效果

docker logs myFirstRedis
1:C 31 Jul 2023 14:28:38.716 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 31 Jul 2023 14:28:38.716 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 31 Jul 2023 14:28:38.717 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 31 Jul 2023 14:28:38.717 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 31 Jul 2023 14:28:38.718 # Server initialized
1:M 31 Jul 2023 14:28:38.718 * Ready to accept connections

1.3.4 通过docker exec进入Redis容器

进入容器:

docker exec -it myFirstRedis /bin/bash
root@db18e24f57c6:/data# 

与redis服务器交互:

redis-cli
127.0.0.1:6379> set val 1
OK
127.0.0.1:6379> get val
"1"

退出redis-cli:

127.0.0.1:6379> exit
root@db18e24f57c6:/data# 

退出容器:

root@db18e24f57c6:/data# exit
exit

1.3.5 停止、重启和删除Redis容器

停止容器:

docker stop myFirstRedis
myFirstRedis

查看结果:

docker ps -a           
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS                      PORTS     NAMES
db18e24f57c6   redis:latest                 "docker-entrypoint.s…"   5 minutes ago   Exited (0) 28 seconds ago             myFirstRedis

再次启动容器:

docker start myFirstRedis
myFirstRedis

注:docker restart myFirstRedis也可以再次启动一个被停止的容器.但与docker start的区别在于:docker start会挂载容器所关联的文件系统,而docker restart则不会

在Redis这个场景下,若更改了Redis启动时所需加载的配置项参数,则在重启时就需要先docker stopdocker start.直接docker restart则不一定会加载更改后的配置项

1.3.6 查看Redis的版本

查看Redis服务端版本:

root@db18e24f57c6:/data# docker exec -it myFirstRedis /bin/bash
redis-server --version
Redis server v=6.2.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=b61f37314a089f19

查看Redis客户端版本:

root@db18e24f57c6:/data# redis-cli --version
redis-cli 6.2.6

1.3.7 Redis服务器和客户端

Redis是基于键值对存储的NoSQL数据库,其中的数据是存储在Redis服务器里的.和传统的MySQL数据库服务器相似,一个Redis服务器可以同多个客户端创建连接.

通过客户端停止Redis服务端:

root@db18e24f57c6:/data# redis-cli
127.0.0.1:6379> shutdown
docker ps -a
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS                     PORTS     NAMES
db18e24f57c6   redis:latest                 "docker-entrypoint.s…"   20 minutes ago   Exited (0) 9 seconds ago             myFirstRedis

当通过docker run -itd --name myFirstRedis -p 6379:6379 redis:latestdocker start myFirstRedis这两个命令启动Redis容器后,包含在容器里的Redis服务器会自动启动.

Last updated