简体   繁体   English

Redis Docker 连接被拒绝

[英]Redis Docker connection refused

I just built the redis docker instance我刚刚构建了 redis docker 实例

$ docker pull redis

After which I ran it like this.之后我就这样运行了。

$ docker run --name=redis --detach=true --publish=6379:6379 redis

I get the following我得到以下

$ docker ps 
key        redis   "/sbin/entrypoint.sh"    22 minutes ago      Up 22 minutes       0.0.0.0:6379->6379/tcp   redis

To me the above means that it is now running listening on port 6379 on localhost or 127.0.0.1 or 0.0.0.0.对我来说,以上意味着它现在正在 localhost 或 127.0.0.1 或 0.0.0.0 上的端口 6379 上运行侦听。

But to my great surprise, when I try to connect is responds with connection refused.但令我惊讶的是,当我尝试连接时,连接被拒绝。

Please can someone throw some light.请有人可以扔一些灯。

You need to provide more information about your environment (OS, Docker installation, etc), but basically, if you start your Redis container like this:您需要提供有关您的环境(操作系统、Docker 安装等)的更多信息,但基本上,如果您像这样启动 Redis 容器:

docker run --name=redis-devel --publish=6379:6379 --hostname=redis --restart=on-failure --detach redis:latest

It should expose the port no matter what.无论如何,它应该公开端口。 The only reason you might not be able to connect to it, is if you've messed up your bridge interface, if you're on Linux, or you're using a docker machine with its own network interface and IP address and you're not connecting to that IP address.您可能无法连接到它的唯一原因是,如果您弄乱了桥接接口,如果您使用的是 Linux,或者您正在使用具有自己的网络接口和 IP 地址的 docker 机器并且您'未连接到该 IP 地址。 If you're using Docker for Mac, then that only supports routing to the localhost address, since bridging on Mac hosts doesn't work yet.如果您在 Mac 上使用 Docker,那么它只支持路由到本地主机地址,因为在 Mac 主机上桥接还不起作用。

Anyway, on MacOS with Docker for Mac (not the old Docker Toolbox), the following should be enough to get your started:无论如何,在带有 Docker for Mac(不是旧的 Docker 工具箱)的 MacOS 上,以下内容应该足以让您入门:

➜  ~ docker run --name=redis-devel --publish=6379:6379 --hostname=redis --restart=on-failure --detach redis:latest
6bfc6250cc505f82b56a405c44791f193ec5b53469f1625b289ef8a5d7d3b61e
➜  ~ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6bfc6250cc50        redis:latest        "docker-entrypoint.s…"   10 minutes ago      Up 10 minutes       0.0.0.0:6379->6379/tcp   redis-devel
➜  ~ redis-cli ping
PONG
➜  ~ 

找到发现的 redis 端口并使用它进行连接

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-server

I recomend you to use docker-compose https://docs.docker.com/compose/我建议你使用 docker-compose https://docs.docker.com/compose/

in you docker-compose.yml在你 docker-compose.yml

redis:                                                                          
  image: redis
ports:
  - "6379:6379" 

After this you can run:在此之后,您可以运行:

$ docker-compose run redis /bin/bash
$ redis-server
$ redis-cli ping

Should return: PONG应该返回:PONG

If you're using the Redis configuration example as a starting place (like I was when I encountered this issue), the combination of the bind 127.0.0.1 directive and protected-mode yes was not allowing for communication between Docker containers.如果您使用Redis 配置示例作为起点(就像我遇到此问题时的情况),则bind 127.0.0.1指令和protected-mode yes的组合不允许 Docker 容器之间进行通信。 For my purposes I commented out the bind directive and set the protected-mode directive to no -- but heed the provided warnings about not exposing Redis to the open internet.出于我的目的,我注释掉了bind指令并将protected-mode指令设置为no —— 但注意提供的关于不要将 Redis 暴露给开放互联网的警告。

It looks like a networking problem caused by the redis client, as your redis server looks fine.看起来像是由 redis 客户端引起的网络问题,因为您的 redis 服务器看起来不错。

Mind that the redis docker page is a little confusing.请注意,redis docker 页面有点令人困惑。 It says that to connect to your redis via redis-cli, you should type in:它说要通过 redis-cli 连接到你的 redis,你应该输入:

docker run -it --network some-network --rm redis redis-cli -h some-redis

This however creates another docker container which is basically the redis client.然而,这会创建另一个 docker 容器,它基本上是 redis 客户端。 It cannot connect to your redis server because they are not connected to the same network.它无法连接到您的 redis 服务器,因为它们未连接到同一网络。

You could do something a little different as following:你可以做一些不同的事情,如下所示:

docker network create some-network
docker run --name redis-server --network some-network -d redis
docker run -it --network some-network --rm redis redis-cli -h redis-server

The other option is to indeed map the port 6379 like you did, however it will require from you to use a redis client which should be downloaded to your own computer.另一种选择是确实像您一样映射端口 6379,但是它需要您使用应下载到您自己的计算机的 redis 客户端。

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

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