Search code examples
dockerredis

Cannot connect to Redis from Laravel Application


I have to configure Redis with Socketio in my Laravel application. However, what ever I have tried so far, I get the same error:

Connection refused [tcp://127.0.0.1:6379] i

I can go to the container with docker exec -it id sh and when I ping the server I get the PONG message. Client is already 'predis' in my database.php file and package also installed.

.env

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

docker-compose.yml

 version: "2"
    services:
      api:
        build: .
        ports:
          - 9000:9000
        volumes:
          - .:/app
          - /app/vendor
        depends_on:
          - postgres
          - redis
        environment:
          DATABASE_URL: postgres://xx@postgres/xx
      postgres:
        image: postgres:latest
        environment:
          POSTGRES_USER: xx
          POSTGRES_DB: xx
          POSTGRES_PASSWORD: xx
        volumes:
          - .Data:/var/lib/postgresql/data
        ports:
          - 3306:5432
      redis:
        build: ./Redis/
        ports:
          - 6003:6379
        volumes:
          - ../RedisData/data:/data
        command: redis-server --appendonly yes

Dockerfile (redis)

FROM redis:alpine

COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

Solution

  • The error is saying it can connect to 127.0.0.1 on port 6379. So make sure the host and port is ok:

    • host 127.0.0.1 is ok: this work if you run the php on the same host than redis, or if you run php on Docker host machine, but in this case, the port will be 6003

    • port 6379 is ok: host is not good, you must specify the Docker container hostname: redis

    • make sure configuration cache is ok