DEV Community

Chathura Rathnayaka
Chathura Rathnayaka

Posted on Originally published at backendrecipes.com

Fixing Laravel Redis Connection Refused (127.0.0.1:6379) in Docker & Linux

Originally published on BackendRecipes.

When Laravel throws Predis\Connection\ConnectionException: Connection refused [tcp://127.0.0.1:6379] or RedisException: Connection refused, your PHP worker is attempting to open a TCP socket to Redis on 127.0.0.1:6379 and nothing is listening on that interface.


1. Fix the Docker Compose Loopback Trap (127.0.0.1 vs redis)

Inside a multi-container Docker setup, 127.0.0.1 inside the app or php container points to the PHP container itself—not your Redis container.

Update your Laravel .env file to use the Redis service name defined in docker-compose.yml:

REDIS_CLIENT=phpredis
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
Enter fullscreen mode Exit fullscreen mode

Ensure both services share the same Docker bridge network in docker-compose.yml:

services:
  app:
    build: .
    depends_on:
      - redis
    networks:
      - laravel

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    networks:
      - laravel

networks:
  laravel:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Audit Your Container Config: Running multi-container Laravel stacks? Check your docker-compose.yml and .env files locally in your browser using OpsCheck to catch exposed ports, default credentials, and container misconfigurations before deploying.


2. Verify Redis Service & Port Binding on Ubuntu / Linux

If you are running on a bare-metal VPS or WSL2 without Docker, confirm the redis-server daemon is active and bound to 127.0.0.1:

sudo systemctl status redis-server
sudo ss -tulpn | grep 6379
Enter fullscreen mode Exit fullscreen mode

If Redis is stopped or masked, start and enable it on boot:

sudo systemctl enable --now redis-server
Enter fullscreen mode Exit fullscreen mode

3. The config:clear Catch-22 When Redis Is Down

When CACHE_STORE=redis or SESSION_DRIVER=redis is active, running php artisan cache:clear after editing .env can itself fail with Connection refused [tcp://127.0.0.1:6379] because Artisan boots the configured cache store before clearing it.

Bypass Laravel's container to verify whether the block is network-level or PHP-extension-level:

# 1. Verify Redis is actually listening on the target interface
redis-cli -h 127.0.0.1 -p 6379 ping
# Expected: PONG

# 2. Test raw PHP socket reachability without booting Laravel
php -r '$s = @fsockopen("127.0.0.1", 6379, $errno, $errstr, 2); var_dump($s ? "CONNECTED" : "$errno: $errstr");'
Enter fullscreen mode Exit fullscreen mode

If redis-cli returns PONG on your host machine but php -r inside your Docker container returns 111: Connection refused, your app container is trying to reach its own loopback interface (127.0.0.1) instead of your Redis service hostname (REDIS_HOST=redis). Delete bootstrap/cache/config.php manually if Artisan refuses to boot, then re-run php artisan config:cache.


Related Backend Troubleshooting Guides

Top comments (0)