Container
is a class for working with Docker containers through the API. It provides methods for creating, starting, stopping, restarting, removing, and managing Docker containers.
Sangezar\DockerClient\Api
The Container
class inherits from AbstractApi
and implements the ContainerInterface
interface.
public const SIGNAL_HUP = 'SIGHUP';
public const SIGNAL_INT = 'SIGINT';
public const SIGNAL_QUIT = 'SIGQUIT';
public const SIGNAL_ILL = 'SIGILL';
public const SIGNAL_TRAP = 'SIGTRAP';
public const SIGNAL_ABRT = 'SIGABRT';
public const SIGNAL_BUS = 'SIGBUS';
public const SIGNAL_FPE = 'SIGFPE';
public const SIGNAL_KILL = 'SIGKILL';
public const SIGNAL_USR1 = 'SIGUSR1';
public const SIGNAL_SEGV = 'SIGSEGV';
public const SIGNAL_USR2 = 'SIGUSR2';
public const SIGNAL_PIPE = 'SIGPIPE';
public const SIGNAL_ALRM = 'SIGALRM';
public const SIGNAL_TERM = 'SIGTERM';
public const SIGNAL_STKFLT = 'SIGSTKFLT';
public const SIGNAL_CHLD = 'SIGCHLD';
public const SIGNAL_CONT = 'SIGCONT';
public const SIGNAL_STOP = 'SIGSTOP';
public const SIGNAL_TSTP = 'SIGTSTP';
public const SIGNAL_TTIN = 'SIGTTIN';
public const SIGNAL_TTOU = 'SIGTTOU';
public const SIGNAL_URG = 'SIGURG';
public const SIGNAL_XCPU = 'SIGXCPU';
public const SIGNAL_XFSZ = 'SIGXFSZ';
public const SIGNAL_VTALRM = 'SIGVTALRM';
public const SIGNAL_PROF = 'SIGPROF';
public const SIGNAL_WINCH = 'SIGWINCH';
public const SIGNAL_IO = 'SIGIO';
public const SIGNAL_PWR = 'SIGPWR';
public const SIGNAL_SYS = 'SIGSYS';
public const SIGNAL_POLL = 'SIGPOLL';
public function list(array $parameters = []): array
Gets a list of containers.
$parameters
- Array of parameters for filtering results:
all
(bool) - Show all containers (by default only running containers are shown)limit
(int) - Limit the number of resultssize
(bool) - Show container sizesfilters (array |
string) - Filters in JSON format |
InvalidParameterValueException
- if invalid parameters are passedpublic function create(ContainerConfig $config): array
Creates a new container.
$config
- ContainerConfig
object with container configurationMissingRequiredParameterException
- if required parameters are missingInvalidParameterValueException
- if invalid parameters are passedInvalidConfigurationException
- if the configuration is invalidOperationFailedException
- if the operation failspublic function inspect(string $containerId): array
Gets detailed information about a container.
$containerId
- Container ID or nameMissingRequiredParameterException
- if the container ID is emptyNotFoundException
- if the container is not foundpublic function start(string $containerId): bool
Starts a container.
$containerId
- Container ID or nametrue
if the container was successfully startedMissingRequiredParameterException
- if the container ID is emptyOperationFailedException
- if the operation failsNotFoundException
- if the container is not foundpublic function stop(string $containerId, int $timeout = 10): bool
Stops a container.
$containerId
- Container ID or name$timeout
- Timeout in seconds before forced stop (default 10 seconds)true
if the container was successfully stoppedMissingRequiredParameterException
- if the container ID is emptyOperationFailedException
- if the operation failsNotFoundException
- if the container is not foundpublic function restart(string $containerId, int $timeout = 10): bool
Restarts a container.
$containerId
- Container ID or name$timeout
- Timeout in seconds before forced restart (default 10 seconds)true
if the container was successfully restartedMissingRequiredParameterException
- if the container ID is emptyOperationFailedException
- if the operation failsNotFoundException
- if the container is not foundpublic function kill(string $id, string $signal = null): bool
Kills a container by sending a signal.
$id
- Container ID or name$signal
- Signal to send (default SIGKILL)true
if the container was successfully killedMissingRequiredParameterException
- if the container ID is emptyInvalidParameterValueException
- if the signal is invalidOperationFailedException
- if the operation failsNotFoundException
- if the container is not foundpublic function remove(string $containerId, bool $force = false, bool $removeVolumes = false): bool
Removes a container.
$containerId
- Container ID or name$force
- Force removal, even if the container is running (default false
)$removeVolumes
- Remove associated volumes (default false
)true
if the container was successfully removedMissingRequiredParameterException
- if the container ID is emptyOperationFailedException
- if the operation failsNotFoundException
- if the container is not foundpublic function logs(string $containerId, array $parameters = []): array
Gets container logs.
$containerId
- Container ID or name$parameters
- Array of parameters for filtering logs:
follow
(bool) - Follow log outputstdout
(bool) - Include stdout (default true
)stderr
(bool) - Include stderr (default true
)since
(int) - Timestamp for log startuntil
(int) - Timestamp for log endtimestamps
(bool) - Include timestampstail (string |
int) - Number of lines to show from the end of logs |
MissingRequiredParameterException
- if the container ID is emptyInvalidParameterValueException
- if invalid parameters are passedNotFoundException
- if the container is not foundpublic function stats(string $containerId, bool $stream = false): array
Gets container resource usage statistics.
$containerId
- Container ID or name$stream
- Get statistics in real-time (default false
)MissingRequiredParameterException
- if the container ID is emptyNotFoundException
- if the container is not foundpublic function exists(string $containerId): bool
Checks if a container exists.
$containerId
- Container ID or nametrue
if the container exists, false
otherwiseMissingRequiredParameterException
- if the container ID is emptyuse Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$containers = $client->container()->list(['all' => true]);
foreach ($containers as $container) {
echo "Container: {$container['Names'][0]}, status: {$container['Status']}\n";
}
use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Config\ContainerConfig;
$client = DockerClient::createUnix();
$config = new ContainerConfig();
$config->setName('my-container')
->setImage('nginx:latest')
->setExposedPorts(['80/tcp' => []])
->setHostConfig([
'PortBindings' => [
'80/tcp' => [['HostPort' => '8080']]
]
]);
$container = $client->container()->create($config);
$client->container()->start($container['Id']);
echo "Container created with ID: {$container['Id']}\n";
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$containerId = 'my-container';
if ($client->container()->exists($containerId)) {
$client->container()->stop($containerId);
$client->container()->remove($containerId);
echo "Container {$containerId} stopped and removed\n";
}