无法连接到redis容器(编写安装程序)

fae0ux8s  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(404)

我正在尝试使用php中的phpredis建立从http服务器容器到redis容器的连接。
这是撰写文件:

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

这是redis客户端设置:

use Redis;

final class ArcadeCache
{

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

这是我用phpunit来测试连接的一个测试

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

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

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

当我运行测试(使用“arcade”服务的映像构建)时,我得到以下错误:

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

当我'exec'到http服务器容器'arcade\u cache'时,主机名被正确解析

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

当我尝试使用ip时( $redis->connect('172.28.0.3'); )而不是主机名连接超时:

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

使用“marry”主机名连接到db工作正常。
有什么想法吗?

67up9zun

67up9zun1#

你可以试着用 networks ,就像这篇文章。
关。文档网络

相关问题