0

I'm trying to establish connection from a HTTP server container to Redis container using phpredis in PHP.

This is the compose file:

version: '3'
services:
  arcade:
    build:
      context: .
      args:
        - HOST_IP=${HOST_IP}
        - XDEBUG_PORT=${XDEBUG_PORT}
    image: arcade-dev:latest
    ports:
      - "80:80"
    volumes:
      - ../../..:/var/www/localhost/htdocs
    links:
      - marry
    networks:
      - arcade_net
  marry:
    image: mariadb
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=s3cr3t
    volumes:
      - arcade_data:/var/lib/mysql
    networks:
      - arcade_net
  arcade_cache:
    image: redis
    volumes:
      - arcade_cache_data:/data
    ports:
      - "6379:6379"
    networks:
      - arcade_net
volumes:
  arcade_data: {}
  arcade_cache_data: {}
networks:
  arcade_net:
    driver: bridge

This is the Redis client setup:

use Redis;

final class ArcadeCache
{

    public static function getClient(): Redis
    {
        $redis = new Redis();
        $redis->connect('arcade_cache');
        return $redis;
    }
}

And this is a test I run using phpunit to test the connection

public function testRedisConnection(): void
{
    $client = ArcadeCache::getClient();

    $client->append('testKey', 'BAZINGA');
    $bazinga = $client->get('testKey');

    $this->assertEquals('BAZINGA', $bazinga);
}

When I run the test (using the image build for the 'arcade' service) I get the following error:

RedisException : php_network_getaddresses: getaddrinfo failed: Name does not resolve
 /opt/project/Infrastructure/ArcadeCache.php:14
 /opt/project/Tests/Infrastructure/ArcadeCacheTest.php:22

 Caused by
 PHPUnit\Framework\Error\Warning: Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

 /opt/project/Infrastructure/ArcadeCache.php:14
 /opt/project/Tests/Infrastructure/ArcadeCacheTest.php:22

When I 'exec' to the http server container 'arcade_cache' hostname is properly resolved

bash-5.0# ping arcade_cache
PING arcade_cache (172.28.0.3): 56 data bytes
64 bytes from 172.28.0.3: seq=0 ttl=64 time=0.152 ms
64 bytes from 172.28.0.3: seq=1 ttl=64 time=0.080 ms

When I try to use the IP ($redis->connect('172.28.0.3');) instead of hostname connection get's timed out:

RedisException : Operation timed out
 /opt/project/Infrastructure/ArcadeCache.php:14
 /opt/project/Tests/Infrastructure/ArcadeCacheTest.php:21

Time: 1.05 minutes, Memory: 6.00 MB

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Process finished with exit code 2

Connection to DB using 'marry' hostname works fine.

Any ideas?

2
  • Where and how are you running the tests? (From the host, the Dockerfile, a separate docker run command, docker-compose run, something else?) Commented Feb 23, 2020 at 11:48
  • From PhpStorm - I set the php interpreter to the container built by the compose file (arcade-dev:latest) and right click on the test method or the whole suite and click run test. Commented Feb 23, 2020 at 18:06

2 Answers 2

1

You may try using networks, like in this post.

Off. doc networking

Sign up to request clarification or add additional context in comments.

2 Comments

Unless I'm missing something - I am using it the same way. Tried also without network specified (by default all containers should be on the same network within compose) - but gives exactly same results. Besides - pinging between containers works, that's why it puzzles me :)
You can also delete all of the explicitly declared networks: and use the default network that Compose provides for you, as discussed in Networking in Compose. The networks: declarations in the question are self-consistent and that doesn't seem like the issue.
0

So for me, when I run tests from inside php container they worked. My redis connection was this way:

redis://redis_user:redis_password@redis:6379

the container name of the redis container is redis, that is why @redis:6379 part in the redis config.

But this config did not work when I run the test from PHPStorm. If you have the same problem you can update your redis config this way:

redis://redis_user:[email protected]:6379

to also get the tests from PHPStorm work. With this way both running tests from PHP container and PHPStorm works for me.

By the way I am using Mac device, if you have Linux device you may need to configure your Linux to support host.docker.internal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.