Skip to content
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

Open
kursph opened this issue Dec 21, 2023 · 4 comments
Open

No host port mapping after creating container #3

kursph opened this issue Dec 21, 2023 · 4 comments

Comments

@kursph
Copy link

kursph commented Dec 21, 2023

I noticed when creating a new MySQLContainer like so

protected function setUp(): void
{
      $this->container = MySQLContainer::make();
      $this->container->withMySQLDatabase('pimcore');
      $this->container->withMySQLUser('pimcore_user', 'pimcore');
      try {
          $this->container->run();
      } catch (JsonException $e) {
          $this->fail($e->getMessage());
      }
}

but when checking out the docker container with docker ps and docker inspect there is no port mapped to the host (localhost). And then when I try to connect to it like so

$this->pdo = new PDO(
      sprintf('mysql:host=%s;port=3306', $this->container->getAddress()),
      'pimcore_user',
      'pimcore',
);

host missing when inspecting the container

"Ports": {
      "3306/tcp": null,
      "33060/tcp": null
},

I get this timeout error

1) Tests\App\journey\ContactIntegrationTest::testGetAllAction
PDOException: SQLSTATE[HY000] [2002] Operation timed out

here the container in Docker Desktop
Bildschirm­foto 2023-12-21 um 11 05 05

@shyim
Copy link
Collaborator

shyim commented Dec 21, 2023

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 :)

@hantsy
Copy link

hantsy commented Jun 7, 2024

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.

@shyim
Copy link
Collaborator

shyim commented Jun 7, 2024

Do you have an link to the implementation? What do they use as "IP", that would be interesting 🤔, hard-code 127.0.0.1 🤔

@hantsy
Copy link

hantsy commented Jun 8, 2024

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.

see: https://github.com/hantsy/spring-r2dbc-sample/blob/master/testcontainers/src/test/java/com/example/demo/PostRepositoryWithTestcontainersAnnotationsAndDynamicPropertySourceTest.java#L35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants