【问题标题】:PHP Redis connection via Docker - can't connect to redis container (compose setup)通过 Docker 的 PHP Redis 连接 - 无法连接到 redis 容器(撰写设置)
【发布时间】:2020-06-07 04:08:12
【问题描述】:

我正在尝试使用 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);
}

当我运行测试(使用“街机”服务的图像构建)时,我收到以下错误:

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

当我“执行”到 http 服务器容器时,“arcade_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' 主机名连接到数据库可以正常工作。

有什么想法吗?

【问题讨论】:

  • 您在哪里以及如何运行测试? (来自主机,Dockerfile,一个单独的docker run 命令,docker-compose run,还有别的吗?)
  • 来自 PhpStorm - 我将 php 解释器设置为由 compose 文件 (arcade-dev:latest) 构建的容器,然后右键单击测试方法或整个套件,然后单击运行测试。

标签: php docker redis phpredis


【解决方案1】:

您可以尝试使用networks,例如post

Off. doc networking

【讨论】:

  • 除非我遗漏了什么——我使用它的方式相同。在没有指定网络的情况下也尝试过(默认情况下,所有容器都应该在 compose 中的同一网络上) - 但给出完全相同的结果。此外 - 容器之间的 ping 是有效的,这就是为什么它让我感到困惑 :)
  • 您还可以删除所有显式声明的networks: 并使用 Compose 为您提供的default 网络,如Networking in Compose 中所述。问题中的 networks: 声明是自洽的,这似乎不是问题。
猜你喜欢
  • 2022-01-07
  • 1970-01-01
  • 2020-06-03
  • 2018-07-12
  • 2020-11-09
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 2019-05-11
相关资源
最近更新 更多