docker中安装redis
原创 2019-05-30 19:02 阅读(1635)次
1.搜索
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that 6792 [OK]
bitnami/redis Bitnami Redis Docker Image 111 [OK]
sameersbn/redis 76 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0 & 5.0 45
hypriot/rpi-redis Raspberry Pi compatible redis image 34
kubeguide/redis-master redis-master with "Hello World!" 28
2.拉取
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 4.0 14433f4e77ab 3 weeks ago 83.4MB
3.默认数据是存在docker容器的/data目录
创建redis数据文件与配置目录,后面启动docker时要映射目录到本机文件里,以免丢失redis数据:
/home/docker/redis/r1下有如下文件:
[root@localhost data]# cd ..
[root@localhost r1]# ll
total 8
drwxr-xr-x 2 polkitd root 23 Apr 23 06:21 data
drwxr-xr-x 2 root root 6 Apr 23 05:30 logs
-rw-r--r-- 1 root root 207 Apr 23 06:16 redis.conf
-rwxrwxrwx 1 root root 243 Apr 23 06:14 start.sh
自建一个redis.conf:
自建启动脚本:
[root@localhost r1]# cat start.sh
启动时网络模式最好用host模式,默认为桥接模式,改成--net=host
-v参数把配置文件与数据目录都映射到本机的/home/docker/redis/r1目录中
4.启动
[root@localhost r1]# ./start.sh
84323ba53d5bed5ebaef1f97d46623262d9974fae42bea54ee675cdc363c885b
[root@localhost r1]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84323ba53d5b redis:4.0 "docker-entrypoint.s猞" 9 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp redis1
5.进入docker中,用redis-cli工具连接redis
docker exec -it 84323ba53d5b /bin/bash
redis-cli -a passwordky123
127.0.0.1:6379> set a 12
OK
127.0.0.1:6379> get a
"12"
docker search redis
[root@localhost r1]# docker search redisNAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that 6792 [OK]
bitnami/redis Bitnami Redis Docker Image 111 [OK]
sameersbn/redis 76 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0 & 5.0 45
hypriot/rpi-redis Raspberry Pi compatible redis image 34
kubeguide/redis-master redis-master with "Hello World!" 28
2.拉取
docker pull redis:4.0
[root@localhost r1]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE
redis 4.0 14433f4e77ab 3 weeks ago 83.4MB
3.默认数据是存在docker容器的/data目录
创建redis数据文件与配置目录,后面启动docker时要映射目录到本机文件里,以免丢失redis数据:
/home/docker/redis/r1下有如下文件:
[root@localhost data]# cd ..
[root@localhost r1]# ll
total 8
drwxr-xr-x 2 polkitd root 23 Apr 23 06:21 data
drwxr-xr-x 2 root root 6 Apr 23 05:30 logs
-rw-r--r-- 1 root root 207 Apr 23 06:16 redis.conf
-rwxrwxrwx 1 root root 243 Apr 23 06:14 start.sh
自建一个redis.conf:
requirepass 123456
port 6379
bind 0.0.0.0
logfile "/data/redis.log"
#daemonize yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename redis1.rdb
dir /data
自建启动脚本:
[root@localhost r1]# cat start.sh
docker run -d --privileged=true --net=host -p 6379:6379 -v /home/docker/redis/r1/redis.conf:/etc/redis/redis.conf -v /home/docker/redis/r1/data:/data --name redis1 redis:4.0 redis-server /etc/redis/redis.conf
启动时网络模式最好用host模式,默认为桥接模式,改成--net=host
-v参数把配置文件与数据目录都映射到本机的/home/docker/redis/r1目录中
4.启动
[root@localhost r1]# ./start.sh
84323ba53d5bed5ebaef1f97d46623262d9974fae42bea54ee675cdc363c885b
[root@localhost r1]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84323ba53d5b redis:4.0 "docker-entrypoint.s猞" 9 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp redis1
5.进入docker中,用redis-cli工具连接redis
docker exec -it 84323ba53d5b /bin/bash
redis-cli -a passwordky123
127.0.0.1:6379> set a 12
OK
127.0.0.1:6379> get a
"12"