mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 15:29:31 +00:00
add withHostname, withName, withLabels support
This commit is contained in:
@@ -31,16 +31,6 @@ class Container extends GenericContainer
|
||||
return $this->withCommand($cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `withEntrypoint` instead
|
||||
* TODO: this is just dummy method for compatibility,
|
||||
* the implementation with Docker Engine API should be discussed
|
||||
*/
|
||||
public function withHostname(string $hostname): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `withPrivilegedMode` instead
|
||||
*/
|
||||
|
||||
@@ -29,6 +29,16 @@ class GenericContainer implements TestContainer
|
||||
|
||||
protected string $image;
|
||||
|
||||
protected ?string $name = null;
|
||||
|
||||
/**
|
||||
* User-defined key/value metadata.
|
||||
* @param array<string, string>|null $labels
|
||||
*/
|
||||
protected ?array $labels = null;
|
||||
|
||||
protected ?string $hostname = null;
|
||||
|
||||
protected string $id;
|
||||
|
||||
/** @var list<string> */
|
||||
@@ -140,9 +150,20 @@ class GenericContainer implements TestContainer
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withHostname(string $hostname): static
|
||||
{
|
||||
$this->hostname = $hostname;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withMount(string $localPath, string $containerPath): static
|
||||
{
|
||||
$this->mounts[] = new Mount(['type' => 'bind', 'source' => $localPath, 'target' => $containerPath]);
|
||||
$this->mounts[] = new Mount([
|
||||
'type' => 'bind',
|
||||
'source' => $localPath,
|
||||
'target' => $containerPath,
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -170,6 +191,23 @@ class GenericContainer implements TestContainer
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $labels
|
||||
*/
|
||||
public function withLabels(array $labels): static
|
||||
{
|
||||
$this->labels = $labels;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withPrivilegedMode(bool $privileged = true): static
|
||||
{
|
||||
$this->isPrivileged = $privileged;
|
||||
@@ -196,9 +234,13 @@ class GenericContainer implements TestContainer
|
||||
{
|
||||
$this->startAttempts++;
|
||||
$containerConfig = $this->createContainerConfig();
|
||||
$queryParameters = [];
|
||||
if ($this->name !== null) {
|
||||
$queryParameters['name'] = $this->name;
|
||||
}
|
||||
try {
|
||||
/** @var ContainerCreateResponse|null $containerCreateResponse */
|
||||
$containerCreateResponse = $this->dockerClient->containerCreate($containerConfig);
|
||||
$containerCreateResponse = $this->dockerClient->containerCreate($containerConfig, $queryParameters);
|
||||
$this->id = $containerCreateResponse?->getId() ?? '';
|
||||
} catch (ContainerCreateNotFoundException) {
|
||||
if ($this->startAttempts >= self::MAX_START_ATTEMPTS) {
|
||||
@@ -223,6 +265,8 @@ class GenericContainer implements TestContainer
|
||||
$containerCreatePostBody = new ContainersCreatePostBody();
|
||||
$containerCreatePostBody->setImage($this->image);
|
||||
$containerCreatePostBody->setCmd($this->command);
|
||||
$containerCreatePostBody->setLabels($this->labels);
|
||||
$containerCreatePostBody->setHostname($this->hostname);
|
||||
|
||||
$envs = array_map(static fn ($key, $value) => "$key=$value", array_keys($this->env), $this->env);
|
||||
$containerCreatePostBody->setEnv($envs);
|
||||
|
||||
Reference in New Issue
Block a user