Cannot connect to Redis from Laravel Application
Asked Answered
I

4

8

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" ]
Ideogram answered 3/9, 2018 at 18:23 Comment(2)
How do you read your .env file? The error is too obviously saying your host is set to 127.0.0.1.Grimaldi
redis host is running on the container ports 0.0.0.0:6003->6379/tcp. DB_HOST=postgres is running without problem. What I'm missing here? @AlexKarshinIdeogram
S
13

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

Saltsman answered 7/9, 2018 at 11:28 Comment(0)
A
1

Set your REDIS_HOST to redis like this REDIS_HOST=redis. The reason is that you already built your docker file and specified redis as the name of your redis service

Adjacent answered 17/5, 2022 at 17:45 Comment(0)
T
0

Had Same issue...

Also updated following in redis.conf

bind 127.0.0.1 

To

bind redis

as redis is the existing host now

Tendon answered 1/9, 2022 at 7:47 Comment(0)
A
0

If you have defined networks in your docker-compose.yml also make sure that all services (both your Laravel as well as redis) are in the same network.

networks:
  internal:
    driver: bridge

Then:

  app:
    [cut]
    networks:
      - internal
  redis:
    image: redis:alpine
    volumes:
      - ./docker/volumes/redis:/data
    ports:
      - "6379:6379"
    networks:
      - internal

So that caching can talk to your app.

Alcoholize answered 18/6 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.