繁体   English   中英

从主机连接到在 Docker 容器中运行的 Redis

[英]Connecting to Redis running in Docker Container from Host machine

我看到很多人都在为此苦苦挣扎,感觉可能是 redis 容器映像中存在错误,而其他人似乎也在解决类似的问题。

我在 DockerHub 上使用标准 redis 映像。 ( https://github.com/dockerfile/redis )

像这样运行它:

docker run -it -p 6379:6379 redis bash

进入后,我可以启动服务器,并从容器映像执行 redis ping。

不幸的是,我无法从我的主机连接到 redis 容器。

我尝试过设置,如下所示。

bind 127.0.0.1

并从配置中删除了绑定

并尝试关闭保护模式

protected-mode no

我知道它正在读取配置文件,因为我更改了端口只是为了测试,而且我能够做到这一点。

我正在运行 Windows 10,所以可能是 Windows 网络问题。 我通常对 docker 从来没有问题。 我很困惑

问题在于您的绑定,您应该设置以下内容:

bind 0.0.0.0

这会将redis设置为绑定到所有可用接口,在具有一个接口( eth0 )和环回( lo )的容器化环境中,redis 将绑定到上述两个接口。 您应该考虑通过config file其他指令或使用firewalls等外部工具添加安全措施。 因为通过这种方法,每个人都可以连接到您的redis服务器。

默认设置为bind 127.0.0.1并且此设置将导致redis仅侦听环回接口,并且只能从容器内部访问它。 (为了安全)

要使用自定义配置文件运行 redis:

sudo docker run -d --name redis-test -p 6379:6379  -v /path/to/redisconf/redis.conf:/redis.conf redis redis-server /redis.conf

现在在安装了redis-tools docker 主机上进行验证:

redis-cli                           
127.0.0.1:6379> 
127.0.0.1:6379> set farhad likes:stackoverflow
OK
127.0.0.1:6379> get farhad
"likes:stackoverflow"
127.0.0.1:6379> 

您还可以通过以下方式从外部主机连接到您的redis容器:

redis-cli -h 'IP-address-of-dockerhost-running-redis-container'

这是设置 Redis 容器的更简单方法。

下载镜像并运行容器

docker run -d --name some-redis -p 6379:6379 redis

如果您没有图像,此命令将拉取它。 然后,如果您需要从redis-cli访问控制台,可以使用:

docker exec -it some-redis bash

进入容器控制台,并在控制台中输入:

root@72c388dc2cb8:/data# redis-cli

输出:

127.0.0.1:6379> 

这对于我的用例来说已经足够了(简单快速的本地开发)。

现在使用4.0.9版( Docker Toolbox on Win10 )可能会更容易。 只需连接一个 redis 客户端,然后:

set bind 0.0.0.0
save

新设置在停止/启动后保持不变。

这里有一些说明可以使这项工作正常进行。

安装官方 Docker 而不是发行版存储库。

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
systemctl enable docker ; systemctl start docker; systemctl status docker

请参阅使用便利脚本安装

从主机连接到 Redis 容器

mkdir -p /etc/redis/
chown -R 1000:1000 /etc/redis
sudo docker run -d --name redis -p 6379:6379 --restart unless-stopped -v /etc/redis/:/data redis redis-server /data

注意:对您的解决方案至关重要的重要部分是将端口公开 (-p 6379:6379) 到您的 docker 主机并路由到容器端口。 请参阅Redis Docker 文档

在您的 docker 主机中安装“redis-tools”。 Centos 通过 epel 发行版安装 redis。

使用以下命令创建Redis容器

sudo docker run -d --name redis-test -p 6379:6379  -v /redis/redis.conf:/redis.conf redis redis-server /redis.conf --appendonly yes --requirepass "redis"

您可以使用Redis-CLI在同一台机器上访问 Redis,如果您使用其他机器,请使用host machine IP 如果您正在访问同一主机中的 Redis 容器,则另一个 docker 容器使用该机器的private IP

如果您想在 docker 容器中运行 Redis 集群。

答案在 Redis 自记录的redis.conf文件中,这里是从文件中提取的关于如何像专业人士一样解决问题的摘录。

所有归功于 redis.conf 文件,redis 版本 6 及更高版本。

########################## CLUSTER DOCKER/NAT support  ########################

# In certain deployments, Redis Cluster nodes address discovery fails, because
# addresses are NAT-ted or because ports are forwarded (the typical case is
# Docker and other containers).
#
# In order to make Redis Cluster working in such environments, a static
# configuration where each node knows its public address is needed. The
# following two options are used for this scope, and are:
#
# * cluster-announce-ip
# * cluster-announce-port
# * cluster-announce-bus-port
#
# Each instructs the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node
# publishing the information.
#
# If the above options are not used, the normal Redis Cluster auto-detection
# will be used instead.
#
# Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usual.
#
# Example:
#
# cluster-announce-ip 10.1.1.5
# cluster-announce-port 6379
# cluster-announce-bus-port 6380

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM