1安装
docker pull redis:latest
2 启动容器并带密码
docker run --name myredis -p 6379:6379 -d --restart=always redis:latest redis-server --appendonly yes --requirepass "root123456"
-p 6379:6379 :将容器内端口映射到宿主机端口(右边映射到左边)
redis-server –appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
requirepass “your passwd” :设置认证密码
–restart=always : 随docker启动而启动
查看容器
docker ps
查看所有容器
docker ps -a
查看进程
ps -ef|grep redis
3进入容器执行redis客户端
redis容器的id是 a126ec987cfe
docker exec -it a126ec987cfe redis-cli -a ‘your passwd‘
[[email protected]~]# docker exec -it a126ec987cfe redis-cli -h 127.0.0.1 -p 6379 -a ‘your passwd‘ 127.0.0.1:6379> ping PONG 127.0.0.1:6379> info # Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:d3ebfc7feabc1290 redis_mode:standalone os:Linux 3.10.0-693.21.1.el7.x86_64 x86_64 ...
-h 127.0.0.1 :默认不加为-h 127.0.0.1 -p 6379 :默认不加为 -p 6379
或者连接的时候不带密码,如下:
[[email protected] ~]# docker exec -it a126ec987cfe redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth ‘your passwd‘ OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> info # Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:d3ebfc7feabc1290 redis_mode:standalone os:Linux 3.10.0-693.21.1.el7.x86_64 x86_64 arch_bits:64
原文地址:https://www.cnblogs.com/Samuel-Leung/p/12316079.html
时间: 2024-11-07 08:16:21