Skip to content

Commit 3aabe0f

Browse files
committed
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.
1 parent 8d04c89 commit 3aabe0f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

docs/advanced/docker-integration.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ $webui = $labels[$docker_label_webui] ?? '';
6464

6565
### Via Command Line
6666

67+
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.
68+
6769
```php
6870
<?
6971
// List running containers
@@ -78,6 +80,8 @@ $info = json_decode(implode('', $output), true);
7880

7981
### Via Docker Socket
8082

83+
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.
84+
8185
```php
8286
<?
8387
// Direct socket communication (advanced)
@@ -94,6 +98,8 @@ $containers = json_decode(implode('', $output), true);
9498

9599
### List Containers
96100

101+
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+
97103
```php
98104
<?
99105
// Get all containers (including stopped)
@@ -108,6 +114,8 @@ foreach ($output as $line) {
108114

109115
### Start/Stop Containers
110116

117+
Basic container lifecycle management functions. Always use `escapeshellarg()` to sanitize container names and check the return value to confirm success.
118+
111119
```php
112120
<?
113121
function startContainer($name) {
@@ -129,6 +137,8 @@ function restartContainer($name) {
129137

130138
### Get Container Logs
131139

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+
132142
```php
133143
<?
134144
function getContainerLogs($name, $lines = 100) {
@@ -140,6 +150,8 @@ function getContainerLogs($name, $lines = 100) {
140150

141151
### Check Container Status
142152

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+
143155
```php
144156
<?
145157
function isContainerRunning($name) {
@@ -151,6 +163,8 @@ function isContainerRunning($name) {
151163

152164
## Container Stats
153165

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.
167+
154168
```php
155169
<?
156170
// Get resource usage
@@ -163,6 +177,8 @@ $stats = json_decode($output[0], true);
163177

164178
## Working with Images
165179

180+
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+
166182
```php
167183
<?
168184
// List images
@@ -194,7 +210,7 @@ Docker templates allow Unraid to store container configurations for easy recreat
194210

195211
### DockerClient.php
196212

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).
198214

199215
```php
200216
<?php
@@ -214,7 +230,7 @@ $dockerRunning = ($retval === 0);
214230

215231
### Wrapper Script Pattern
216232

217-
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.
218234

219235
```bash
220236
#!/bin/bash
@@ -247,7 +263,7 @@ esac
247263

248264
### Stack State Detection
249265

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.
251267

252268
```php
253269
<?php

0 commit comments

Comments
 (0)