Connection refused when Gateway and Redis in separate Docker containers


#1

I have 2 containers, one for the gateway and one for Redis, as you can see in the docker-compose.yml file:

tyk_gateway-test:
    build: .
    ports:
      - "8101:8080"
    links:
      - redis
    volumes:
      - ./tyk.conf:/opt/tyk-gateway/tyk.conf
      - ./apps:/opt/tyk-gateway/apps
    extra_hosts:
      - "host:192.168.99.1"

redisdata:
    image: busybox
    volumes:
      - /var/lib/redis

redis:
    image: redis:latest
    ports:
      - "8102:6379"
    volumes_from:
      - redisdata

When I lift the containers, I get the error:

"Connection to Redis failed, reconnect in 10s" err="dial tcp 172.17.0.3:8102: getsockopt: connection refused"

Here’s the configuration of Tyk (tyk.conf):

{
  "listen_port": 8080,
  "secret": "<some-secret-that-I-have-hidden-for-the-post>",
  "template_path": "/opt/tyk-gateway/templates",
  "tyk_js_path": "/opt/tyk-gateway/js/tyk.js",
  "use_db_app_configs": false,
  "app_path": "/opt/tyk-gateway/apps",
  "middleware_path": "/opt/tyk-gateway/middleware",
  "storage": {
    "type": "redis",
    "host": "redis",
    "port": 8102,
    "username": "",
    "password": "",
    "database": "2",
    "optimisation_max_idle": 500,
    "optimisation_max_active": 800
  },
...

I am sure the problem is from the host/IP but I don’t know how to approach the problem. I guess it’s because the gateway is trying to find the Redis instance within its container, instead of going outside, to the host machine, and then connecting to the Redis container…

By looking at the /etc/hosts file of the gateway container I got this info:

So, it sets up Redis to be on that 172.17.0.3 IP… How can I force the gateway too look towards the host machine IP instead?

Any ideas?


#2

Have you configured redis to allow connections from outisde? Redis usually binds to localhost


#3

I updated my OP with a screenshot of the gateway’s container’s hosts file. Maybe that will give you some insight?
And the only configuration I’ve got for Redis is the one you can see in the docker-compose.yml file. :slight_smile:


#4

If I set the extra_hosts of the gateway container as follows:

extra_hosts:
      - "tykgatewaytest_redis_1 redis:192.168.99.1"

Then it seems to connect to Redis successfully, but then I cannot access the Tyk container anymore…


#5

You just need to make sure the redis container is linked to the gateway and dashboard containers.

If you look at your tyk.conf, it says it expects the redis DB to be on the host redis, in your hosts file, I can see a redis listed.

I think if you have more than one image, you might want to use a docker network instead of direct links, take a look at our docker quickstart