1.拉取镜像
docker pull redis:5.0.8
2.新建目录conf存放配置文件和目录data存放数据
mkdir docker/redis/{conf,data} -p
3.下载配置文件
curl https://raw.githubusercontent.com/antirez/redis/5.0.8/redis.conf -o conf/redis.conf
4.创建容器
docker run -v ${PWD}/data:/data:rw -v ${PWD}/conf/redis.conf:/etc/redis.conf:ro -p 6379:6379 --name redis -d redis:5.0.8 redis-server /etc/redis.conf
(注:pwd是命令,PWD是变量,所以不能用$pwd)
5.进入容器并运行客户端
docker exec -it redis bash
redis-cli
6.测试
127.0.0.1:6379> ping PONG
后记
配置文件有这么一段,如果不指定bind,会监听所有的外部连接,默认bindlocalhost,因为部署在ecs上,所以将这条注释掉。
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ 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 into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1