9

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" ]
2
  • How do you read your .env file? The error is too obviously saying your host is set to 127.0.0.1. Commented Sep 3, 2018 at 19:54
  • 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? @AlexKarshin Commented Sep 4, 2018 at 21:22

4 Answers 4

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

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

Comments

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

Comments

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

Comments

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.

Comments

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.