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'
[root@localhost~]# 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
或者連接的時候不帶密碼,如下:
[root@localhost ~]# 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