Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API.
安装环境
Linux系统:Ubuntu 16.04
docker版本:18.09.5
redis版本: 5.0
部署思路
- OS切换到root用户,并启动docker
$ su root
$ sytemctl start docker
- 拉取mysql 8.0 镜像
访问:https://hub.docker.com 搜索redis 我们可以看到
docker pull redis:5.0
- 下载完成
root@instance-o70no2nw:~# su root
root@instance-o70no2nw:~# docker pull mysql:8.0.16
latest: Pulling from library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
Digest: sha256:f5f78fe2054b4686da3fddb460eab0b53d04e067c977d6a02fcb5ec25375ed15
Status: Downloaded newer image for mysql:8.0.16
- 查看镜像
root@instance-o70no2nw:~# docker pull redis:5.0
5.0: Pulling from library/redis
27833a3ba0a5: Already exists
cde8019a4b43: Pull complete
97a473b37fb2: Pull complete
c6fe0dfbb7e3: Pull complete
39c8f5ba1240: Pull complete
cfbdd870cf75: Pull complete
Digest: sha256:000339fb57e0ddf2d48d72f3341e47a8ca3b1beae9bdcb25a96323095b72a79b
Status: Downloaded newer image for redis:5.0
root@instance-o70no2nw:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 5.0 a55fbf438dfd 4 weeks ago 95MB
- 启动容器 (重要)
思路:redis可用,需保以下几点:
- 端口开放:6379对外开放 -> 需映射端口
- 数据持久化:配置文件、存储信息不丢失 -> 需挂载本地卷
- 永不停机: restart=always
- 守护态运行: -d
基于以上思路, redis:
$ docker run \
--name redis \
--restart=always \
-p 6379:6379 \
-v /usr/local/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/docker/redis/redis/data:/data \
-d redis:5.0 /usr/local/etc/redis/redis.conf
- 验证
[root@scrt ~]# redis-cli -h 182.61.15.132
182.61.15.139:6379> set users "gaoxuefeng"
OK
182.61.15.139:6379> get users
"gaoxuefeng"
182.61.15.139:6379>
到此结束
其他资料
- 官方指南:Docker Hub for Redis
关注点:
Additionally, If you want to use your own redis.conf …
You can create your own Dockerfile that adds a redis.conf from the context into /data/, like so.
FROM redis
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
Alternatively, you can specify something along the same lines with docker run options.
$ docker run -v /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf
Where /myredis/conf/ is a local directory containing your redis.conf file. Using this method means that there is no need for you to have a Dockerfile for your redis container.