<p>I came to a very specific case by using Laravel framework as a part of a kubernetes cluster. These are the facts, which have to be known:</p>

<ul>
<li>I've created a Docker container for caching called <code>redis</code></li>
<li>I've created a Docker container for application called <code>application</code></li>
<li>These two work together in a Kubernetes cluster</li>
</ul>

<p>Kubernetes is setting ENV variables in each Docker container. Commonly, one is called <code>{container-name}_PORT</code>. Therefore, Kubernetes has created the ENV variable <code>REDIS_PORT</code> in my <code>application</code> container, which is set to something like that: <code>tcp://{redis-container-ip}:{redis-container-port}</code>.</p>

<p>Laravel sets this ENV variable too, but use it as a standalone port variable like <code>6379</code>. However, in this specific case, Redis does not work in Laravel, because of overwritten <code>REDIS_PORT</code> variable. The framework try to fetch redis on this example host string inside Kubernetes: <code>tcp://redis:tcp://10.7.240.204:6379</code>. Laravel logic behind: <code>{scheme}://{REDIS_HOST}:{REDIS_PORT}</code>. You can see, <code>REDIS_PORT</code> is filled with <code>tcp://10.7.240.204:6379</code>.</p>

<p><strong>What is preferable to solve the issue?</strong></p>

<p>In my opinion, Kubernetes uses the ENV variable for <code>{container-name}_PORT</code> in a wrong way, but I do understand the internal logic behind Kubernetes ENV variables.</p>

<p>At the moment, I have changed my <code>config/database.php</code> configuration in Laravel, but this causes a review of changelogs on every update.</p>

<p><em>Some of other details can be read here: <a href="https://github.com/laravel/framework/issues/24999" rel="nofollow noreferrer">https://github.com/laravel/framework/issues/24999</a></em></p>

<p>@Florian's <a href="https://github.com/laravel/framework/issues/24999" rel="nofollow noreferrer">reply</a> to himself on github:</p>

<p>My solution was to change the config in <code>config/database.php</code> like that:</p>

<pre><code>'redis' =&gt; [

    'client' =&gt; 'predis',

    'default' =&gt; [
        'scheme' =&gt; 'tcp',
        'host' =&gt; env('REDIS_SERVICE_HOST', env('REDIS_HOST','127.0.0.1')),
        'port' =&gt; env('REDIS_SERVICE_PORT', env('REDIS_PORT',6379)),
        'password' =&gt; env('REDIS_PASSWORD', null),
        'database' =&gt; 0,
    ],

],
</code></pre>

<p>Now, the config checks first, if the REDIS_SERVICE_HOST and REDIS_SERVICE_PORT are present as ENV variable. This is the case, if you have a container in a docker/kubernetes cluster which is called REDIS.</p>

<p>Advantage of this solution is, that REDIS_SERVICE_HOST returns the IP address of the container, not a hostname. Therefore, there is no dns resolution anymore for this internal connections.</p>
