22

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?

1 Answer 1

36

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That solved the connection error. I used the host parameter for all Python commands to connect to "redis" and it works perfectly.
I was having a similar problem, trying to access the redis-server from a django web service. What solved the problem for me was to alter my docker-compose.yml file to expose port 6379 in my spec for the redis_db service (I used this instead of redis since that's also the image name) and then in my "web" service, I added "links: /n - redis_db" (note the new line in there). After that I was able to connect to redis from inside my web service.
The idea expressed here helped me in solving inter container communication issue with the error: Error 99 connecting to localhost:6379. Cannot assign requested address

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.