sudo docker pull redis
使用配置文件(更灵活)
bash
# 创建存放配置文件的目录
mkdir -p /home/redis/conf
# 下载默认配置文件
wget -O /home/redis/conf/redis.conf http://download.redis.io/redis-stable/redis.conf
# 编辑配置文件设置密码
sudo nano /home/redis/conf/redis.conf
在配置文件中找到并修改以下行:
plaintext
# requirepass foobared
改为:
plaintext
requirepass your_password_here
然后启动容器:
bash
sudo docker run -d --name redis-container -p 6379:6379 -v /home/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf
验证 Redis 是否正常运行并设置了密码:
# 进入容器
sudo docker exec -it redis-container redis-cli
# 尝试执行命令,会提示需要认证
127.0.0.1:6379> ping
# 进行认证
127.0.0.1:6379> auth your_password_here
# 认证成功后可以执行命令
127.0.0.1:6379> ping
PONG