Docker 安裝 redis 詳細步驟


一、下載 redis 鏡像

docker pull redis:5.0.10

   

二、配置redis.conf文件

去官網下載 redis配置文件 https://download.redis.io/releases/redis-5.0.10.tar.gz?_ga=2.70623312.1702853211.1606441998-1630381163.1606441998

下載完成后解壓

   

將redis.conf文件進行編輯

   

注釋 bind 127.0.0.1 默認開啟

關閉安全模式 protected-mode no 默認是開啟 yes

配置redis連接密碼 requirepass foobared 默認注釋關閉,開啟后不修改密碼為foobared

   

不想去官網下載配置文件,可以直接復制下面文件到redis.conf中,但是我沒測試能不能跑,應該能跑吧!!!

protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
pidfile /var/run/redis_6379.pid
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
requirepass foobared
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
stream-node-max-bytes 4096
stream-node-max-entries 100
dynamic-hz yes
rdb-save-incremental-fsync yes

   

   

三、配置宿主機環境及 redis.conf

  • 宿主機是Linux環境時

cd /opt

mkdir redis/

cd redis

mkdir data

touch redis.conf

   

命令執行完畢后,目錄結構應該是

   

   

  • 宿主機是windows環境時

    1、若不掛載宿主機配置文件啟動,執行下面命令 關閉保護模式,解除外部訪問限制,配置密碼,開啟持久化。

docker run --name redis -p 6379:6379 -d redis:5.0.10 --requirepass "foobared" --bind 0.0.0.0 --protected-mode no --appendonly yes

   

2、掛載宿主機配置文件啟動 。注意宿主機路徑 D:/docker/redis/redis.conf , D:/docker文件夾是經過配置 宿主機到docker的映射目錄 ,不配置命令執行會失敗。

docker run -p 6379:6379 --name redis -v D:/docker/redis/redis.conf:/etc/redis/redis.conf -v D;/docker/redis/data:/data -d redis:5.0.10 redis-server /etc/redis/redis.conf

   

四、在 redis目錄下執行命令、創建容器

docker run -p 6379:6379 --name redis -v /opt/redis/redis.conf:/etc/redis/redis.conf -v /opt/redis/data:/data -d redis:5.0.10 redis-server /etc/redis/redis.conf

   

-p 端口映射 宿主機端口:docker端口

--name 創建的容器名稱 redis

-v 文件映射 宿主機文件:docker文件

-d 后台啟動redis

redis-server redis服務以 指定配置文件啟動

--appendonly yes 開啟持久化(其實可以直接在redis.conf配置)

   

使用 docker logs container_name 命令查看是容器日志

   

五、使用RedisStudio 可視化工具連接、連接不上請關閉防火牆。

   

  • 使用redis-cli 連接,warning警告信息只是說 -a -u 密碼存在安全隱患。

root@3e401b54a859:/data# redis-cli -a foobared

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

127.0.0.1:6379> set test 001

OK

127.0.0.1:6379> get test

"001"

127.0.0.1:6379>

   

  • 上面的127.0.0.1是docker的本機 ,下面的連接是宿主機的IP

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM