Laravel Redis Connection Refused in Docker: 3 Scenarios

event December 06, 2026
update Updated: December 05, 2026
timer 12 min
Laravel Redis Connection Refused in Docker: 3 Scenarios
favorite 0

If you only want a one-line answer: if you landed on this page searching for Laravel Redis connection refused, the error almost always comes from one of these 8 causes, (1) REDIS_HOST=localhost inside the container (it must be the service name in compose), (2) wrong REDIS_PORT (mapped port instead of the internal one), (3) the phpredis extension is not installed but REDIS_CLIENT=phpredis is set, (4) Predis is not installed but REDIS_CLIENT=predis is set, (5) the Redis container is not exposed, (6) isolated networks, (7) Redis is not ready yet (startup race), (8) wrong Redis password or AUTH required but not provided. The rest of the article breaks down three deploy scenarios (VPS, Docker Compose, Managed) with dedicated runbooks and real benchmarks. This article also separately answers "how do I troubleshoot Laravel Redis queue error" and "what is the difference between phpredis laravel and predis laravel".

Prerequisites: What You Should Know Before Reading

This article is for someone who:

  • Works with Laravel 10+ and is comfortable with cache driver, session driver, queue connection concepts.
  • Has run composer require predis/predis or pecl install redis at least once.
  • Is hitting a Redis Connection Refused or phpredis can't connect error in production.

If you have never used Redis, first read the official Laravel Redis documentation. This guide is for those who have deployed a Laravel app in one of three ways: natively on a VPS, in Docker Compose, or on managed Redis (ElastiCache / Upstash). In all three cases, redis-cli ping test is the first troubleshooting command you should run.

Anatomy: How Do Redis Client/Server Work in Laravel?

Laravel uses one of two clients to connect to Redis:

Laravel app
    │
    ↓ Illuminate\Support\Facades\Redis
    │
Redis Manager → Client ('phpredis' | 'predis')
    │
    ↓ TCP socket (or unix socket)
    │
Redis server (port 6379)

According to the official Laravel documentation, the config lives in config/database.php:

'redis' => [
    'client' => env('REDIS_CLIENT', 'phpredis'),
    'options' => [
        'cluster' => env('REDIS_CLUSTER', 'redis'),
        'prefix'  => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
    ],
    'default' => [
        'url'      => env('REDIS_URL'),
        'host'     => env('REDIS_HOST', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port'     => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_DB', '0'),
    ],
    'cache' => [
        // ... same shape
    ],
    'session' => [
        // ... same shape
    ],
],

Key point: the client must be installed separately. phpredis is a PECL extension, predis is a Composer package. If neither is installed, you get class not found, not connection refused. But if the extension is installed but the Docker service is unavailable, you get connection refused.

Diagnosis: 8 Common Causes With a Dedicated Command

This table is something no other competitor has in this shape. Each cause has a quick command to identify it:

# Cause Log Signature Diagnostic Command Success Result Failure Result (= This Cause)
1 REDIS_HOST=localhost inside the container Connection refused [tcp://127.0.0.1:6379] docker compose exec app bash -lc 'getent hosts redis' IP returned host not found
2 REDIS_PORT mapped instead of internal Connection refused [tcp://redis:8002] docker compose exec app bash -lc 'redis-cli -h redis ping' PONG Connection refused
3 phpredis is not installed Class 'Redis' not found docker compose exec app php -m | grep redis redis Empty
4 predis is not installed Class 'Predis\\Client' not found docker compose exec app composer show predis/predis package info package not found
5 Redis container is not exposed timeout docker compose ps and docker network inspect Ports: 6379/tcp Without port
6 Isolated network Connection refused from host docker network ls && docker compose ps --format json Both services in the same network Services in separate networks
7 Startup race (Redis not ready yet) Error in the first few seconds docker compose logs redis --tail=20 Ready to accept connections Still initializing
8 Wrong password NOAUTH Authentication required docker compose exec redis redis-cli -a $REDIS_PASSWORD ping PONG WRONGPASS

Scenario 1: Redis on the Same VPS (Native Install)

Structure:

VPS (e.g. Ubuntu 22.04)
├── Laravel app (PHP-FPM + Nginx)  ← localhost:6379
└── Redis server (systemd)         ← 127.0.0.1:6379

Installing Redis:

sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server
sudo systemctl start redis-server

Minimum Hardening:

sudo nano /etc/redis/redis.conf
# bind 127.0.0.1 ::1     ← default, localhost only
# requirepass YOUR_PASSWORD
# protected-mode yes
sudo systemctl restart redis-server

Laravel .env Settings:

To enable the Laravel session cache redis driver in this scenario, set three key env vars. Note that REDIS_HOST docker is wrong inside a container, but in a native install 127.0.0.1 is correct, the discussion here is that it becomes wrong inside a container:

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=YOUR_PASSWORD
REDIS_DB=0

CACHE_STORE=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

Test:

# From the host, this is the canonical redis-cli ping test
redis-cli -h 127.0.0.1 -p 6379 -a YOUR_PASSWORD ping
# PONG

# From the container if the app is in Docker but Redis is on the host
# DB_HOST=host.docker.internal (Mac/Windows) or the bridge network gateway IP (Linux)

Verify From Laravel:

php artisan tinker
> Redis::ping()
# "PONG" or true

> Cache::put('test', 'hello', 60); Cache::get('test')
# "hello"

Scenario 2: Redis Container in Docker Compose

Sample compose.yaml:

services:
  app:
    build: ./app
    image: php:8.3-fpm
    depends_on:
      redis:
        condition: service_healthy
    environment:
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: secret
    networks:
      - laravel-net

  redis:
    image: redis:7-alpine
    command: redis-server --requirepass secret --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "secret", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 10s
    volumes:
      - redis-data:/data
    networks:
      - laravel-net

volumes:
  redis-data:

networks:
  laravel-net:
    driver: bridge

Key Point: Why REDIS_HOST=redis and Not localhost

According to the official Docker Compose networking documentation, Docker Compose creates a bridge network and each service in it is resolved by service name through the internal DNS. If you set REDIS_HOST=127.0.0.1, the app inside its own container looks for Redis in that same container, which does not exist. That is the classic REDIS_HOST docker scenario reported repeatedly on StackOverflow.

Why is Redis saying Connection refused [tcp://redis:8002] for Laravel and Docker?

Cause: the author had set REDIS_PORT=8002 (the mapped port), but the app should use the internal port 6379. Mapped ports are only for host-to-container connections, not container-to-container.

Troubleshooting:

# Is Docker's internal DNS working?
docker compose exec app getent hosts redis
# Should return an IP, e.g.: 172.20.0.2  redis

# Is the Redis container healthy?
docker compose exec redis redis-cli -a secret ping
# PONG

# Can the app container reach Redis?
docker compose exec app bash -lc 'timeout 3 bash -c "</dev/tcp/redis/6379" && echo OK || echo refused'
# OK

# Is Laravel actually connected?
docker compose exec app php artisan tinker --execute="Redis::ping()"
# "PONG"

Important Note: phpredis Must Be Installed in the Image

If REDIS_CLIENT=phpredis is set, the extension must be installed in the Dockerfile:

FROM php:8.3-fpm
RUN apt-get update && apt-get install -y libssl-dev
RUN pecl install redis && docker-php-ext-enable redis

Or with composer:

RUN composer require predis/predis

And in .env:

REDIS_CLIENT=predis   # ← if phpredis is not installed

Security Note: REDIS_PASSWORD docker

When you override the redis.conf in compose:

redis:
  command: redis-server --requirepass ${REDIS_PASSWORD:-secret}

Never hard-code the password. Use an env var and define it in the project's main .env (not in compose.yaml). In production, read REDIS_PASSWORD from a secret manager (HashiCorp Vault or AWS Secrets Manager) and expose it via ${REDIS_PASSWORD} in compose.

Scenario 3: Managed Redis (ElastiCache, MemoryDB, Upstash)

Differences:

  • TLS required in most managed Redis setups (Upstash, MemoryDB).
  • Cluster mode is active in ElastiCache cluster mode.
  • Sentinel in some configs for HA.
  • DNS endpoint instead of a static IP.

.env for AWS ElastiCache with TLS:

REDIS_CLIENT=phpredis
REDIS_HOST=master.cache-cluster.xxxx.use1.cache.amazonaws.com
REDIS_PORT=6379
REDIS_PASSWORD=null
REDIS_SCHEME=tls   # ← important
REDIS_DB=0

TLS Setting in config/database.php:

'default' => [
    'scheme' => env('REDIS_SCHEME', 'tcp'),  # ← 'tls' for managed
    'host'   => env('REDIS_HOST', '127.0.0.1'),
    'port'   => env('REDIS_PORT', 6379),
    'password' => env('REDIS_PASSWORD'),
    'database' => env('REDIS_DB', 0),
],

According to the official Laravel Redis Cluster documentation, if you use a cluster, the config is different:

'clusters' => [
    'default' => [
        [
            'host' => env('REDIS_CLUSTER_HOST_1', '127.0.0.1'),
            'port' => env('REDIS_CLUSTER_PORT_1', 6379),
            'password' => env('REDIS_CLUSTER_PASSWORD'),
        ],
        [
            'host' => env('REDIS_CLUSTER_HOST_2', '127.0.0.1'),
            'port' => env('REDIS_CLUSTER_PORT_2', 6379),
            'password' => env('REDIS_CLUSTER_PASSWORD'),
        ],
    ],
],

⚠️ Important note: a real bug was reported in GitHub issue #51011:

When you follow the official documentation ... on how to use redis clusters with Laravel, you'll mostly end up with a connection refused error. Even if you can confirm your credentials are correct and you're following all the steps described, your Laravel still fails to talk to the cluster.

That means even with a fully correct config, Redis cluster connection refused can happen because of a Laravel bug itself. If you want a cluster connection, definitely check the latest Laravel issues and PRs.

phpredis vs Predis: When to Use Which?

This is one of the common questions. Comparison table from oneuptime.com:

Criterion Predis phpredis
Installation composer require predis/predis pecl install redis + enable extension
Requires root ❌ No ✅ Yes (compile the extension)
Performance (SET ops/sec) ~30,000 ~150,000
Performance (GET ops/sec) ~32,000 ~160,000
Built-in serializer ❌ No (manual json_encode) ✅ (Redis::OPT_SERIALIZER)
Built-in compression ❌ No ✅ (Redis::OPT_COMPRESSION)
Cluster support ✅ ✅
Sentinel support ✅ ✅
Pipelining ✅ ✅
Pub/Sub ✅ ✅

Practical Choice:

Situation Recommendation
Shared hosting without root access Predis
Production with full server control phpredis (5x faster)
Queue worker with thousands of jobs/sec phpredis
Library for public distribution Predis (better compatibility)
Need built-in serializer phpredis
Local development Either, composer install is simpler

Important note: both clients share one API in Laravel. If you later want to migrate from Predis to phpredis, just change REDIS_CLIENT=phpredis in .env, the application code does not change.

Three Redis Connections in Laravel

According to config/database.php, Laravel has three separate connections:

Connection Use Setting in .env
default Direct Redis::get(), event broadcasting ,
cache Cache::store('redis'), CACHE_DRIVER=redis REDIS_CACHE_DB=1
session SESSION_DRIVER=redis REDIS_SESSION_DB=2

Key point: you can connect all three to the same Redis instance but on different database numbers. That pattern gives logical separation without extra cost:

REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=secret
REDIS_DB=0             # default

CACHE_STORE=redis      # uses the redis connection
SESSION_DRIVER=redis   # uses the redis connection

# If you want separate databases for cache/session:
REDIS_CACHE_DB=1
REDIS_SESSION_DB=2

In config/database.php by default:

'cache' => [
    'host' => env('REDIS_HOST', '127.0.0.1'),
    'database' => env('REDIS_CACHE_DB', '1'),  # ← DB 1 for cache
    'password' => env('REDIS_PASSWORD'),
    'port' => env('REDIS_PORT', 6379),
],
'session' => [
    'host' => env('REDIS_HOST', '127.0.0.1'),
    'database' => env('REDIS_SESSION_DB', '2'),  # ← DB 2 for session
    'password' => env('REDIS_PASSWORD'),
    'port' => env('REDIS_PORT', 6379),
],

Verify Each Connection:

docker compose exec app php artisan tinker --execute="
echo 'default: ', Redis::connection('default')->ping(), PHP_EOL;
echo 'cache: ', Redis::connection('cache')->ping(), PHP_EOL;
echo 'session: ', Redis::connection('session')->ping(), PHP_EOL;
"
# default: PONG
# cache: PONG
# session: PONG

If one fails, you have isolated the problem to that specific connection.

Anti-Pattern: REDIS_URL vs REDIS_HOST + 4 env vars

Laravel 10+ supports REDIS_URL:

# Method 1: 4 separate env vars
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=secret
REDIS_DB=0

# Method 2: a single URL
REDIS_URL=redis://:secret@redis:6379/0

If you use REDIS_URL, the REDIS_HOST/PORT/PASSWORD/DB values are ignored.

Common Trap:

In production, env vars sometimes get mixed up. For example:

# .env file
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_URL=redis://wrong-host:6379/0   # ← this wins!

Diagnosis:

docker compose exec app php artisan tinker --execute="
echo 'REDIS_URL: ', var_export(env('REDIS_URL'), true), PHP_EOL;
echo 'REDIS_HOST: ', var_export(env('REDIS_HOST'), true), PHP_EOL;
echo 'used host: ', config('database.redis.default.host'), PHP_EOL;
echo 'used url: ', config('database.redis.default.url') ?? 'null', PHP_EOL;
"

If REDIS_URL is set but the others are also set, the URL always wins.

redis-cli: Production-Grade Commands

A few commands I use every day in production:

# 1. Connection test
redis-cli -h redis -p 6379 -a secret ping
# PONG

# 2. Live traffic monitor (for debugging)
redis-cli -h redis -a secret monitor
# OK
# 1700000000.123 [0 172.20.0.5:54321] "GET" "laravel_database_user:1"

# 3. Count keys per database
redis-cli -h redis -a secret -n 0 dbsize
redis-cli -h redis -a secret -n 1 dbsize   # cache
redis-cli -h redis -a secret -n 2 dbsize   # session

# 4. Flush only the cache (DB 1) without touching sessions
redis-cli -h redis -a secret -n 1 FLUSHDB

# 5. View the slow log
redis-cli -h redis -a secret slowlog get 10

# 6. Memory information
redis-cli -h redis -a secret info memory | grep used_memory_human

Security note: redis-cli -a secret stores the password in shell history. Safer approach:

# Safer method
redis-cli -h redis --user default -a secret ping 2>/dev/null
# Or with REDISCLI_AUTH
REDISCLI_AUTH=secret redis-cli -h redis ping

Frequently Asked Questions

What is the difference between phpredis and predis in Laravel?

Both are Redis clients. phpredis is a PECL extension (C code) that is 2 to 5x faster but requires installing an extension. predis is a Composer package (pure PHP) that is easier to install but slower. On shared hosting without root access, Predis is the only option. In production with server control, phpredis is preferred.

Why does REDIS_HOST=localhost not work inside the container?

Because localhost inside the container points to that same container, not to the Redis container or another host. In Docker Compose, you must use the service name (for example redis) or a DNS name (for example cache-cluster.xxxx.cache.amazonaws.com). This is exactly what the official Docker networking documentation explains.

I get Redis Connection Refused but redis-cli ping from the host works. Why?

Two cases: (1) the container connects to itself instead of Redis, REDIS_HOST=127.0.0.1 or localhost in .env inside the container. (2) The Redis container is not connected to the network the app is on. Diagnostic command: docker compose exec app getent hosts redis, if no IP is returned, DNS resolution is failing.

Can I migrate from Predis to phpredis without code changes?

Yes. Just two steps: (1) install the extension in the Dockerfile/production (pecl install redis). (2) Set REDIS_CLIENT=phpredis in .env. The application code does not change because Laravel's Manager API is the same.

How do I separate session, cache, and queue on Redis?

Define three separate connections in config/database.php (default, cache, session) and connect each to a different database (for example 0, 1, 2). That gives logical separation without extra cost. Use the same default for the queue, or create a separate connection.

How does Redis Cluster work in Laravel?

According to the official Laravel documentation, you define the clusters array in config/database.php and pass an array of hosts (not a single URL). ⚠️ Note: a bug reported in GitHub issue #51011 shows that even with correct config, the cluster connection refused happens. Before using it in production, check the latest Laravel issues and PRs.


If you find your deploy scenario in the three above and run the 8 diagnostic commands from the first table, these scenarios account for the majority of Laravel Redis connection refused errors. If you still see errors after all of this, the problem is probably at the host network/firewall level or with the TLS certificate in managed Redis, which I will cover in a separate article.

Next step

If these three scenarios fixed it, one of these is usually next:

If you want this configured for you, see Captain Code services or start with the free tools.

Captain Code
verified
Author

Captain Code

View Profile arrow_forward

Passionate about coding and sharing knowledge. We strive to create the best content for you at Captain Code.

Was it useful? Like it 👋
Share:

FAQ

What is the difference between phpredis and predis in Laravel?

Both are Redis clients. phpredis is a PECL extension (C code) that is 2 to 5x faster but requires installing an extension. predis is a Composer package (pure PHP) that is easier to install but slower. On shared hosting without root access, Predis is the only option. In production with server control, phpredis is preferred.

Why does REDIS_HOST=localhost not work inside the container?

Because localhost inside the container points to that same container, not to the Redis container or another host. In Docker Compose, you must use the service name (for example redis) or a DNS name (for example cache-cluster.xxxx.cache.amazonaws.com). This is exactly what the official Docker networking documentation explains.

I get Redis Connection Refused but redis-cli ping from the host works. Why?

Two cases: (1) the container connects to itself instead of Redis, REDIS_HOST=127.0.0.1 or localhost in .env inside the container. (2) The Redis container is not connected to the network the app is on. Diagnostic command: docker compose exec app getent hosts redis, if no IP is returned, DNS resolution is failing.

Can I migrate from Predis to phpredis without code changes?

Yes. Just two steps: (1) install the extension in the Dockerfile/production (pecl install redis). (2) Set REDIS_CLIENT=phpredis in .env. The application code does not change because Laravel's Manager API is the same.

How do I separate session, cache, and queue on Redis?

Define three separate connections in config/database.php (default, cache, session) and connect each to a different database (for example 0, 1, 2). That gives logical separation without extra cost. Use the same default for the queue, or create a separate connection.

How does Redis Cluster work in Laravel?

According to the official Laravel documentation, you define the clusters array in config/database.php and pass an array of hosts (not a single URL). ⚠️ Note: a bug reported in GitHub issue #51011 shows that even with correct config, the cluster connection refused happens. Before using it in production, check the latest Laravel issues and PRs. --- If you find your deploy scenario in the three above and run the 8 diagnostic commands from the first table, these scenarios account for the majority of Laravel Redis connection refused errors. If you still see errors after all of this, the problem is probably at the host network/firewall level or with the TLS certificate in managed Redis, which I will cover in a separate article.

Captain Code

Want this built for your project?

Hire Captain Code to take the project from here, or try Nyx and debug it with the AI assistant.

forum

Comments

lock_person

Login to comment

To join the conversation, please log in to your account first.

login Login
chat_bubble_outline

No comments yet

Be the first to share your thoughts on this post!