Skip to content

Redis

RedisContainer exposes port 6379 and waits for the log line Ready to accept connections.

Requirements

  • PHP package: predis/predis
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

declare(strict_types=1);

use Predis\Client;
use Testcontainers\Modules\RedisContainer;

$container = (new RedisContainer())->start();

try {
    $redisClient = new Client([
        'host' => $container->getHost(),
        'port' => $container->getFirstMappedPort(),
    ]);

    $redisClient->ping();
    $redisClient->set('framework', 'testcontainers-php');
    echo (string) $redisClient->get('framework');
} finally {
    $container->stop();
}