Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Fix hostname to work on MacOS (#2)
Browse files Browse the repository at this point in the history
A quick fix to mostly return 'localhost' as the host name of a container

This is largely based on the Ruby version's handling with the DOCKER_HOST stuff stripped out
  • Loading branch information
ciaranmcnulty authored Mar 19, 2024
1 parent 3d11055 commit 36226b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/GenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,20 @@ public function withEnv(array $env): self

public function getHost(): string
{
// @todo implement env vars like DOCKER_HOST

if (!$this->insideContainer()) {
return 'localhost';
}

return $this->inspect()->gateway;
}

private function insideContainer(): bool
{
return file_exists("/.dockerenv");
}

public function getMappedPort(string $port): int
{
return $this->inspect()->ports[$port];
Expand Down
9 changes: 9 additions & 0 deletions src/Module/MySql/MySqlContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function __construct(
]);
}

public function getHost() : string
{
$host = parent::getHost();

// depending on compile flags, mysql tries to connect on a local socket when
// given 'localhost' as host so we need to correct it
return $host == 'localhost' ? '127.0.0.1' : $host;
}

public function getDsn(): string
{
return "mysql:host={$this->getHost()};port={$this->getFirstMappedPort()};dbname={$this->database}";
Expand Down

0 comments on commit 36226b6

Please sign in to comment.