Docker 安裝 Redis

第一步:獲取鏡像git

docker pull redis:latest

 

第二步:生成容器github

  建立宿主機 redis 容器的數據和配置文件目錄redis

# 這裏咱們在 /home/docker 下建立
mkdir /home/docker/redis/{conf,data} -p
cd /home/docker/redis

  獲取 redis 的默認配置模版docker

# 獲取 redis 的默認配置模版
# 這裏主要是想設置下 redis 的 log / password / appendonly
# redis 的 docker 運行參數提供了 --appendonly yes 但沒 password
wget https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf -O conf/redis.conf
 
# 直接替換編輯
sed -i 's/logfile ""/logfile "access.log"/' conf/redis.conf
sed -i 's/# requirepass foobared/requirepass 123456/' conf/redis.conf
sed -i 's/appendonly no/appendonly yes/' conf/redis.conf
 
# 這裏可能還需配置一些 bind protected-mode

  建立並運行一個名爲 myredis 的容器bash

# 建立並運行一個名爲 myredis 的容器
docker run \
-p 6379:6379 \
-v $PWD/data:/data \
-v $PWD/conf/redis.conf:/etc/redis/redis.conf \
--privileged=true \
--name myredis \
-d redis redis-server /etc/redis/redis.conf
 
# 命令分解
docker run \
-p 6379:6379 \ # 端口映射 宿主機:容器
-v $PWD/data:/data:rw \ # 映射數據目錄 rw 爲讀寫
-v $PWD/conf/redis.conf:/etc/redis/redis.conf:ro \ # 掛載配置文件 ro 爲readonly
--privileged=true \ # 給與一些權限
--name myredis \ # 給容器起個名字
-d redis redis-server /etc/redis/redis.conf # deamon 運行容器 並使用配置文件啓動容器內的 redis-server 

  查看活躍的容器app

# 查看活躍的容器
docker ps
# 若是沒有 myredis 說明啓動失敗 查看錯誤日誌
docker logs myredis
# 查看 myredis 的 ip 掛載 端口映射等信息
docker inspect myredis
# 查看 myredis 的端口映射
docker port myredis

 

第三步:配置外網能夠訪問ui

  更改redis.conf 文件spa

# bind 127.0.0.1
 
protected-mode no

 

進入容器日誌

docker exec -it myredis bash redis-cli
相關文章
相關標籤/搜索