我正在尝试设置一个docker-compose文件,该文件旨在使用Supervisor替换运行多个进程(RQ worker,RQ仪表板和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,试图通过本地主机和端口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

我尝试了针对本地IP 0.0.0.0/127.0.0.1甚至本地主机运行RQ工作器的所有操作。在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

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

最佳答案

在您的代码中,来自localhostrq-worker1本身就是rq-worker1,而不是redis,并且您无法通过从redis:6379连接到localhost来达到rq-worker1。但是默认情况下,redisrq-worker1在同一network中,您可以在该网络中将服务名称用作域名。
这意味着您可以使用redis作为域名从rq-worker1连接到redis服务,例如:client.connect(("redis", 6379))
您应该在localhost的配置中将redis替换为rq-worker1

关于python - docker-compose : redis connection refused between containers,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41302791/

10-12 22:42