K
K
Kolya Vantukh2020-06-25 12:19:33
PHP
Kolya Vantukh, 2020-06-25 12:19:33

How to connect to Redis with php via docker?

I use docker-compose, I connect redis like this

redis:
      restart: always
      image: redis
      depends_on:
        - vesta

In php redis installed via Dockerfile like this
RUN pecl install -o -f redis \
&&  rm -rf /tmp/pear \
&&  docker-php-ext-enable redis

Everything seems to be set up right. The radish container starts up, phpinfo() shows that the radish is installed.
But trying to connect to radish via php
$redis = new Redis();
$redis->connect('127.0.0.1');
echo $redis->ping();

gives an error message
Fatal error: Uncaught RedisException: Redis server went away

Tried passing different hosts: 192.168.0.1,127.0.0.1,0.0.0.0,redis - this exception is always returned. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-06-25
@vkolya

What does it docker-compose psshow for you - are all containers running?

192.168.0.1
This is the ip of the host, there is no Redis there.
127.0.0.1
This is a localhost, there is no Redis there.
0.0.0.0
This is generally invalid ip.
redis
But by name it should work. You need to figure out the network name and do docker network inspect %name%to see Redis' ip - it should work for sure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question