本文最后更新于1 分钟前,文中所描述的信息可能已发生改变。
一、找到一个合适的docker的redis的版本
可以去docker hub
中去找一下https://hub.docker.com/_/redis?tab=tags
二、使用docker安装redis
bash
sudo docker pull redis
安装好之后使用docker images
即可查看
truedei@truedei:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 987b78fc9e38 10 days ago 104MB
httpd latest a8a9cbaadb0c 2 weeks ago 166MB
fjudith/draw.io latest 7b136fc80d31 3 weeks ago 683MB
mysql 5.7.29 f5829c0eee9e 5 weeks ago 455MB
truedei@truedei:~$
truedei@truedei:~$
三、准备redis的配置文件
因为需要redis的配置文件,这里最好还是去redis
的官方去下载一个redis
使用里面的配置文件即可redis
中文官方网站: http://www.redis.cn/download.html
下载后解压出来: 这个redis.conf
文件就是咱们需要的,为了保险,还是拷贝一下,做个备份。
四、配置redis.conf配置文件
修改redis.conf
配置文件:
yml
# 主要配置的如下:
# 注释掉这部分,使redis可以外部访问
# bind 127.0.0.1
# 用守护线程的方式启动
daemonize no
# 给redis设置密码
requirepass your_password
# redis持久化,默认是no
appendonly yes
# 防止出现远程主机强迫关闭了一个现有的连接的错误,默认是300
tcp-keepalive 300
五、创建本地与docker映射的目录,即本地存放的位置
创建本地存放redis的位置; 可以自定义,因为我的docker
的一些配置文件都是存放在./data
目录下面的,所以我依然在./data
目录下创建一个redis
目录,这样是为了方便后期管理
bash
root@root:redis-5.0.5$ sudo mkdir /data/redis
root@root:redis-5.0.5$ sudo mkdir /data/redis/data
把配置文件拷贝到刚才创建好的文件里 因为我本身就是Linux
操作系统,所以我可以直接拷贝过去,如果你是windows
的话,可能需要使用ftp
拷贝过去,或者直接复制内容,然后粘贴过去。
bash
root@root:redis-5.0.5$ sudo cp -p redis.conf /data/redis/
root@root:redis-5.0.5$
六、启动docker redis
启动:
bash
root@root:~$ sudo docker run -p 6379:6379 --name redis -v /data/redis/redis.conf:/etc/redis/redis.conf -v /data/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
参数解释:
- -p 6379:6379: 把容器内的6379端口映射到宿主机6379端口
- -v /data/redis/redis.conf:/etc/redis/redis.conf: 把宿主机配置好的redis.conf放到容器内的这个位置中
- -v /data/redis/data:/data: 把redis持久化的数据在宿主机内显示,做数据备份
- redis-server /etc/redis/redis.conf: 这个是关键配置,让redis不是无配置启动,而是按照这个redis.conf的配置启动
- –appendonly yes: redis启动后数据持久化
七、查看是否启动成功
查看是否成功启动:
bash
root@root:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85cb7d83a2ff redis "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:6379->6379/tcp redis
0a122a08125f mysql:5.7.29 "docker-entrypoint.s…" 5 weeks ago Up About an hour 0.0.0.0:3306->3306/tcp, 33060/tcp mysql57
可以查看一下日志:
shell
root@root:~$ sudo docker logs redis
1:C 29 May 2020 01:16:22.107 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 29 May 2020 01:16:22.107 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 29 May 2020 01:16:22.107 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 29 May 2020 01:16:22.108 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 29 May 2020 01:16:22.108 # Server initialized
1:M 29 May 2020 01:16:22.108 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 29 May 2020 01:16:22.108 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 29 May 2020 01:16:22.108 * Ready to accept connections
root@root:~$