Docker安裝Redis服務並以配置文件方式啟動
1)鏡像獲取
#搜索鏡像
docker search redis
#拉取鏡像
docker pull redis
#查看鏡像
docker images
2)啟動Redis
一、從Redis.io官網獲取redis.conf配置文件
二、在/usr/local/創建redis文件夾,並把redis.conf文件拷貝進來
三、修改redis.conf配置文件
#注釋掉這部分,這是限制redis只能本地訪問 bind 127.0.0.1
#默認yes,開啟保護模式,限制為本地訪問 protected-mode no
#默認no,改為yes意為以守護進程方式啟動,可后台運行,除非kill進程,改為yes會使配置文件方#式啟動redis失敗 daemonize no
#redis持久化(可選) appendonly yes
#設置密碼 requirepass 123456
四、啟動命令
docker run -p 6379:6379 --name redis -v /usr/local/redis.conf:/etc/redis/redis.conf -v /usr/local/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
命令注釋:
- -p 6379:6379 端口映射:前表示主機部分,:后表示容器部分。
- --name redis 指定該容器名稱,查看和進行操作都比較方便。
- -v 掛載目錄,規則與端口映射相同。
- -d redis 表示后台啟動redis。
- redis-server /etc/redis/redis.conf 以配置文件啟動redis,加載容器內的conf文件,最終找到的是掛載的目錄/usr/local/redis.conf。
- --appendonly yes 開啟redis 持久化
3)總結
- 在服務器部署redis服務,一定要設置密碼,不然很容易受到攻擊!
- 使用外部服務連接redis,需要開放相應的端口,同時根據上文所說redis.conf配置文件注釋掉bind行,否則訪問不了