一、简介
Redis是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
二、安装
1、拉取镜像
docker pull redis:latest
拉取之后可通过docker images
查看。
2、初始化配置文件
从github上https://github.com/redis/redis/找到并下载对应版本的redis.conf
文件,以此为模板进行配置并部分参数进行简单说明。
bind 127.0.0.1 -::1
找到该配置并将其注释。
该设置是为了避免redis暴露在互联网环境中引发安全问题,默认绑定只能本机访问。故需要将其注释。
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1
protected-mode yes
找到该配置并将其值设为no
。
该处配置则是redis默认开启保护模式,避免在互联网上打开的Redis实例会被访问和利用。当启用保护模式且默认用户没有密码时,服务器仅接受来自IPv4地址(127.0.0.1)、IPv6地址的本地连接(::1)或Unix域套接字。该设置可视情况而定。
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and the default user has no password, the server
# only accepts local connections from the IPv4 address (127.0.0.1), IPv6 address
# (::1) or Unix domain sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured.
protected-mode yes
daemonize no
维持默认
该配置为设置redis是否以守护进程运行,如果设置为yes
会使得对/var/run/redis.pid
配置文件的修改无效。故设置维持默认即可。
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no
dir ./
工作目录设置,维持默认
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
appendonly no
持久化,改为yes
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check https://redis.io/topics/persistence for more information.
appendonly no
3、启动容器
docker run \
--restart=always \
-p 6379:6379 \
--name myredis \
--net mynet1 --ip 172.18.0.4 \
-v /opt/redis_data_docker/redis.conf:/etc/redis/redis.conf \
-v /opt/redis_data_docker/data:/data \
-v /etc/localtime:/etc/localtime:ro \
-d redis \
redis-server /etc/redis/redis.conf \
--appendonly yes \
--requirepass 密码
通过命令docker logs 容器id或容器名
查看启动日志。正常即表示容器启动完毕。