1.拉取 redis镜像,这里使用最新版(不加latest,默认使用最新版)
docker pull redis:latest
查询redis镜像包可使用,OFFICIAL-[OK],表示官方镜像
docker search redis
2.创建redis配置文件,无特殊要求可跳过
//创建配置文件
touch /xxx/redis.conf
//编辑
vi /xxx/redis.conf
i
//保存退出
esc
:wq
redis.conf 内容
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
# 都可访问,远程连接需使用IP地址
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
# 登录密码
requirepass sk123456
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
# 默认没有日志
logfile ""
# 指定日志输出位置
# logfile /var/log/redis/redis-server.log
databases 30
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
3. 创建单个容器或使用docker-compose配置redis
1.创建单个容器
//参数说明
//-restart=always 总是开机启动
// -p 宿主机端口和容器端口映射
// -v 挂载数据卷
// -d 后台启动redis
// --appendonly yes 开启持久化
// --requirepass 123456 设置密码
docker run -p 6379:6379 --name redis --restart=always \
-v /usr/local/redis/redis.conf:/etc/redis/redis.conf \
-v /usr/local/redis/data:/data \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes --requirepass sk123456
//进入redis
docker exec -it redis redis-cli
//输入密码,测试
auth "sk123456"
2.使用容器编排创建
version: '3'
services:
redis: # 服务名称
image: redis:latest # redis镜像版本
container_name: redis # 容器名称
ports:
- 6379:6379 # 指定宿主机端口与容器端口映射关系,宿主机:容器
volumes:
- /data/redis/conf/redis.conf:/etc/redis/redis.conf # 映射配置文件目录,宿主机:容器
- /data/redis/data:/data # 映射数据目录,宿主机:容器
restart: always # 容器开机自启
privileged: true # 获取宿主机root权限
command: ["redis-server","/etc/redis/redis.conf"] # 指定配置文件启动redis-server进程
docker-compose 常用指令
docker-compose stop <容器ID/服务名> # 停止指定服务容器,不指定停止所有
docker-compose restart <容器ID/服务名> # 重启指定服务容器,不指定全部重启
docker-compose build <容器ID/服务名> # 重新构建指定容器,不指定全部重新构建
docker-compose -f docker-compose.yml up -d # -f运行指定的编排文件,不指定则默认在当前目录寻找docker-compose.yml
docker exec -it <容器名/容器ID> bash(bin/bash) 进入某个容器内部
网友评论