Docker-redis

 2022-08-08    0 条评论    19082 浏览

docker redis

Docker服务安装

略...

Docker安装redis服务

redis在docker官网地址:https://hub.docker.com/_/redis

拉取镜像

docker pull redis

也可以指定版本拉取

docker pull redis:7.0.4

拉取成功显示如下:

[root@VM-24-13-centos ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis
1efc276f4ff9: Pull complete
adf5288e41ff: Pull complete
2f387b0b685f: Pull complete
1b3d2bd4ccba: Pull complete
937c39fc1973: Pull complete
751dcbc45baa: Pull complete
Digest: sha256:9bc34afe08ca30ef179404318cdebe6430ceda35a4ebe4b67d10789b17bdf7c4
Status: Downloaded newer image for docker.io/redis:latest

官网拉取配置文件

wget http://download.redis.io/redis-stable/redis.conf

修改配置文件内容

启动命令

指定配置文件进行启动

docker run -p 6379:6379 --name redis \
-v /opt/redis/redis.conf:/etc/redis/redis.conf \
-v /opt/redis/data:/data \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes

命令详解

-p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。
--name myredis  指定该容器名称,查看和进行操作都比较方便。
-v 挂载目录,规则与端口映射相同。
-d redis 表示后台启动redis
redis-server /etc/redis/redis.conf  以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录/opt/redis_docker/redis.conf
appendonly yes 开启redis 持久化

进入容器

docker exec -it redis /bin/bash

代码测试

import redis

ip = 'x.x.x.x'
password = 'auth'

r1 = redis.Redis(host=ip, password=password, port=6379, db=0, decode_responses=True)

# 打印详情
print(r1.info())
# set、get
print(r1.set('qhou', 'test'))
print(r1.get('qhou'))

打印结果

{'redis_version': '7.0.4',...
True
test