Docker Compose使用自定义用户名密码启动Redis

通常我们使用下面的命令来启动 redis 容器,此时连接 Redis 的时候是不需要用户认证的

shell 复制代码
sudo docker run -d --name my-redis -p 6379:6379 redis

此时我们可以使用 redis-server --requirepass "mypassword" 来指定默认用户(default)的密码,客户端可以使用 default/mypassword 来连接 Redis

shell 复制代码
sudo docker run -d --name my-redis -p 6379:6379 redis redis-server --requirepass "mypassword"

那如果想使用自定义用户名/密码可以通过 redis.conf 来实现。

创建 redis.conf 文件,内容如下

shell 复制代码
user default off on nopass nocommands
user admin >mypassword on ~* &* +@all

其中第一行是disable默认用户default,第二行是创建 admin/mypassword 用户和分配权限。

然后使用下面命令启动

shell 复制代码
docker run --name my-redis -p 6379:6379 -v /home/kongxx/redis.conf:/etc/redis.conf redis redis-server /etc/redis.conf

最后,使用compose文件redis.yml,内容如下

shell 复制代码
name: my-redis

services:
    my-redis:
        container_name: my-redis
        image: redis:latest
        restart: always
        ports:
            - 6379:6379
        command: >
            --'user default off on nopass nocommands'
            --'user admin on >mypassword ~* &* +@all'

启动容器

shell 复制代码
sudo docker compose -f ./redis.yml up -d
相关推荐
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS1 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
IT大白鼠2 天前
Redis 系列 · 第 01 篇——认知入门:Redis 是什么
redis·nosql
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
玉&心2 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing2 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
赵文宇(温玉)2 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang2 天前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特2 天前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe2 天前
docker常见命令
docker