文章目录
环境信息
操作系统
$ cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
$ uname --kernel-release
3.10.0-1062.el7.x86_64
Docker 版本
$ docker version
Client: Docker Engine - Community
Version: 19.03.11
API version: 1.40
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:13:48 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.11
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:12:26 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
Redis 配置文件
因为我准备在主机 A 中使用 Java 客户端连接主机 B 中的 Redis 服务器,所以需要提前准备配置文件,方便客户端连接服务器。
提示:Redis 配置文件可以从 Redis 的 GitHub 仓库中找到。(注意选择合适的版本)
编辑配置文件,注释 bind 127.0.0.1
(第 22 行),监听所有网卡的连接请求:
# 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
不以后台进程的方式运行 Redis(第 3 行):
# 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.
daemonize no
设置认证密码(第 8 行):
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# requirepass foobared
requirepass 123456
编辑完成之后,将该配置文件存放在 /usr/local/redis/
目录中,稍后需要用到。
部署 Redis
从 Docker 仓库中拉取 Redis 镜像:
$ docker pull redis:6.0.8
查看拉取的镜像:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0.8 84c5f6e03bf0 8 days ago 104MB
运行 Redis 实例:
$ docker run --detach --publish 6379:6379 --volume /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis redis:6.0.8 redis-server /usr/local/etc/redis/redis.conf
注:--volume /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf
表示将本地的 /usr/local/redis/redis.conf
配置文件绑定到 Docker 容器中的 /usr/local/etc/redis/redis.conf
。
查看容器,可以看到 Redis 已经成功启动:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c3aa3241302 redis:6.0.8 "docker-entrypoint.s…" 34 minutes ago Up 16 minutes 0.0.0.0:6379->6379/tcp redis
查看 Redis 容器的信息(下面只贴出其中的一部分):
$ docker inspect redis
[
{
"Args": [
"redis-server",
"/usr/local/etc/redis/redis.conf"
],
"HostConfig": {
"Binds": [
"/usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf"
]
}
}
]
进入正在运行的 Redis 容器:
$ docker exec --interactive --tty redis /bin/sh
redis-server、redis-cli 等可执行文件位于容器中的 /usr/local/bin
目录中:
$ cd /usr/local/bin && ls -l
total 26484
-rwxrwxr-x. 1 root root 374 Sep 10 19:12 docker-entrypoint.sh
-rwxr-xr-x. 1 root root 2404352 Apr 16 06:41 gosu
-rwxr-xr-x. 1 root root 6707152 Sep 10 19:14 redis-benchmark
lrwxrwxrwx. 1 root root 12 Sep 10 19:14 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root 12 Sep 10 19:14 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 6679304 Sep 10 19:14 redis-cli
lrwxrwxrwx. 1 root root 12 Sep 10 19:14 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 11317848 Sep 10 19:14 redis-server
参考
https://hub.docker.com/_/redis/