-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No host port mapping after creating container #3
Comments
In which environment are you running PHPUnit? We use the internal IP of the container right now, so we don't have to do any port mapping :) |
Testcontainer Java uses random ports if it is not set, this way can avoid port conflict. After container is started, we can get the real exposed port. |
Do you have an link to the implementation? What do they use as "IP", that would be interesting 🤔, hard-code 127.0.0.1 🤔 |
I used Testcontainers Java in projects for years, and by default when declaring a container, it will expose all ports, and map to random ports of clients(your machine), it has APIs to get the mapped ports, and first mapping port, etc. |
Same problem here. I think it's macOS-specific because docker is running differently. If no exposed port maps to the host, you can't connect to the container from the host where the PHPUnit is running. Workaround, use For example, should be easily adaptable to plain PDO: abstract class DatabaseTestCase extends TestCase
{
protected Zend_Db_Adapter_Abstract $db;
protected MySQLContainer $container;
/**
* @throws \Zend_Db_Exception
*/
protected function setUp(): void
{
$container = MySQLContainer::make('8.0');
$container->withMySQLDatabase('foo');
$container->withMySQLUser('bar', 'baz');
$randomPort = rand(3300, 3400);
$container->withPort($randomPort, 3306);
$container->run();
$params = [
'host' => '127.0.0.1',
'port' => $randomPort,
'dbname' => 'foo',
'username' => 'bar',
'password' => 'baz',
'driver_options' => [
PDO::ATTR_TIMEOUT => 2,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
],
];
$this->db = Zend_Db::factory(
"pdo_mysql",
$params
);
Zend_Db_Table_Abstract::setDefaultAdapter($this->db);
}
} |
I noticed when creating a new MySQLContainer like so
but when checking out the docker container with
docker ps
anddocker inspect
there is no port mapped to the host (localhost). And then when I try to connect to it like sohost missing when inspecting the container
I get this timeout error
here the container in Docker Desktop

The text was updated successfully, but these errors were encountered: