简体   繁体   English

通过 Docker 的 PHP Redis 连接 - 无法连接到 redis 容器(撰写设置)

[英]PHP Redis connection via Docker - can't connect to redis container (compose setup)

I'm trying to establish connection from a HTTP server container to Redis container using phpredis in PHP.我正在尝试使用 PHP 中的 phpredis 建立从 HTTP 服务器容器到 Redis 容器的连接。

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:这是Redis客户端设置:

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这是我使用 phpunit 运行的测试来测试连接

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:当我运行测试(使用“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

When I 'exec' to the http server container 'arcade_cache' hostname is properly resolved当我'exec'到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

When I try to use the IP ( $redis->connect('172.28.0.3'); ) instead of hostname connection get's timed out:当我尝试使用 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

Connection to DB using 'marry' hostname works fine.使用 'marry' 主机名连接到数据库工作正常。

Any ideas?有任何想法吗?

You may try using networks , like in this post .您可以尝试使用networks ,就像在这篇文章中一样

Off.离开。 doc networking文档网络

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM