Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CarbonNanotubes's avatar

Redis: Connection Refused

I have a project based on this github repo https://github.com/aschmelyun/docker-compose-laravel

Everything is in standard configuration but when I attempt to run a redis command with the redis facade I get connection refused.

I have also tried debugging in tinker. The repo has a artisan container so I run tinker from that which is a part of my docker network

Psy Shell v0.11.7 (PHP 8.1.8 — cli) by Justin Hileman
>>> \Illuminate\Support\Facades\Redis::get('test');
RedisException with message 'Connection refused'

I am able to connect to the redis server and run commands from my desktop redis client so I know that the server itself is working fine.

Any ideas as to why this isn't working?

0 likes
9 replies
Sinnbeck's avatar

What is the redis host set to in your .env file? It needs to be the name of the service that is running redis. In this case redis

REDIS_HOST=redis
1 like
Sinnbeck's avatar

@CarbonNanotubes Check if the container is running by running docker ps and find the redis container. It should be running a long time :) Just in case there is a redis service on your pc, that makes it seem like its working locally.

Sinnbeck's avatar

And try clearing cache

php artisan config:clear
CarbonNanotubes's avatar
h1-http_redis_1        docker-entrypoint.sh redis ...   Up (healthy)   0.0.0.0:6379->6379/tcp,:::6379->6379/tcp

To ensure I am connecting to the correct server I brought my containers down and mapped port 6600 to redis

redis:
    image: redis:alpine
    restart: unless-stopped
    ports:
      - 6600:6379
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      retries: 3
      timeout: 5s
    networks:
      - mynetwork

I was able to use my desktop client to connect to the server on port 6600

CarbonNanotubes's avatar

I also tried creating a route and just calling redis without the laravel facade and it connects

Route::get('redis', function () {
    $redis = new \Redis();
    try {
        $redis->connect('redis', 6379);
    } catch (\Exception $e) {
        var_dump($e->getMessage());
        die;
    }
});

So the issue is something to do with Laravel's Redis configuration

CarbonNanotubes's avatar
Level 19

I figured out the issue. Long story short I had an old copy of the project within my vscode workspace and I was editing the wrong .env file. I noticed this opened the right one and the host was not set correctly.

Imma just gonna go give myself a toilet swirly for being such an idiot, then I'll come back and delete that old code lol

1 like
de_lvin's avatar

@CarbonNanotubes hie , I am also having a similiar same issue. I am trying to run an app that i pulled from github. Building docker images is working perfectly , redis container, node container, database container are running perfectly but the php container is the one giving me problems. Once i run docker compose up -d , i get the following error

2023-10-24 14:26:53   Connection refused
2023-10-24 14:26:53 
2023-10-24 14:26:53   at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:155
2023-10-24 14:26:53     151▕         if (version_compare(phpversion('redis'), '5.3.0', '>=') && ! is_null($context = Arr::get($config, 'context'))) {
2023-10-24 14:26:53     152▕             $parameters[] = $context;
2023-10-24 14:26:53     153▕         }
2023-10-24 14:26:53     154▕ 
2023-10-24 14:26:53   ➜ 155▕         $client->{($persistent ? 'pconnect' : 'connect')}(...$parameters);
2023-10-24 14:26:53     156▕     }
2023-10-24 14:26:53     157▕ 
2023-10-24 14:26:53     158▕     /**
2023-10-24 14:26:53     159▕      * Create a new redis cluster instance.
2023-10-24 14:26:53 
2023-10-24 14:26:53       +25 vendor frames 
2023-10-24 14:26:53   26  artisan:37
2023-10-24 14:26:53       Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Please or to participate in this conversation.