Search code examples
redistraefik

Redis connection refused when using traefik


I'm having a hard time trying to configure one redis container for all my applications using traefik. This is my configuration:

1 - Docker compose for Traefik and Redis: version: '2'

services:
  proxy:
    container_name: traefik
    image: traefik:1.3.6-alpine
    command: --docker
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
    networks:
      - proxy
    labels:
      - traefik.frontend.rule=Host:monitor.company.dev
      - traefik.port=8080

redis:
    container_name: main_redis
    image: redis:3.2
    restart: always
    volumes:
      - ./data/redis:/data
    networks:
      - proxy
    labels:
      - traefik.backend=main-redis
      - traefik.default.protocol=http
      - traefik.frontend.rule=Host:main-redis.company.dev
      - traefik.docker.network=proxy
      - traefik.port=6379

networks:
  proxy:
    external: true

2 - Docker compose for my PHP Application.

version: '2'

services:
  ...


  php:
    container_name: myapp_php
    build: ./docker/php # php:7.1-fpm base image

    networks:
      - internal
      - proxy
    labels:
      - traefik.enable=false
      - traefik.docker.network=proxy
    expose:
      - 9000

networks:
  proxy:
    external: true
  internal:
    external: false

I tried to connect my php application to main-redis.company.dev on both ports 6379 and 80 but I get a Redis::connect(): connect() failed: Connection refused message.

I also changed these stuff in my redis.conf: Commented the line with bind 127.0.0.1 And changed protected-mode to no

My docker containers are staying in the same network, so I think it should work. Anyone knows why am I having this problem?


Solution

  • First off, remove the traefik labels from your redis service definition, traefik is currently (Nov 2017) a HTTP proxy, so you can't expose the endpoint like that

    See here:

    https://github.com/containous/traefik/issues/10

    https://github.com/containous/traefik/issues/1611

    Then to connect the php service to the redis service, that looks like you are trying to do that within the same docker instance (rather than externally)

    Instead of main-redis.company.dev:6379, it should be like on of these:

    • redis:6379
    • main_redis:6379
    • %PROJECT_NAME%_redis:6379

    depending upon how you are deploying the container