容器

Docker安装Redis(使用dockerfile)

2020-02-03  本文已影响0人  夜行神喵

1. 下载镜像

docker pull redis

或在官网查看 https://redis.io 哪个是最后稳定版本(latest stable version)

image.png
docker pull redis:5.0.7
�docker history redis:5.0.7
docker image inspect redis:5.0.7

2. 创建redis参数文件

2.1 下载参数文件模板

https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf

mkdir -p ~/Docker/redis/data
cd ~/Docker/redis/data

2.2 修改参数文件

mv ~/Downloads/redis.conf redis.conf.orig
vi redis.conf.orig
#bind 127.0.0.1
bind 127.0.0.1
daemonize no
#protected-mode yes
protected-mode no
#requirepass foobared
requirepass welcome1
#appendonly no
appendonly yes

3. 创建Dockerfile

cd ~/Docker/redis/

vi Dockerfile

FROM redis:5.0.7
MAINTAINER mayongzhi@163.com
COPY ./data/redis.conf.orig /usr/local/etc/redis/redis.conf
EXPOSE 6379
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

4. 创建镜像

docker build -t myredis:5.0.7 .

myzmac:redis myz$ docker build -t myredis:5.0.7 .
Sending build context to Docker daemon  127.5kB
Step 1/5 : FROM redis:5.0.7
 ---> 9b188f5fb1e6
Step 2/5 : MAINTAINER mayongzhi@163.com
 ---> Running in c4ba5c76f878
Removing intermediate container c4ba5c76f878
 ---> 2a616e1fccd6
Step 3/5 : COPY ./data/redis.conf.orig /usr/local/etc/redis/redis.conf
 ---> e2f3268b26dd
Step 4/5 : EXPOSE 6379
 ---> Running in 90a9b1a59d54
Removing intermediate container 90a9b1a59d54
 ---> 9f92e2f1a529
Step 5/5 : CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
 ---> Running in 3c7b6eb85d97
Removing intermediate container 3c7b6eb85d97
 ---> 46a8c234c96d
Successfully built 46a8c234c96d
Successfully tagged myredis:5.0.7
myzmac:redis myz$ 

myzmac:redis myz$ docker images myredis
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myredis             5.0.7               46a8c234c96d        31 seconds ago      98.3MB

4. 创建容器

docker run -e TZ="Asia/Shanghai"  -h myredis --name myredis507  -m 64m  -p 63791:6379 -v /Users/myz/Docker/redis/data:/data -d myredis:5.0.7

5. 连接使用

myzmac:redis myz$ docker exec -it myredis bash
root@myredis:/data# redis-cli -a welcome1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set nm myz
OK
127.0.0.1:6379> get nm
"myz"
127.0.0.1:6379> save 
OK
127.0.0.1:6379> 
root@myredis:/data# 
redis-cli -h 1.1.1.2 -p  63791 -a welcome1
pip3 install redis

myzmac:redis myz$ cat demo.py
import redis
redis_pool = redis.ConnectionPool(host='127.0.0.1', port= 63791, password= 'welcome1', db= 0)
redis_conn = redis.Redis(connection_pool= redis_pool)
v = redis_conn.get('nm')
print (v)
name_dict = {
    'nm1' : 'mmm',
    'nm2' : 'yyy',
    'nm3' : 'zzz'
}
redis_conn.mset(name_dict)
m = redis_conn.mget('nm1', 'nm1','nm3')
print(m)

myzmac:redis myz$ 
myzmac:redis myz$ python3 demo.py 
b'myz'
[b'mmm', b'mmm', b'zzz']
myzmac:redis myz$ cat demo2.py 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = 'demo'
__author__ = 'Jason Ma'
__mtime__ = '2020/2/2'
__version__= ' '
"""

import redis
import time

redis_pool = redis.ConnectionPool(host='127.0.0.1', port= 63791, password= 'welcome1', db= 1)
r = redis.Redis(connection_pool= redis_pool)
r.flushdb()

# start = time.time()
# for i in range(10000):
#     r.set('nm_'+str(i),i)
# print(r.dbsize())
# end = time.time()
# print ("执行用时:",end-start,"秒")

redis_pool = redis.ConnectionPool(host='127.0.0.1', port= 63791, password= 'welcome1', db= 2)
r = redis.Redis(connection_pool= redis_pool)
pipe = r.pipeline()
r.flushdb()

start = time.time()
for i in range(100000):
    pipe.set('nm_'+str(i),i)

pipe.execute()
cnt = r.dbsize()
end = time.time()

print ("写入",cnt,"条用时:",end-start,"秒")
myzmac:redis myz$ python3 demo2.py
写入 100000 条用时: 3.0522289276123047 秒
myzmac:redis myz$ python3 demo2.py
写入 100000 条用时: 2.919153928756714 秒

FYI:

REDIS官网:
https://redis.io

详细参数配置参考:
https://redis.io/topics/config

通过python 使用redis
https://zhuanlan.zhihu.com/p/51608696

上一篇下一篇

猜你喜欢

热点阅读