Getting a RedisException connection refused in file PhpRedisConnector.php

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Getting a RedisException: Connection Refused in Your Laravel Sail Project

Dealing with cryptic exceptions like RedisException: Connection refused when setting up a new environment, especially one involving Docker and services like Redis, can be incredibly frustrating. As a senior developer, I’ve seen this exact scenario countless times. It often signals a networking or service discovery issue rather than a simple typo in the configuration file.

If you are working with a Laravel project using Sail and Redis, the problem is almost certainly how your application container is attempting to reach the Redis service container within the Docker network.

Here is a comprehensive breakdown of why this happens and the exact steps you need to take to resolve the connection refusal error.


Understanding the "Connection Refused" Error

The error RedisException: Connection refused means that your Laravel application (running inside one Docker container) successfully initiated a connection request to the specified Redis host and port, but no service was actively listening on that specific network socket. It’s not a DNS failure or an authentication issue; it's a physical refusal from the target host—the server simply refused the connection attempt because nothing was there to accept it.

In the context of Docker Compose setups (like Sail), this usually points to one of three possibilities:

  1. Incorrect Hostname: The application is pointing to an IP address or hostname that doesn't resolve correctly within the Docker network.
  2. Service Not Running/Exposed: The Redis service container either failed to start, crashed immediately, or was not properly exposed on the network.
  3. Port Mapping Misconfiguration: The port mapping between the services is incorrect, meaning the application is connecting to an unused port instead of the actual Redis instance.

Troubleshooting Steps for Laravel & Sail Redis

Since you have already tried changing APP_URL and REDIS_HOST, let’s move into the infrastructure layer where these issues are most commonly found.

1. Verify docker-compose.yml Service Names

The most critical step is ensuring that the hostname used in your .env file exactly matches the service name defined in your docker-compose.yml. Docker Compose automatically sets up an internal network where services communicate using their service names as hostnames.

Review your docker-compose.yml:
Ensure your Redis service is correctly named (e.g., redis) and that you are referencing it by that name in your application configuration.

# Example snippet from docker-compose.yml
version: '3.8'
services:
  app:
    build: .
    # ... other settings
    depends_on:
      - redis # Ensure app waits for redis to start
  redis: # <-- This is the crucial hostname
    image: redis:alpine
    ports:
      - "6379:6379"

2. Validate the .env Configuration

If your Redis service is named redis in docker-compose, then your application should be configured to connect to redis.

Correct Environment Setup:

# .env file example
REDIS_HOST=redis  # Must match the service name in docker-compose.yml
REDIS_PORT=6379   # Default Redis port

If you are using a custom setup where you need to connect via the host machine's network (which is generally discouraged in Docker Compose), you might need to use host.docker.internal, but for internal container-to-container communication, the service name is the standard practice.

3. Check Container Health and Logs

If configuration checks fail, look at what the containers are actually doing. Use Docker commands to diagnose the health of your services:

  • Check Status: Run docker-compose ps to see if both app and redis are listed as "Up".
  • Examine Redis Logs: If Redis is refusing connections, it might be crashing immediately upon startup. Check the logs for any errors:
    docker-compose logs redis
    

If the Redis container starts successfully but you still get "Connection refused," it confirms the issue lies in the networking layer between the two containers, meaning Step 1 and Step 2 are almost certainly the source of the problem.

Conclusion: Adhering to Laravel Best Practices

When working with modern PHP frameworks like Laravel, embracing the containerization paradigm is essential. Relying on service discovery mechanisms provided by Docker Compose—using service names as hostnames—is a fundamental best practice. This approach ensures that your application remains decoupled and resilient, which aligns perfectly with the principles outlined by platforms like laravelcompany.com.

By meticulously aligning your .env configuration with your docker-compose.yml structure, you move from guessing about network issues to implementing robust, predictable infrastructure. Debugging these connection problems is a core skill for any senior developer, and mastering Docker networking environments is key to successful deployment.