ContainerConfig
is a class for configuring Docker containers. It allows you to set the image, name, environment variables, commands, volumes, ports, labels, and other container parameters.
Sangezar\DockerClient\Config
public static function create(): self
Creates a new container configuration instance.
ContainerConfig
instancepublic function setImage(string $image): self
Sets the image for the container.
$image
- Image name (with tag)InvalidParameterValueException
- if the image name is emptypublic function setName(string $name): self
Sets the name for the container.
$name
- Container nameInvalidParameterValueException
- if the name is invalidpublic function addEnv(string $name, string $value): self
Adds an environment variable.
$name
- Variable name$value
- Variable valuepublic function setCmd(array $cmd): self
Sets the command to run.
$cmd
- Command as an array of argumentspublic function addVolume(string $hostPath, string $containerPath, string $mode = 'rw'): self
Adds a volume.
$hostPath
- Path on the host$containerPath
- Path in the container$mode
- Access mode (‘ro’, ‘rw’)public function addPort(int $hostPort, int $containerPort, string $protocol = 'tcp'): self
Adds a port mapping.
$hostPort
- Port on the host$containerPort
- Port in the container$protocol
- Protocol (‘tcp’, ‘udp’)public function addLabel(string $name, string $value): self
Adds a label.
$name
- Label name$value
- Label valuepublic function toArray(): array
Converts the configuration to an array for the Docker API.
InvalidConfigurationException
- if the configuration is invalidpublic function setWorkingDir(string $dir): self
Sets the working directory for the container.
$dir
- Path to the working directorypublic function setUser(string $user): self
Sets the user for the container.
$user
- Username or UIDpublic function setNetworkMode(string $mode): self
Sets the network mode for the container.
$mode - Network mode (‘bridge’, ‘host’, ‘none’, ‘container:[name |
id]’) |
public function addNetworkConnection(string $networkName, array $config = []): self
Adds a network connection.
$networkName
- Network name$config
- Connection configuration (aliases, IPAddress, etc.)public function setTty(bool $enable = true): self
Sets the TTY option.
$enable
- Enable TTY (default true)public function setOpenStdin(bool $enable = true): self
Sets the option to open stdin.
$enable
- Enable stdin opening (default true)public function setMemoryLimit(int $memoryBytes): self
Sets the memory limit for the container.
$memoryBytes
- Number of memory bytespublic function setCpuShares(int $cpuShares): self
Sets the CPU share for the container.
$cpuShares
- CPU share (relative weight)public function setRestartPolicy(string $policy, int $maxRetryCount = 0): self
Sets the container restart policy.
$policy
- Restart policy (‘no’, ‘always’, ‘unless-stopped’, ‘on-failure’)$maxRetryCount
- Maximum retry count (for ‘on-failure’)InvalidParameterValueException
- if the policy is invaliduse Sangezar\DockerClient\Config\ContainerConfig;
// Creating container configuration
$config = ContainerConfig::create()
->setImage('nginx:latest')
->setName('my-nginx')
->addEnv('NGINX_HOST', 'example.com')
->addEnv('NGINX_PORT', '80')
->addPort(8080, 80)
->addPort(8443, 443, 'tcp');
use Sangezar\DockerClient\Config\ContainerConfig;
$config = ContainerConfig::create()
->setImage('php:8.2-fpm')
->setName('php-app');
// Adding volumes
$config->addVolume('/local/path/app', '/var/www/app', 'rw')
->addVolume('/local/path/config', '/etc/nginx/conf.d', 'ro');
// Adding labels
$config->addLabel('com.example.environment', 'production')
->addLabel('com.example.version', '1.0.0')
->addLabel('maintainer', 'team@example.com');
use Sangezar\DockerClient\Config\ContainerConfig;
$config = ContainerConfig::create()
->setImage('mysql:8.0')
->setName('db-server');
// Configuring resources
$config->setMemoryLimit(512 * 1024 * 1024) // 512 MB
->setCpuShares(512)
->setRestartPolicy('always');
// Configuring network
$config->setNetworkMode('bridge')
->addNetworkConnection('app-network', [
'Aliases' => ['database', 'mysql'],
'IPAddress' => '172.18.0.10'
]);
use Sangezar\DockerClient\Config\ContainerConfig;
$config = ContainerConfig::create()
->setImage('node:18')
->setName('node-app')
->setUser('node')
->setWorkingDir('/app')
->setCmd(['npm', 'start'])
->setTty(true)
->setOpenStdin(true);
// Converting configuration to array for Docker API
$apiConfig = $config->toArray();
use Sangezar\DockerClient\Config\ContainerConfig;
use Sangezar\DockerClient\DockerClient;
// Creating Docker client
$client = DockerClient::createUnix();
// Creating container configuration
$config = ContainerConfig::create()
->setImage('wordpress:latest')
->setName('my-wordpress')
->addEnv('WORDPRESS_DB_HOST', 'db-server')
->addEnv('WORDPRESS_DB_USER', 'wordpress')
->addEnv('WORDPRESS_DB_PASSWORD', 'secret')
->addPort(8000, 80)
->addVolume('/local/path/wordpress', '/var/www/html', 'rw')
->setRestartPolicy('unless-stopped')
->setNetworkMode('bridge')
->addNetworkConnection('wordpress-network');
// Creating container
$container = $client->container()->create($config);
echo "Container created with ID: " . $container['Id'] . "\n";
// Starting container
$client->container()->start($container['Id']);
echo "Container started\n";