docker-compose:容器之间拒绝redis连接

时间:2016-12-23 13:57:52

标签: python docker redis containers

我正在尝试设置一个docker-compose文件,该文件旨在用Supervisor替换运行多个进程(RQ worker,RQ dashboard和Flask应用程序)的单个Docker容器解决方案。

主机系统是Debian 8 Linux,我的docker-compose.yml看起来像这样(我删除了所有其他条目以减少错误来源):

version: '2'
  services:
    redis:
      image: redis:latest
    rq-worker1:
      build: .
      command: /usr/local/bin/rqworker boo-uploads
      depends_on:
        - redis

" RQ-worker1"是一个Python RQ worker,试图通过localhost和端口6379连接到redis,但它无法建立连接:

redis_1       | 1:M 23 Dec 13:06:26.285 * The server is now ready to accept connections on port 6379
rq-worker1_1  | [2016-12-23 13:06] DEBUG: worker: Registering birth of worker d5cb16062fc0.1
rq-worker1_1  | Error 111 connecting to localhost:6379. Connection refused.
galileoqueue_rq-worker1_1 exited with code 1

docker ps的输出如下所示:

CONTAINER ID        IMAGE               COMMAND                      CREATED             STATUS              PORTS               NAMES
36cac91670d2        redis:latest        "docker-entrypoint.sh"   14 minutes ago      Up About a minute   6379/tcp                galileoqueue_redis_1

我尝试了从运行RQ worker到本地IP 0.0.0.0 / 127.0.0.1甚至localhost的所有内容。 Stackoverflow上发布的其他解决方案也不适合我(docker-compose: connection refused between containers, but service accessible from host例如。)。

这是我的docker info输出:

Containers: 25
  Running: 1
  Paused: 0
  Stopped: 24
Images: 485
Server Version: 1.12.5
Storage Driver: aufs
  Root Dir: /var/lib/docker/aufs
  Backing Filesystem: extfs
  Dirs: 436
  Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
  Volume: local
  Network: null bridge host overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options:
Kernel Version: 3.16.0-4-amd64
Operating System: Debian GNU/Linux 8 (jessie)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 13.61 GiB
Name: gp-pc-201
ID: LBGV:K26G:UXXI:BWRH:OYVE:OQTA:N7LQ:I4DV:BTNH:FZEW:7XDD:WOCU

有没有人知道为什么两个容器之间的连接不起作用?

1 个答案:

答案 0 :(得分:19)

来自localhost的代码rq-worker1本身为rq-worker1,而不是redis,您无法通过{{1}连接到redis:6379来自localhost。但默认情况下,rq-worker1redis位于同一network,您可以将服务名称用作该网络中的域名。 这意味着,您可以使用rq-worker1作为域名从redis连接到rq-worker1服务,例如:redis

您应该在client.connect(("redis", 6379))的配置中将localhost替换为redis