Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose: redis connection refused between containers

I am trying to setup a docker-compose file that is intended to replace a single Docker container solution that runs several processes (RQ worker, RQ dashboard and a Flask application) with Supervisor.

The host system is a Debian 8 Linux and my docker-compose.yml looks like this (I deleted all other entries to reduce error sources):

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

"rq-worker1" is a Python RQ worker, trying to connect to redis via localhost and port 6379, but it fails to establish a connection:

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

The output of docker ps looks like this:

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

I tried everything from running the RQ worker against the local IPs 0.0.0.0 / 127.0.0.1 and even localhost. Other solutions posted on Stackoverflow didn't work for me, too (docker-compose: connection refused between containers, but service accessible from host e.g.).

And this is my docker info output:

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

Does anyone have an idea why the connect between the two containers doesn't work?

like image 557
henning82 Avatar asked Dec 23 '16 13:12

henning82


1 Answers

In your code localhost from rq-worker1 is rq-worker1 itself, not redis and you can't reach redis:6379 by connect to localhost from rq-worker1. But by default redis and rq-worker1 are in the same network and you can use service name as a domain name in that network. It means, that you can connect to redis service from rq-worker1 using redis as a domain name, for instance: client.connect(("redis", 6379))

You should replace localhost with redis in config of rq-worker1.

like image 176
Gennady Kandaurov Avatar answered Nov 02 '22 15:11

Gennady Kandaurov