Linux 安装 Redis

Redis - Linux下环境搭建 - docker

一、安装过程

[root@VM-16-12-centos docker]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ... 
latest: Pulling from docker.io/library/redis
bf5952930446: Pull complete 
911b8422b695: Pull complete 
093b947e0ade: Pull complete 
5b1d5f59e382: Pull complete 
7a5f59580c0b: Pull complete 
f9c63997c980: Pull complete 
Digest: sha256:09c33840ec47815dc0351f1eca3befe741d7105b3e95bc8fdb9a7e4985b9e1e5
Status: Downloaded newer image for docker.io/redis:latest
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     latest              1319b1eaa0b7        7 hours ago         104 MB
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# docker run -p 6379:6379 --name redis -d docker.io/redis redis-server
099ef0f5b1bfe45172b46e2511bdcf78203ff40450dc61140182e797a5d49e2d
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS          
          NAMES099ef0f5b1bf        docker.io/redis     "docker-entrypoint..."   10 seconds ago      Up 10 seconds       0.0.0.0:6379->6
379/tcp   redis[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# 
[root@VM-16-12-centos docker]# docker exec -it 099ef0f5b1bf redis-cli
127.0.0.1:6379> 

二、以配置方式启动 redis

配置文件修改项

bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode no #默认yes,开启保护模式,限制为本地访问
daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方#式启动redis失败
appendonly yes #redis持久化(可选)
requirepass 123456 #个人测试使用的密码

启动

docker run -p 6379:6379 --name myredis 
-v /usr/local/redis/config/redis.conf:/etc/redis/redis.conf 
-v /usr/local/redis/data:/data 
-d docker.io/redis redis-server /etc/redis/redis.conf 
--appendonly yes

命令解释说明:

-p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。

--name myredis  指定该容器名称,查看和进行操作都比较方便。

-v 挂载目录,规则与端口映射相同。

为什么需要挂载目录:个人认为docker是个沙箱隔离级别的容器,这个是它的特点及安全机制,不能随便访问外部(主机)资源目录,所以需要这个挂载目录机制。

-d redis 表示后台启动redis

redis-server /etc/redis/redis.conf  以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录/usr/local/docker/redis.conf

三、问题

1、以配置方式启动后 容器没有启动,原因未知。

原因:daemonize 配置成了 yes 导致的问题

daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方#式启动redis失败

解决方法:

  • 更改配置
daemonize no
  • 进入容器手动启动

a、以默认方式启动 ,即 不指定配置文件

b、进入容器

c、找到 redis-server 所在目录

d、然后以 配置问价方式启动

f、退出 容器

docker exec -it 61acfe1a5b88 bash

whereis redis-server

./redis-server /etc/redis/redis.conf

exit

参考资料:

Docker安装redis的使用

docker安装redis并以配置文件方式启动

原文地址:https://www.cnblogs.com/wangwangfei/p/13443086.html