拉取镜像
以docker官方的3.2版本为例:
docker pull redis:3.2
查看镜像
[root@seven ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
redis 3.2 5b1dd5e7e25e 6 weeks ago 99.69 MB
创建并运行容器
docker run -p 6379:6379 --name redis_ian --restart=always -v /root/docker/redis/data:/data -d redis:3.2 redis-server --appendonly yes
参数说明:
- -p 6379:6379:指定暴露的端口
- --name redis_ian:为容器设置名称
- -v /root/docker/redis/data:/data:将主机中
/root/docker/redis/data
目录挂载到容器的/data
目录
-d 后台启动
查看容器
[root@seven ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
512a5d83b4c3 redis:3.2 "docker-entrypoint.s 8 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp redis_ian
查看启动日志
[root@seven ~]# docker logs 512a5d83b4c3
1:M 27 Apr 12:03:10.012 # Not listening to IPv6: unsupproted
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 27 Apr 12:03:10.013 # 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 27 Apr 12:03:10.013 # Server started, Redis version 3.2.11
1:M 27 Apr 12:03:10.013 # 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 27 Apr 12:03:10.013 # 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 27 Apr 12:03:10.013 * DB loaded from append only file: 0.000 seconds
1:M 27 Apr 12:03:10.014 * The server is now ready to accept connections on port 6379
进入容器
[root@seven ~]# docker exec -it 512a5d83b4c3 bash
root@512a5d83b4c3:/data# redis-cli
127.0.0.1:6379> keys *
1) "a"
2) "1"
3) "b"
127.0.0.1:6379> set c ccc
OK
127.0.0.1:6379> keys *
1) "a"
2) "1"
3) "b"
4) "c"
127.0.0.1:6379> get c
"ccc"
127.0.0.1:6379>
配置Redis
设置密码
首先进去容器,然后执行以下命令:
127.0.0.1:6379> CONFIG SET requirepass "123456"
OK
以上!