You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance Docker integration documentation with additional details on command line interaction, Docker socket communication, container lifecycle management, log retrieval, and resource usage statistics for improved clarity and usability.
The simplest way to interact with Docker from PHP is using the `exec()` function to run Docker CLI commands. Use the `--format` flag with Go templates to get structured output that's easy to parse.
For more advanced use cases, you can communicate directly with the Docker daemon via its Unix socket. This gives access to the full Docker API but requires more complex request handling.
Retrieve all containers (running and stopped) as JSON objects. The `--format '{{json .}}'` flag outputs one JSON object per line, which you can parse into an array.
102
+
97
103
```php
98
104
<?
99
105
// Get all containers (including stopped)
@@ -108,6 +114,8 @@ foreach ($output as $line) {
108
114
109
115
### Start/Stop Containers
110
116
117
+
Basic container lifecycle management functions. Always use `escapeshellarg()` to sanitize container names and check the return value to confirm success.
118
+
111
119
```php
112
120
<?
113
121
function startContainer($name) {
@@ -129,6 +137,8 @@ function restartContainer($name) {
129
137
130
138
### Get Container Logs
131
139
140
+
Fetch recent log output from a container. The `--tail` flag limits output to the most recent lines. Note that `2>&1` redirects stderr (where Docker sends some log output) to stdout.
141
+
132
142
```php
133
143
<?
134
144
function getContainerLogs($name, $lines = 100) {
@@ -140,6 +150,8 @@ function getContainerLogs($name, $lines = 100) {
140
150
141
151
### Check Container Status
142
152
153
+
Use `docker inspect` with a Go template to query specific container properties. This is more efficient than parsing full JSON output when you only need one value.
154
+
143
155
```php
144
156
<?
145
157
function isContainerRunning($name) {
@@ -151,6 +163,8 @@ function isContainerRunning($name) {
151
163
152
164
## Container Stats
153
165
166
+
Get real-time resource usage for a container. The `--no-stream` flag returns a single snapshot instead of continuously updating output. The JSON format includes CPU percentage, memory usage, network I/O, and block I/O statistics.
Common image operations including listing, pulling, and removing. When pulling images, redirect stderr to capture progress output. Always check return values for error handling.
181
+
166
182
```php
167
183
<?
168
184
// List images
@@ -194,7 +210,7 @@ Docker templates allow Unraid to store container configurations for easy recreat
194
210
195
211
### DockerClient.php
196
212
197
-
Unraid provides a PHP class for Docker operations:
213
+
Unraid includes a built-in PHP class that provides helper functions for Docker operations. The `DockerUtil::ensureImageTag()` function normalizes image names to match Unraid's internal format (adding `library/` prefix for official images and ensuring a tag is present).
For plugins that manage Docker Compose, use a wrapper script:
233
+
A wrapper script provides a clean interface for compose operations from PHP. It handles argument parsing, environment file loading, and command execution. Setting `HOME=/root` ensures Docker can find its configuration. Using `getopts` makes the script easy to call with different parameters.
218
234
219
235
```bash
220
236
#!/bin/bash
@@ -247,7 +263,7 @@ esac
247
263
248
264
### Stack State Detection
249
265
250
-
Determine if a compose stack is running:
266
+
To determine if a compose stack is running, first check if any containers exist for the project, then verify at least one is actually running. A stack with containers that are all stopped should return 'stopped' rather than appearing active.
0 commit comments