Docker下使用Redis
Posted jfcat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker下使用Redis相关的知识,希望对你有一定的参考价值。
- 下载Redis image
sudo docker pull redis
- 运行镜像
sudo docker run -itd --name redis-test -p 6379:6379 redis
如果发现有如下重复镜像错误可以先删除镜像
sudo docker rmi 8e712f4f1065
$sudo docker run -itd --name redis-test -p 6379:6379 redis
docker: Error response from daemon: Conflict. The container name "/redis-test" is already in use by container "8e712f4f1065ed8bf01d52766aa412af60c58ac6a82521e2eeb1b4a08ef35a27". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
$sudo docker ps -a -q
8e712f4f1065
3e84ea15aac8
$sudo docker rmi 8e712f4f1065
- 检查镜像版本
sudo docker image inspect redis:latest|grep -i version
$sudo docker image inspect redis:latest|grep -i version
"GOSU_VERSION=1.12",
"REDIS_VERSION=6.2.4",
"DockerVersion": "19.03.12",
"GOSU_VERSION=1.12",
"REDIS_VERSION=6.2.4",
- 查看redis镜像情况
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea506c589d1c redis "docker-entrypoint.s…" 48 seconds ago Exited (0) 14 seconds ago redis-test
3e84ea15aac8 apache/sharding-proxy:latest "/bin/sh -c '${LOCAL…" 4 weeks ago Exited (137) 4 weeks ago vigorous_golick
- 运行和停止redis
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea506c589d1c redis "docker-entrypoint.s…" 48 seconds ago Exited (0) 14 seconds ago redis-test
3e84ea15aac8 apache/sharding-proxy:latest "/bin/sh -c '${LOCAL…" 4 weeks ago Exited (137) 4 weeks ago vigorous_golick
$ sudo docker start redis-test
redis-test
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea506c589d1c redis "docker-entrypoint.s…" 58 seconds ago Up 2 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-test
3e84ea15aac8 apache/sharding-proxy:latest "/bin/sh -c '${LOCAL…" 4 weeks ago Exited (137) 4 weeks ago vigorous_golick
$ sudo docker stop redis-test
redis-test
- 执行redis-cli
sudo docker exec -it redis-test /bin/bash
$sudo docker exec -it redis-test /bin/bash
root@9f70c9122c06:/data# redis-cli
- 性能测试
$ redis-benchmark -n 100000 -c 32 -t SET,GET,INCR,HSET,LPUSH,MSET -q
SET: 128534.70 requests per second, p50=0.127 msec
GET: 128205.13 requests per second, p50=0.127 msec
INCR: 128040.97 requests per second, p50=0.127 msec
LPUSH: 129198.97 requests per second, p50=0.127 msec
HSET: 126582.27 requests per second, p50=0.127 msec
MSET (10 keys): 136239.78 requests per second, p50=0.127 msec
- Redis字符串、整形操作
set/ get/ getset/ del/exist/ append
incr/decr/incrby/decrby
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> incr a
(integer) 2
127.0.0.1:6379> getset a 3
"2"
127.0.0.1:6379> DECR a
(integer) 2
127.0.0.1:6379> INCRBY a
(error) ERR wrong number of arguments for 'incrby' command
127.0.0.1:6379> INCRBY a 1
(integer) 3
127.0.0.1:6379> INCRBY a 2
(integer) 5
- Redis 哈希表操作
hset/hget/hmset/hmget/hgetall/hdel/hincr/hincrby
hexits/hlen/hkeys/hvals
127.0.0.1:6379> hset h1 a 1
(integer) 1
127.0.0.1:6379> hget h1 a
"1"
127.0.0.1:6379> hset h1 b 100
(integer) 1
127.0.0.1:6379> hget h1 b
"100"
127.0.0.1:6379> hmset h1 abc 100
OK
127.0.0.1:6379> hmget h1 abc 100
1) "100"
2) (nil)
127.0.0.1:6379> hmget h1 abc
1) "100"
127.0.0.1:6379> HEXISTS h1 abc
(integer) 1
127.0.0.1:6379> HLEN h1
(integer) 3
127.0.0.1:6379> hset h1 a 1 b 2 c 3
(integer) 1
127.0.0.1:6379> hget h1 a
"1"
127.0.0.1:6379> hget h1 b
"2"
127.0.0.1:6379> hgetall h1
1) "a"
2) "1"
3) "b"
4) "2"
5) "abc"
6) "100"
7) "c"
8) "3"
- Redis 链表操作
lpush/lpop/rpush/lrange/rpop
127.0.0.1:6379> lpush l1 1
(integer) 1
127.0.0.1:6379> lpush l1 2
(integer) 2
127.0.0.1:6379> lpush l1 3
(integer) 3
127.0.0.1:6379> lrange l1 1 2
1) "2"
2) "1"
127.0.0.1:6379> lrange l1 1 3
1) "2"
2) "1"
127.0.0.1:6379> lrange l1 0 3
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> rpush l1 4
(integer) 4
127.0.0.1:6379> lrange l1 0 3
1) "3"
2) "2"
3) "1"
4) "4"
127.0.0.1:6379> lrange l1 0 2
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lrange l1 0 3
1) "3"
2) "2"
3) "1"
4) "4"
- Redis的set操作
sadd/srem/smembers/sismember
sdiff/sinter/sunion
127.0.0.1:6379> sadd s1 1
(integer) 1
127.0.0.1:6379> sadd s1 2
(integer) 1
127.0.0.1:6379> smembers s1
1) "1"
2) "2"
127.0.0.1:6379> sadd s2 3
(integer) 1
127.0.0.1:6379> sadd s2 2
(integer) 1
127.0.0.1:6379> sdiff s1 s2
1) "1"
- Redis的sorted set操作
zadd key score member 将成员以及该成员的分数
zscore key member 返回指定成员的分数
zcard key 获取集合中成员数量
zrem key member 移除集合汇总指定的成员,可以指定多个成员
zrange key start end withscores 获取集合中脚注为start-end的成员
zrevrange key start stop with scores 按照分数从大到小的顺序返回索引从start到stop之间的所有元素
127.0.0.1:6379> zadd z1 1.0 a
(integer) 1
127.0.0.1:6379> zadd z1 1.5 b
(integer) 1
127.0.0.1:6379> zadd z1 2 c
(integer) 1
127.0.0.1:6379> zcard key
(integer) 0
127.0.0.1:6379> zcard z1
(integer) 3
127.0.0.1:6379> zrange z1 a c
(error) ERR value is not an integer or out of range
127.0.0.1:6379> zrange z1 1.0 2
(error) ERR value is not an integer or out of range
127.0.0.1:6379> help zrange
ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
summary: Return a range of members in a sorted set
since: 1.2.0
group: sorted_set
127.0.0.1:6379> zrange z1 1.0 2 withscores
(error) ERR value is not an integer or out of range
127.0.0.1:6379> zrange z1 1 2 withscores
1) "b"
2) "1.5"
3) "c"
4) "2"
127.0.0.1:6379> zrange z1 1 2
1) "b"
2) "c"
以上是关于Docker下使用Redis的主要内容,如果未能解决你的问题,请参考以下文章
NetCore 3.1 项目搭建反射依赖注入,Swagger结合Jwt,sqlSugar+EfCore异常中间件+Log4Net+MongoDb,Redis+Docker,丰富的公共类库,代码示例 下